当前位置:   article > 正文

Unity中特效透明度动态设置_unity 透明度动画

unity 透明度动画

Unity开发中,特效的透明度无法直接使用代码或者动画直接控制很不方便,便制作了一个一个脚本,专用来控制一个节点下的所有子节点的透明度。

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using UnityEngine;
  6. public class UIParticleSystemTweenHelper : MonoBehaviour
  7. {
  8. // Start is called before the first frame update
  9. [Range(0,1)]
  10. public float alpha = 0f;
  11. //¼ϴõ͸
  12. private float lastAlpha = -1f;
  13. private List<ParticleSystemAlpha> colorList = null;
  14. void Start()
  15. {
  16. Init();
  17. }
  18. private void Init()
  19. {
  20. colorList = new List<ParticleSystemAlpha>();
  21. AddColorData();
  22. }
  23. public void ForceInit()
  24. {
  25. Init();
  26. }
  27. void AddColorData()
  28. {
  29. ParticleSystemAlpha psa = new ParticleSystemAlpha();
  30. if (psa.psr == null)
  31. {
  32. psa.psr = GetComponentsInChildren<ParticleSystemRenderer>(true);
  33. float[] tempAlpha_1 = new float[psa.psr.Length];
  34. float[] tempAlpha_2 = new float[psa.psr.Length];
  35. for (int i = 0; i < psa.psr.Length; i++)
  36. {
  37. if (psa.psr[i].gameObject != null || psa.psr[i].enabled)
  38. {
  39. float temp_alpha = 0;
  40. if (psa.psr[i].sharedMaterial && psa.psr[i].sharedMaterial.HasColor("_Color_Water"))
  41. {
  42. temp_alpha = psa.psr[i].sharedMaterial.GetColor("_Color_Water").a;
  43. tempAlpha_2[i] = temp_alpha;
  44. }
  45. if (psa.psr[i].sharedMaterial && psa.psr[i].sharedMaterial.HasColor("_TintColor"))
  46. {
  47. temp_alpha = psa.psr[i].sharedMaterial.GetColor("_TintColor").a;
  48. }
  49. else if (psa.psr[i].sharedMaterial && psa.psr[i].sharedMaterial.HasColor("_Color"))
  50. {
  51. temp_alpha = psa.psr[i].sharedMaterial.GetColor("_Color").a;
  52. }
  53. if (temp_alpha >= 1)
  54. {
  55. temp_alpha = 1;
  56. }
  57. tempAlpha_1[i] = temp_alpha;
  58. }
  59. }
  60. if (psa.alphas_1 == null)
  61. {
  62. psa.alphas_1 = tempAlpha_1;
  63. }
  64. if (psa.alphas_2 == null)
  65. {
  66. psa.alphas_2 = tempAlpha_2;
  67. }
  68. }
  69. colorList.Add(psa);
  70. }
  71. private void OnEnable(){Update();}
  72. // Update is called once per frame
  73. void Update()
  74. {
  75. if (alpha <= 0 || colorList.Count == 0 || Mathf.Approximately(lastAlpha, alpha)) return;
  76. foreach (ParticleSystemAlpha psa in colorList)
  77. {
  78. ParticleSystemRenderer[] psr = psa.psr;
  79. float[] alphas_1 = psa.alphas_1;
  80. float[] alphas_2 = psa.alphas_2;
  81. for (int i = 0; i < psr.Length; i++)
  82. {
  83. if(psr[i].gameObject != null || psr[i].enabled)
  84. {
  85. if (psa.psr[i].sharedMaterial && psa.psr[i].sharedMaterial.HasColor("_Color_Water"))
  86. {
  87. Color c = psr[i].sharedMaterial.GetColor("_Color_Water");
  88. psr[i].sharedMaterial.SetColor("_Color_Water", new Color(c.r, c.g, c.b, alphas_2[i] * alpha));
  89. }
  90. if (psr[i].sharedMaterial && psr[i].sharedMaterial.HasColor("_TintColor"))
  91. {
  92. Color c = psr[i].sharedMaterial.GetColor("_TintColor");
  93. psr[i].sharedMaterial.SetColor("_TintColor", new Color(c.r, c.g, c.b, alphas_1[i] * alpha));
  94. }
  95. else if (psr[i].sharedMaterial && psr[i].sharedMaterial.HasColor("_Color"))
  96. {
  97. Color c = psr[i].sharedMaterial.GetColor("_Color");
  98. psr[i].sharedMaterial.SetColor("_Color", new Color(c.r, c.g, c.b, alphas_1[i] * alpha));
  99. }
  100. }
  101. }
  102. }
  103. lastAlpha = alpha;
  104. }
  105. private void OnDestroy()
  106. {
  107. colorList.Clear();
  108. colorList = null;
  109. }
  110. }
  111. public class ParticleSystemAlpha
  112. {
  113. public ParticleSystemRenderer[] psr = null;
  114. public float[] alphas_1 = null;
  115. public float[] alphas_2 = null;
  116. }

 在其父节点添加即可

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/116303?site
推荐阅读
相关标签
  

闽ICP备14008679号