当前位置:   article > 正文

unity代码控制物体的透明度总结_unity 20%透明度

unity 20%透明度

以前测试过的成功实例,主图如下,默认材料可能不行,要新建一个拖到物体上,此例用的rendering mode是不透明模式即opaque,30的透明度(下面的例子用opaque原始透明度255就不行,所以保险起见还是选用fade模式好,0-255的透明度全部可以):

 代码如下(如果物体的材料的属性设置成fade,可能就不用SetMaterialRenderingMode函数了,例子2就没有SetMaterialRenderingMode函数):

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ChangeAparent : MonoBehaviour
  5. {
  6. float transparentvalue = 1.0f;
  7. public enum RenderingMode
  8. {
  9. Opaque,
  10. Cutout,
  11. Fade,
  12. Transparent,
  13. }
  14. public static void SetMaterialRenderingMode(Material material, RenderingMode renderingMode)
  15. {
  16. switch (renderingMode)
  17. {
  18. case RenderingMode.Opaque:
  19. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  20. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  21. material.SetInt("_ZWrite", 1);
  22. material.DisableKeyword("_ALPHATEST_ON");
  23. material.DisableKeyword("_ALPHABLEND_ON");
  24. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  25. material.renderQueue = -1;
  26. break;
  27. case RenderingMode.Cutout:
  28. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  29. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  30. material.SetInt("_ZWrite", 1);
  31. material.EnableKeyword("_ALPHATEST_ON");
  32. material.DisableKeyword("_ALPHABLEND_ON");
  33. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  34. material.renderQueue = 2450;
  35. break;
  36. case RenderingMode.Fade:
  37. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  38. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  39. material.SetInt("_ZWrite", 0);
  40. material.DisableKeyword("_ALPHATEST_ON");
  41. material.EnableKeyword("_ALPHABLEND_ON");
  42. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  43. material.renderQueue = 3000;
  44. break;
  45. case RenderingMode.Transparent:
  46. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  47. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  48. material.SetInt("_ZWrite", 0);
  49. material.DisableKeyword("_ALPHATEST_ON");
  50. material.DisableKeyword("_ALPHABLEND_ON");
  51. material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
  52. material.renderQueue = 3000;
  53. break;
  54. }
  55. }
  56. public void OnTransparentTapped()
  57. {
  58. if (transparentvalue > 0)
  59. transparentvalue -= 0.1f;
  60. else
  61. transparentvalue = 1.0f;//到头了,重置
  62. GameObject.Find("SampleAnchorMe").GetComponent<MeshRenderer>().material.color = new Color(1, 1, 1, transparentvalue);
  63. SetMaterialRenderingMode(GameObject.Find("SampleAnchorMe").GetComponent<MeshRenderer>().material, RenderingMode.Fade);//此模式效果最好
  64. }
  65. }

以上方法会改变物体的颜色为白色,为了不改变颜色,只改变透明度,用以下例子2。

代码挂载到button上,如下 

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ChangeAparent : MonoBehaviour
  5. {
  6. float transparentvalue = 1.0f;
  7. public void changeit()//改变透明度
  8. {
  9. if (transparentvalue > 0)
  10. transparentvalue -= 0.1f;
  11. else
  12. transparentvalue = 1.0f;//到头了,重置
  13. Color a = GameObject.Find("Screw1").GetComponent<MeshRenderer>().material.color;
  14. a.a = transparentvalue;
  15. GameObject.Find("Screw1").GetComponent<MeshRenderer>().material.color = a;
  16. Color b = GameObject.Find("Screw2").GetComponent<MeshRenderer>().material.color;
  17. b.a = transparentvalue;
  18. GameObject.Find("Screw2").GetComponent<MeshRenderer>().material.color = b;
  19. Debug.Log($"transparentvalue is: {transparentvalue}");
  20. }
  21. }

材料设置如下:

 另外单一改变某个物体(代码挂载到此物体上)颜色的代码如下:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Spherecolor : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. void Start()
  8. {
  9. GetComponent<Renderer>().material.color = new Color(0, 1, 0);
  10. }
  11. // Update is called once per frame
  12. void Update()
  13. {
  14. }
  15. }

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

闽ICP备14008679号