• Unity 控制物体透明度变化


    1.需求

            给物体绑定一个脚本,这个脚本实现物体的透明度渐变变化,并且可以重置回原来的颜色。物体为Unity自带的材质Shader为Standard。

    2.代码

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class TransparentChanger : MonoBehaviour
    5. {
    6. ///
    7. /// 保存原颜色
    8. ///
    9. private Color[] oldColors;
    10. ///
    11. /// 控制透明度变化
    12. ///
    13. [Range(0, 1)]
    14. public float nalpha;
    15. private void Start()
    16. {
    17. Init();
    18. }
    19. private void Update()
    20. {
    21. SetOpacity(nalpha);
    22. }
    23. public void Init()
    24. {
    25. int num = 0;
    26. foreach (Renderer r in GetComponentsInChildren<Renderer>(true))
    27. {
    28. num += r.materials.Length;
    29. }
    30. oldColors = new Color[num];
    31. num = 0;
    32. foreach (Renderer r in GetComponentsInChildren<Renderer>(true))
    33. {
    34. foreach (Material m in r.materials)
    35. {
    36. try
    37. {
    38. oldColors[num++] = m.color;
    39. }
    40. catch (System.Exception e)
    41. {
    42. Debug.Log(e.Message);
    43. }
    44. }
    45. }
    46. }
    47. ///
    48. /// 重置回原来的颜色
    49. ///
    50. public void Reset()
    51. {
    52. int num = 0;
    53. foreach (Renderer r in GetComponentsInChildren<Renderer>(true))
    54. {
    55. foreach (Material m in r.materials)
    56. {
    57. try
    58. {
    59. m.color = oldColors[num++];
    60. if (m.color.a >= 1)
    61. {
    62. ChangeMaterialType(0, m);
    63. m.renderQueue = -1;
    64. }
    65. }
    66. catch (System.Exception e)
    67. {
    68. Debug.Log(e.Message);
    69. }
    70. }
    71. }
    72. }
    73. public void SetOpacity(float alpha)
    74. {
    75. int num = 0;
    76. foreach (Renderer r in GetComponentsInChildren<Renderer>(true))
    77. {
    78. foreach (Material m in r.materials)
    79. {
    80. try
    81. {
    82. float mA = Mathf.Min(alpha, oldColors[num++].a);
    83. m.color = new Color(m.color.r, m.color.g, m.color.b, mA);
    84. if (mA >= 0.98f)
    85. {
    86. ChangeMaterialType(0, m);
    87. }
    88. else
    89. {
    90. ChangeMaterialType(1, m);
    91. }
    92. }
    93. catch (System.Exception e)
    94. {
    95. Debug.Log(e.Message);
    96. }
    97. }
    98. }
    99. }
    100. ///
    101. /// 切换材质的Standard Shader 渲染类型
    102. ///
    103. /// 0-Opaque, 1-Transparent
    104. /// 材质
    105. void ChangeMaterialType(int mType, Material m)
    106. {
    107. if(mType == 0)
    108. {
    109. m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
    110. m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
    111. m.SetInt("_ZWrite", 1);
    112. m.DisableKeyword("_ALPHATEST_ON");
    113. m.DisableKeyword("_ALPHABLEND_ON");
    114. m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    115. m.renderQueue = -1;
    116. }
    117. else if(mType == 1)
    118. {
    119. m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
    120. m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    121. m.SetInt("_ZWrite", 0);
    122. m.DisableKeyword("_ALPHATEST_ON");
    123. m.DisableKeyword("_ALPHABLEND_ON");
    124. m.EnableKeyword("_ALPHAPREMULTIPLY_ON");
    125. m.renderQueue = 3000;
    126. }
    127. }
    128. }

  • 相关阅读:
    Zookeeper-3.8.0单台、集群环境搭建
    基于GStreamer和FFmpeg的OpenCV安装和使用
    QT day3作业
    一次JAVA频繁写大文件的记录
    MeterSphere 任意文件读取漏洞(CVE-2023-25814)
    试图替代 Python 的下一代AI编程语言:Mojo
    排序算法、堆排序、大顶堆、小顶堆、手写快排-215. 数组中的第K个最大元素、2336. 无限集中的最小数字
    SpringBoot中读取配置文件的几个注解
    Linux中shell外壳,用户权限,文件权限
    Spring - 单实例Bean 和 多实例Bean 的不同
  • 原文地址:https://blog.csdn.net/qq_26540577/article/details/134536620