赞
踩
以前测试过的成功实例,主图如下,默认材料可能不行,要新建一个拖到物体上,此例用的rendering mode是不透明模式即opaque,30的透明度(下面的例子用opaque原始透明度255就不行,所以保险起见还是选用fade模式好,0-255的透明度全部可以):
代码如下(如果物体的材料的属性设置成fade,可能就不用SetMaterialRenderingMode函数了,例子2就没有SetMaterialRenderingMode函数):
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class ChangeAparent : MonoBehaviour
- {
- float transparentvalue = 1.0f;
- public enum RenderingMode
- {
- Opaque,
- Cutout,
- Fade,
- Transparent,
- }
-
- public static void SetMaterialRenderingMode(Material material, RenderingMode renderingMode)
- {
- switch (renderingMode)
- {
- case RenderingMode.Opaque:
- material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
- material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
- material.SetInt("_ZWrite", 1);
- material.DisableKeyword("_ALPHATEST_ON");
- material.DisableKeyword("_ALPHABLEND_ON");
- material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
- material.renderQueue = -1;
- break;
- case RenderingMode.Cutout:
- material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
- material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
- material.SetInt("_ZWrite", 1);
- material.EnableKeyword("_ALPHATEST_ON");
- material.DisableKeyword("_ALPHABLEND_ON");
- material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
- material.renderQueue = 2450;
- break;
- case RenderingMode.Fade:
- material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
- material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
- material.SetInt("_ZWrite", 0);
- material.DisableKeyword("_ALPHATEST_ON");
- material.EnableKeyword("_ALPHABLEND_ON");
- material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
- material.renderQueue = 3000;
- break;
- case RenderingMode.Transparent:
- material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
- material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
- material.SetInt("_ZWrite", 0);
- material.DisableKeyword("_ALPHATEST_ON");
- material.DisableKeyword("_ALPHABLEND_ON");
- material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
- material.renderQueue = 3000;
- break;
- }
- }
- public void OnTransparentTapped()
- {
- if (transparentvalue > 0)
- transparentvalue -= 0.1f;
- else
- transparentvalue = 1.0f;//到头了,重置
- GameObject.Find("SampleAnchorMe").GetComponent<MeshRenderer>().material.color = new Color(1, 1, 1, transparentvalue);
- SetMaterialRenderingMode(GameObject.Find("SampleAnchorMe").GetComponent<MeshRenderer>().material, RenderingMode.Fade);//此模式效果最好
- }
- }
以上方法会改变物体的颜色为白色,为了不改变颜色,只改变透明度,用以下例子2。
代码挂载到button上,如下
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class ChangeAparent : MonoBehaviour
- {
- float transparentvalue = 1.0f;
- public void changeit()//改变透明度
- {
- if (transparentvalue > 0)
- transparentvalue -= 0.1f;
- else
- transparentvalue = 1.0f;//到头了,重置
-
-
- Color a = GameObject.Find("Screw1").GetComponent<MeshRenderer>().material.color;
- a.a = transparentvalue;
- GameObject.Find("Screw1").GetComponent<MeshRenderer>().material.color = a;
-
- Color b = GameObject.Find("Screw2").GetComponent<MeshRenderer>().material.color;
- b.a = transparentvalue;
- GameObject.Find("Screw2").GetComponent<MeshRenderer>().material.color = b;
-
- Debug.Log($"transparentvalue is: {transparentvalue}");
-
- }
- }
材料设置如下:
另外单一改变某个物体(代码挂载到此物体上)颜色的代码如下:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class Spherecolor : MonoBehaviour
- {
- // Start is called before the first frame update
- void Start()
- {
- GetComponent<Renderer>().material.color = new Color(0, 1, 0);
- }
-
- // Update is called once per frame
- void Update()
- {
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。