栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

unity动态修改标准材质自发光(Emission)

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unity动态修改标准材质自发光(Emission)

目录

一.目的

1.想知道:unity动态修改标准材质自发光(Emission)

二.参考

1Unity利用材质自发光实现物体闪烁

三.操作:一:完成:变换材质自发光的数值

1.运行效果:材质变换了

1.代码

1.Unity设置

三.操作:二:完成:开关 材质自发光

1.运行效果:如果一开始关闭自发光、那么就开启;一开始开启,就关闭;

1.Unity设置

1.代码:开关 材质的自发光

三.操作:三:完成:让模型整体的自发光从发光到不发光

1.运行结果

1.注意

1.代码

1.Unity设置


一.目的

1.想知道:unity动态修改标准材质自发光(Emission)

 

二.参考

1Unity利用材质自发光实现物体闪烁

https://blog.csdn.net/qq_21397217/article/details/80967432

  1. 总结:good:亲测有效

 

三.操作:一:完成:变换材质自发光的数值

1.运行效果:材质变换了

 

1.代码
  1. 启用自发光效果的代码是 material.EnableKeyword("_EMISSION")

    关闭自发光效果的代码是 material.DisableKeyword("_EMISSION")

    设置自发光颜色和亮度的代码是 material.SetColor("_EmissionColor", Color.HSVToRGB(_h, _s, _v))

  2. 其中的 _h、_s、_v参数分别代表颜色的色相、饱和度和亮度。
  3. 获取颜色的色相、饱和度和亮度的代码为 Color.RGBToHSV(color, out _h, out _s, out _v)

material_f22_main.SetColor("_EmissionColor", new Color(0,1,1));

 

1.Unity设置

 

 

三.操作:二:完成:开关 材质自发光

1.运行效果:如果一开始关闭自发光、那么就开启;一开始开启,就关闭;
  1. 总结:材质是个实例,而不是对原本材质进行处理
  2. 总结:文件夹里面的材质不会变换,变换的新建的材质,这个是有区别的

 

1.Unity设置

 

1.代码:开关 材质的自发光
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyTest_emission : MonoBehaviour
{
    /// Material:主要使用到的材质
    [Header("Material:主要使用到的材质")]
    [Tooltip("Material:主要使用到的材质")]
    public Material material;    

    private readonly string _keyword = "_EMISSION";
    private readonly string _colorName = "_EmissionColor";

    // Start is called before the first frame update
    void Start()
    {
        测试:1 :失败:开启自发光
        //material = this.GetComponent().material;
        //material.EnableKeyword(_keyword);

        测试:2:完成:开启自发光   
        //material = this.GetComponent().material;
        //material.EnableKeyword(_keyword);

        //测试:3:待检测:开关自发光       
        material = this.GetComponent().material;
        if (material.IsKeywordEnabled("_EMISSION"))
        {
            material.DisableKeyword(_keyword);
        }
        else
        {
            material.EnableKeyword(_keyword);
        }

    }

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

 

三.操作:三:完成:让模型整体的自发光从发光到不发光

1.运行结果
  1. 可以发现自发光从亮到不亮

 

1.注意
  1. 如果要Build出来让自发光有效,需要将材质的自发光一开始就要打开。

 

1.代码
  1. 总结:操作的材质不能是Project里面的材质,只能是对象自己材质新建一个对象,这样避免Project里面材质变换导致对象的材质也变换了
  2. 总结:材质变换需要使用 面向对象的思想
  3. 总结:使用List,列表将整个模型的材质球都添加,对其整体操作;不能使用Material的数组,这样只会对数组最后赋值的材质进行变换
  4. 总结:协程作为定时器材质球变换很不错的,
  5. 总结:使用  private Coroutine coroutine_glinting 进行控制协程句柄,值得学习。

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 
/// 功能:F22材质变换,模拟出从虚到实的效果
/// 
public class MyMaterialsChange : MonoBehaviour
{
    /// float:材质变换的速度
    [Header("float:材质变换的速度")]
    [Tooltip("float:材质变换的速度")]
    public float fSpeed_materials=0.1f;

    /// Color:开始的颜色
    private Color color_from=new Color(0,0,1);

    /// Color:结束的颜色
    private Color color_to= new Color(0, 0, 0);

    /// Coroutine:协程句柄
    private Coroutine coroutine_glinting;

    ///   List:链表<材质>:需要自发光的材质
    List list_material_needEmission;

    /// string:自发光材质属性的关键字
    private readonly string str_keyword = "_EMISSION";

    /// string:自发光材质属性的颜色名字
    private readonly string str_colorName = "_EmissionColor";

    void Start()
    {
        //使用链表:开启自发光后更改自发光属性,让其变得虚幻
        MeshRenderer[] arr_MeshRenderer = this.GetComponentsInChildren();
        list_material_needEmission = new List();

        //列表赋值
        for (int i = 0; i < arr_MeshRenderer.Length; i++)
        {
            if (arr_MeshRenderer[i].materials[0])
            {
                list_material_needEmission.Add(arr_MeshRenderer[i].materials[0]);
            }
        }

        //列表材质自发光变换
        for (int i = 0;  i 
    /// 开始闪烁。
    /// 
    public void Start_glinting()
    {
        if (coroutine_glinting != null)
        {
            StopCoroutine(coroutine_glinting);
        }
        coroutine_glinting = StartCoroutine(IE_glinting());
    }

    /// 
    /// 停止闪烁。
    /// 
    public void Stop_glinting()
    {
        if (coroutine_glinting != null)
        {
            StopCoroutine(coroutine_glinting);
        }
    }

    /// 
    /// 控制自发光强度。
    /// 
    /// 
    private IEnumerator IE_glinting()
    {
        //材质变换
        while (true)
        {
            for (int i = 0; i  
1.Unity设置 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/905685.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号