Unity动态更改3D物体透明度

/***********************
* 遇见你的眉眼,如清风明月。
* Title:        
* Date:         2021
* Author:       玄策
* UnityVersion: 2019.2.4f1
* Func:           
* -   
***********************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class ChangeColorA : MonoBehaviour
{
    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;
        }
    }
    private void OnMouseEnter()
    {
        if (EventSystem.current.IsPointerOverGameObject() == true)
        {
            return;
        }
        for (int i = 0; i < transform.parent.childCount; i++)
        {
            SetMaterialRenderingMode(transform.parent.GetChild(i).GetComponent().material,
                RenderingMode.Transparent);
            transform.parent.GetChild(i).GetComponent().material.color = new Color(
            transform.parent.GetChild(i).GetComponent().material.color.r,
            transform.parent.GetChild(i).GetComponent().material.color.g,
            transform.parent.GetChild(i).GetComponent().material.color.b,
            180f / 255f);
        }
        
    }
    private void OnMouseExit()
    {
        for (int i = 0; i < transform.parent.childCount; i++)
        {
            if (EventSystem.current.IsPointerOverGameObject() == true)
            {
                return;
            }
            SetMaterialRenderingMode(transform.parent.GetChild(i).GetComponent().material,
                RenderingMode.Opaque);
            //transform.parent.GetChild(i).GetComponent().material.color = new Color(
            //transform.parent.GetChild(i).GetComponent().material.color.r,
            //transform.parent.GetChild(i).GetComponent().material.color.g,
            //transform.parent.GetChild(i).GetComponent().material.color.b,
            //255f / 255f);
        }
    }
}

你可能感兴趣的:(Unity动态更改3D物体透明度)