|
using System;using System.Collections;using System.Collections.Generic;using System.Threading;using UnityEngine;public class UIParticleSystemTweenHelper : MonoBehaviour{ [Range(0, 1)] public float alpha = 0f; private float lastAlpha = -1f; private List<ParticleSystemAlpha> _parcolorList = null; private bool _start = false; void Start() { _start = false; Init(); } public void Init() { _parcolorList = new List<ParticleSystemAlpha>(); AddColorData(); } public void StartChangeAlpha() { _start = true; } void AddColorData() { ParticleSystemAlpha psa = new ParticleSystemAlpha(); if (psa.mPsr == null) { psa.mPsr = GetComponentsInChildren<ParticleSystemRenderer>(true); float[] tempAlpha_1 = new float[psa.mPsr.Length]; float[] tempAlpha_2 = new float[psa.mPsr.Length]; for (int i = 0; i < psa.mPsr.Length; i++) { if (psa.mPsr.gameObject != null || psa.mPsr.enabled) { float temp_alpha = 0; if (psa.mPsr.sharedMaterial && psa.mPsr.sharedMaterial.HasProperty(&#34;_Color_Water&#34;)) { temp_alpha = psa.mPsr.sharedMaterial.GetColor(&#34;_Color_Water&#34;).a; tempAlpha_2 = temp_alpha; } if (psa.mPsr.sharedMaterial && psa.mPsr.sharedMaterial.HasProperty(&#34;_TintColor&#34;)) { temp_alpha = psa.mPsr.sharedMaterial.GetColor(&#34;_TintColor&#34;).a; } else if (psa.mPsr.sharedMaterial && psa.mPsr.sharedMaterial.HasProperty(&#34;_Color&#34;)) { temp_alpha = psa.mPsr.sharedMaterial.GetColor(&#34;_Color&#34;).a; } if (temp_alpha >= 1) { temp_alpha = 1; } tempAlpha_1 = temp_alpha; } } if (psa.mAlphas1 == null) { psa.mAlphas1 = tempAlpha_1; } if (psa.mAlphas2 == null) { psa.mAlphas2 = tempAlpha_2; } } _parcolorList.Add(psa); } // Update is called once per frame void Update() { if (!_start) { return; } if (alpha <= 0 || _parcolorList.Count == 0 || Mathf.Approximately(lastAlpha, alpha)) return; foreach (ParticleSystemAlpha psa in _parcolorList) { ParticleSystemRenderer[] psr = psa.mPsr; float[] alphas_1 = psa.mAlphas1; float[] alphas_2 = psa.mAlphas2; for (int i = 0; i < psr.Length; i++) { if (psr.gameObject != null || psr.enabled) { if (psa.mPsr.sharedMaterial && psa.mPsr.sharedMaterial.HasProperty(&#34;_Color_Water&#34;)) { Color c = psr.sharedMaterial.GetColor(&#34;_Color_Water&#34;); psr.sharedMaterial.SetColor(&#34;_Color_Water&#34;, new Color(c.r, c.g, c.b, alphas_2 * alpha)); } if (psr.sharedMaterial && psr.sharedMaterial.HasProperty(&#34;_TintColor&#34;)) { Color c = psr.sharedMaterial.GetColor(&#34;_TintColor&#34;); psr.sharedMaterial.SetColor(&#34;_TintColor&#34;, new Color(c.r, c.g, c.b, alphas_1 * alpha)); } else if (psr.sharedMaterial && psr.sharedMaterial.HasProperty(&#34;_Color&#34;)) { Color c = psr.sharedMaterial.GetColor(&#34;_Color&#34;); psr.sharedMaterial.SetColor(&#34;_Color&#34;, new Color(c.r, c.g, c.b, alphas_1 * alpha)); } } } } lastAlpha = alpha; } public void ResetAlpha() { _start = true; alpha = 1; Update(); _start = false; } private void OnDestroy() { _parcolorList.Clear(); _parcolorList = null; }}public class ParticleSystemAlpha{ public ParticleSystemRenderer[] mPsr = null; public float[] mAlphas1 = null; public float[] mAlphas2 = null;} |
|