Unity中使用C#脚本控制物体Material的透明度

实现方法

工程中遇到了这样的问题,需要进行调整物体的透明度。
通过查找资料发现,需要将Object的Rendering Mode调整为Fade才可以进行透明度a的调整。

因此,调整物体的透明度分为两步骤:

修改Rendering Mode
修改material的color属性

将物体调成实体也是同样的操作

代码如下,夹带了一些VRTK的手柄监视,就不删去了。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using VRTK;
using UnityEngine.Animations;

public class Car_boom : MonoBehaviour
{
    public GameObject carboom;
    public GameObject driver;
    public VRTK_ControllerEvents controllerEvents;
    public bool judge = false;

    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;
        }
    }    

    // Start is called before the first frame update
    void Start()
    {
        driver = GameObject.Find("Gengon001");
        controllerEvents = GetComponent<VRTK_ControllerEvents>();
        //Setup controller event listeners
        GetComponent<VRTK_ControllerEvents>().StartMenuPressed += new ControllerInteractionEventHandler(DoStartMenuPressed);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void DoStartMenuPressed(object sender, ControllerInteractionEventArgs e)
    {
        if(!judge)//透明
        {
            //透明与renderingmode
            SetMaterialRenderingMode(driver.GetComponent<MeshRenderer>().material,RenderingMode.Fade);
            Color cr = driver.GetComponent<MeshRenderer>().material.color;
            cr.a = 100f/255f;
            driver.GetComponent<MeshRenderer>().material.color = cr;

            judge = true;
        }
        else//实体
        {
            //透明与renderingmode
            SetMaterialRenderingMode(driver.GetComponent<MeshRenderer>().material,RenderingMode.Opaque);
            Color cr = driver.GetComponent<MeshRenderer>().material.color;
            cr.a = 255f/255f;
            driver.GetComponent<MeshRenderer>().material.color = cr;

            judge = false;
        }
        
    }


    
}

你可能感兴趣的:(Unity,VR,c#,unity)