protected override void OnDisable() { base.OnDisable(); MaterialCache.Unregister(_materialCache); _materialCache = null; _player.OnDisable(); }
public static MaterialCache Register(ulong hash, Texture texture, System.Func <Material> onCreateMaterial) { var cache = materialCaches.FirstOrDefault(x => x.hash == hash); if (cache != null && cache.material) { if (cache.material) { cache.referenceCount++; } else { materialCaches.Remove(cache); cache = null; } } if (cache == null) { cache = new MaterialCache() { hash = hash, material = onCreateMaterial(), referenceCount = 1, }; materialCaches.Add(cache); } return(cache); }
public static void Unregister(MaterialCache cache) { if (cache == null) { return; } cache.referenceCount--; if (cache.referenceCount <= 0) { MaterialCache.materialCaches.Remove(cache); cache.material = null; } }
/// <summary> /// Modifies the material. /// </summary> public override void ModifyMaterial() { if (isTMPro) { return; } ulong hash = (m_NoiseTexture ? (uint)m_NoiseTexture.GetInstanceID() : 0) + ((ulong)1 << 32) + ((ulong)m_ColorMode << 36); if (_materialCache != null && (_materialCache.hash != hash || !isActiveAndEnabled || !m_EffectMaterial)) { MaterialCache.Unregister(_materialCache); _materialCache = null; } if (!isActiveAndEnabled || !m_EffectMaterial) { material = null; } else if (!m_NoiseTexture) { material = m_EffectMaterial; } else if (_materialCache != null && _materialCache.hash == hash) { material = _materialCache.material; } else { _materialCache = MaterialCache.Register(hash, m_NoiseTexture, () => { var mat = new Material(m_EffectMaterial); mat.name += "_" + m_NoiseTexture.name; mat.SetTexture("_NoiseTex", m_NoiseTexture); return(mat); }); material = _materialCache.material; } }