Unity 动态设置Material Emission属性

时间:2025-02-21 13:25:05
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; public class MaterialEditorTool : EditorWindow { [MenuItem("Tools/SetEmissionAttribute")] static void SetMaterialEmissionAttribute() { //创建窗口 Rect wr = new Rect(500, 500, 500, 500); MaterialEditorTool window = (MaterialEditorTool)EditorWindow.GetWindowWithRect(typeof(MaterialEditorTool), wr, false, "MaterialEditorTool"); window.Show(); } private void OnGUI() { if (GUILayout.Button("Set Material Emission Attribute", GUILayout.ExpandWidth(true))) { Debug.Log("set..."); SetEmissionMethod(); } } public Transform root; public void SetEmissionMethod() { Object[] targetObj = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);//得到选中的文件,包括选中文件夹的子文件和子文件夹 if (targetObj != null && targetObj.Length > 0) { for (int i = 0; i < targetObj.Length; i++) { if (targetObj[i] is Material) { //string path = (targetObj[i]); //得到资源的路径 //(path); Material material = targetObj[i] as Material; material.EnableKeyword("_Emission"); material.SetColor("_EmissionColor", Color.white); Texture texture = material.GetTexture("_MainTex"); material.SetTexture("_EmissionMap", texture); Debug.Log(texture); } } } } }