当前位置:   article > 正文

Unity中使用C#脚本控制物体Material的透明度_c#怎么根据时间更改obj透明度

c#怎么根据时间更改obj透明度

实现方法

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

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

修改Rendering Mode
修改material的color属性
  • 1
  • 2

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

代码如下,夹带了一些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;
        }
        
    }


    
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/126964?site
推荐阅读
相关标签
  

闽ICP备14008679号