using System.Collections.Generic; using UnityEngine; namespace MoleMole { public class MonoSyncDistortion : MonoBehaviour { private const string DISTORTION_MATERIAL_NAME = "DistortionNormal"; private const string DISTORTION_INTENSITY_NAME = "_DistortionIntensity"; private const string DISTORTION_SPTRANSITION_NAME = "_SPTransition"; private bool needSync; private MaterialPropertyBlock _sourceMaterial; private List> _targetMaterials; private void Start() { _targetMaterials = new List>(); foreach (Transform item in base.transform) { if (item.GetComponent() != null) { needSync = true; MaterialPropertyBlock materialPropertyBlock = new MaterialPropertyBlock(); item.GetComponent().GetPropertyBlock(materialPropertyBlock); _targetMaterials.Add(new KeyValuePair(item.GetComponent(), materialPropertyBlock)); } } _sourceMaterial = new MaterialPropertyBlock(); base.transform.GetComponent().GetPropertyBlock(_sourceMaterial); } private void Update() { if (!needSync) { return; } base.transform.GetComponent().GetPropertyBlock(_sourceMaterial); foreach (KeyValuePair targetMaterial in _targetMaterials) { targetMaterial.Value.SetFloat("_DistortionIntensity", _sourceMaterial.GetFloat("_DistortionIntensity")); targetMaterial.Value.SetFloat("_SPTransition", _sourceMaterial.GetFloat("_SPTransition")); if (targetMaterial.Key != null) { targetMaterial.Key.SetPropertyBlock(targetMaterial.Value); } } } } }