美文网首页
Unity动态更改3D物体透明度

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

作者: 玄策丶 | 来源:发表于2022-08-18 10:04 被阅读0次
    /***********************
    * 遇见你的眉眼,如清风明月。
    * 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<MeshRenderer>().material,
                    RenderingMode.Transparent);
                transform.parent.GetChild(i).GetComponent<MeshRenderer>().material.color = new Color(
                transform.parent.GetChild(i).GetComponent<MeshRenderer>().material.color.r,
                transform.parent.GetChild(i).GetComponent<MeshRenderer>().material.color.g,
                transform.parent.GetChild(i).GetComponent<MeshRenderer>().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<MeshRenderer>().material,
                    RenderingMode.Opaque);
                //transform.parent.GetChild(i).GetComponent<MeshRenderer>().material.color = new Color(
                //transform.parent.GetChild(i).GetComponent<MeshRenderer>().material.color.r,
                //transform.parent.GetChild(i).GetComponent<MeshRenderer>().material.color.g,
                //transform.parent.GetChild(i).GetComponent<MeshRenderer>().material.color.b,
                //255f / 255f);
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Unity动态更改3D物体透明度

          本文链接:https://www.haomeiwen.com/subject/yrnjgrtx.html