unity动态修改标准材质自发光(Emission)
目錄
一.目的
1.想知道:unity動(dòng)態(tài)修改標(biāo)準(zhǔn)材質(zhì)自發(fā)光(Emission)
二.參考
1Unity利用材質(zhì)自發(fā)光實(shí)現(xiàn)物體閃爍
三.操作:一:完成:變換材質(zhì)自發(fā)光的數(shù)值
1.運(yùn)行效果:材質(zhì)變換了
1.代碼
1.Unity設(shè)置
三.操作:二:完成:開(kāi)關(guān) 材質(zhì)自發(fā)光
1.運(yùn)行效果:如果一開(kāi)始關(guān)閉自發(fā)光、那么就開(kāi)啟;一開(kāi)始開(kāi)啟,就關(guān)閉;
1.Unity設(shè)置
1.代碼:開(kāi)關(guān) 材質(zhì)的自發(fā)光
三.操作:三:完成:讓模型整體的自發(fā)光從發(fā)光到不發(fā)光
1.運(yùn)行結(jié)果
1.注意
1.代碼
1.Unity設(shè)置
一.目的
1.想知道:unity動(dòng)態(tài)修改標(biāo)準(zhǔn)材質(zhì)自發(fā)光(Emission)
?
二.參考
1Unity利用材質(zhì)自發(fā)光實(shí)現(xiàn)物體閃爍
https://blog.csdn.net/qq_21397217/article/details/80967432
?
三.操作:一:完成:變換材質(zhì)自發(fā)光的數(shù)值
1.運(yùn)行效果:材質(zhì)變換了
?
1.代碼
啟用自發(fā)光效果的代碼是?material.EnableKeyword("_EMISSION")
關(guān)閉自發(fā)光效果的代碼是?material.DisableKeyword("_EMISSION")
設(shè)置自發(fā)光顏色和亮度的代碼是?material.SetColor("_EmissionColor", Color.HSVToRGB(_h, _s, _v))
?
1.Unity設(shè)置
?
?
三.操作:二:完成:開(kāi)關(guān) 材質(zhì)自發(fā)光
1.運(yùn)行效果:如果一開(kāi)始關(guān)閉自發(fā)光、那么就開(kāi)啟;一開(kāi)始開(kāi)啟,就關(guān)閉;
?
1.Unity設(shè)置
?
1.代碼:開(kāi)關(guān) 材質(zhì)的自發(fā)光
using System.Collections; using System.Collections.Generic; using UnityEngine;public class MyTest_emission : MonoBehaviour {/// <summary>Material:主要使用到的材質(zhì)</summary>[Header("Material:主要使用到的材質(zhì)")][Tooltip("Material:主要使用到的材質(zhì)")]public Material material; private readonly string _keyword = "_EMISSION";private readonly string _colorName = "_EmissionColor";// Start is called before the first frame updatevoid Start(){測(cè)試:1 :失敗:開(kāi)啟自發(fā)光//material = this.GetComponent<MeshRenderer>().material;//material.EnableKeyword(_keyword);測(cè)試:2:完成:開(kāi)啟自發(fā)光 //material = this.GetComponent<MeshRenderer>().material;//material.EnableKeyword(_keyword);//測(cè)試:3:待檢測(cè):開(kāi)關(guān)自發(fā)光 material = this.GetComponent<MeshRenderer>().material;if (material.IsKeywordEnabled("_EMISSION")){material.DisableKeyword(_keyword);}else{material.EnableKeyword(_keyword);}}// Update is called once per framevoid Update(){} }?
三.操作:三:完成:讓模型整體的自發(fā)光從發(fā)光到不發(fā)光
1.運(yùn)行結(jié)果
?
1.注意
?
1.代碼
?
using System.Collections; using System.Collections.Generic; using UnityEngine;/// <summary> /// 功能:F22材質(zhì)變換,模擬出從虛到實(shí)的效果 /// </summary> public class MyMaterialsChange : MonoBehaviour {/// <summary>float:材質(zhì)變換的速度</summary>[Header("float:材質(zhì)變換的速度")][Tooltip("float:材質(zhì)變換的速度")]public float fSpeed_materials=0.1f;/// <summary>Color:開(kāi)始的顏色</summary>private Color color_from=new Color(0,0,1);/// <summary>Color:結(jié)束的顏色</summary>private Color color_to= new Color(0, 0, 0);/// <summary>Coroutine:協(xié)程句柄</summary>private Coroutine coroutine_glinting;/// <summary> List<Material>:鏈表<材質(zhì)>:需要自發(fā)光的材質(zhì)</summary>List<Material> list_material_needEmission;/// <summary>string:自發(fā)光材質(zhì)屬性的關(guān)鍵字</summary>private readonly string str_keyword = "_EMISSION";/// <summary>string:自發(fā)光材質(zhì)屬性的顏色名字</summary>private readonly string str_colorName = "_EmissionColor";void Start(){//使用鏈表:開(kāi)啟自發(fā)光后更改自發(fā)光屬性,讓其變得虛幻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]);}}//列表材質(zhì)自發(fā)光變換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>/// 開(kāi)始閃爍。/// </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>/// 控制自發(fā)光強(qiáng)度。/// </summary>/// <returns></returns>private IEnumerator IE_glinting(){//材質(zhì)變換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設(shè)置
總結(jié)
以上是生活随笔為你收集整理的unity动态修改标准材质自发光(Emission)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: i78700k配什么显卡好_i7 870
- 下一篇: CDN可以防护什么种类的攻击?