目录
1.想知道:unity动态修改标准材质自发光(Emission)
1.运行效果:如果一开始关闭自发光、那么就开启;一开始开启,就关闭;
一.目的
1.想知道:unity动态修改标准材质自发光(Emission)
二.参考
1Unity利用材质自发光实现物体闪烁
https://blog.csdn.net/qq_21397217/article/details/80967432
- 总结:good:亲测有效
三.操作:一:完成:变换材质自发光的数值
1.运行效果:材质变换了
1.代码
启用自发光效果的代码是 material.EnableKeyword("_EMISSION")
关闭自发光效果的代码是 material.DisableKeyword("_EMISSION")
设置自发光颜色和亮度的代码是 material.SetColor("_EmissionColor", Color.HSVToRGB(_h, _s, _v))
- 其中的 _h、_s、_v参数分别代表颜色的色相、饱和度和亮度。
- 获取颜色的色相、饱和度和亮度的代码为 Color.RGBToHSV(color, out _h, out _s, out _v)
material_f22_main.SetColor("_EmissionColor", new Color(0,1,1));
1.Unity设置
三.操作:二:完成:开关 材质自发光
1.运行效果:如果一开始关闭自发光、那么就开启;一开始开启,就关闭;
- 总结:材质是个实例,而不是对原本材质进行处理
- 总结:文件夹里面的材质不会变换,变换的新建的材质,这个是有区别的
1.Unity设置
1.代码:开关 材质的自发光
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyTest_emission : MonoBehaviour
{
/// <summary>Material:主要使用到的材质</summary>
[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<MeshRenderer>().material;
//material.EnableKeyword(_keyword);
测试:2:完成:开启自发光
//material = this.GetComponent<MeshRenderer>().material;
//material.EnableKeyword(_keyword);
//测试:3:待检测:开关自发光
material = this.GetComponent<MeshRenderer>().material;
if (material.IsKeywordEnabled("_EMISSION"))
{
material.DisableKeyword(_keyword);
}
else
{
material.EnableKeyword(_keyword);
}
}
// Update is called once per frame
void Update()
{
}
}
三.操作:三:完成:让模型整体的自发光从发光到不发光
1.运行结果
- 可以发现自发光从亮到不亮
1.注意
- 如果要Build出来让自发光有效,需要将材质的自发光一开始就要打开。
1.代码
- 总结:操作的材质不能是Project里面的材质,只能是对象自己材质新建一个对象,这样避免Project里面材质变换导致对象的材质也变换了
- 总结:材质变换需要使用 面向对象的思想
- 总结:使用List,列表将整个模型的材质球都添加,对其整体操作;不能使用Material的数组,这样只会对数组最后赋值的材质进行变换
- 总结:协程作为定时器材质球变换很不错的,
- 总结:使用 private Coroutine coroutine_glinting 进行控制协程句柄,值得学习。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 功能:F22材质变换,模拟出从虚到实的效果
/// </summary>
public class MyMaterialsChange : MonoBehaviour
{
/// <summary>float:材质变换的速度</summary>
[Header("float:材质变换的速度")]
[Tooltip("float:材质变换的速度")]
public float fSpeed_materials=0.1f;
/// <summary>Color:开始的颜色</summary>
private Color color_from=new Color(0,0,1);
/// <summary>Color:结束的颜色</summary>
private Color color_to= new Color(0, 0, 0);
/// <summary>Coroutine:协程句柄</summary>
private Coroutine coroutine_glinting;
/// <summary> List<Material>:链表<材质>:需要自发光的材质</summary>
List<Material> list_material_needEmission;
/// <summary>string:自发光材质属性的关键字</summary>
private readonly string str_keyword = "_EMISSION";
/// <summary>string:自发光材质属性的颜色名字</summary>
private readonly string str_colorName = "_EmissionColor";
void Start()
{
//使用链表:开启自发光后更改自发光属性,让其变得虚幻
MeshRenderer[] arr_MeshRenderer = this.GetComponentsInChildren<MeshRenderer>();
list_material_needEmission = new List<Material>();
//列表赋值
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 <list_material_needEmission.Count; i++)
{
list_material_needEmission[i].EnableKeyword(str_keyword);
list_material_needEmission[i].SetColor(str_colorName, color_from);
}
//Start_glinting();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyUp(KeyCode.B))
{
Start_glinting();
}
}
/// <summary>
/// 开始闪烁。
/// </summary>
public void Start_glinting()
{
if (coroutine_glinting != null)
{
StopCoroutine(coroutine_glinting);
}
coroutine_glinting = StartCoroutine(IE_glinting());
}
/// <summary>
/// 停止闪烁。
/// </summary>
public void Stop_glinting()
{
if (coroutine_glinting != null)
{
StopCoroutine(coroutine_glinting);
}
}
/// <summary>
/// 控制自发光强度。
/// </summary>
/// <returns></returns>
private IEnumerator IE_glinting()
{
//材质变换
while (true)
{
for (int i = 0; i <list_material_needEmission.Count; i++)
{
Color tmpColor= list_material_needEmission[i].GetColor(str_colorName);
if (tmpColor.b < 0.01f)
{
break;
}
tmpColor = new Color(tmpColor.r, tmpColor.g, tmpColor.b-Time.deltaTime*fSpeed_materials);
list_material_needEmission[i].SetColor(str_colorName, tmpColor);
}
yield return null;
}
}
}