unity动态修改标准材质自发光(Emission)
生活随笔
收集整理的這篇文章主要介紹了
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.運行效果:材質變換了
?
1.代碼
啟用自發光效果的代碼是?material.EnableKeyword("_EMISSION")
關閉自發光效果的代碼是?material.DisableKeyword("_EMISSION")
設置自發光顏色和亮度的代碼是?material.SetColor("_EmissionColor", Color.HSVToRGB(_h, _s, _v))
?
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 updatevoid 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 framevoid Update(){} }?
三.操作:三:完成:讓模型整體的自發光從發光到不發光
1.運行結果
?
1.注意
?
1.代碼
?
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 framevoid 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;}}}1.Unity設置
總結
以上是生活随笔為你收集整理的unity动态修改标准材质自发光(Emission)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: i78700k配什么显卡好_i7 870
- 下一篇: CDN可以防护什么种类的攻击?