当物体有光影贴图时,给他自发光,且数量比较大
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class SetSelfLightmap : Editor
{
//层级窗口(Hierarchy)右键菜单扩展
[MenuItem("GameObject/SetMap", false, 10)]
static void Init()
{
GameObject[] Prefabs = Selection.gameObjects;
for (int i = 0; i < Prefabs.Length; i++)
{
Transform[] m_childTransformArray = Prefabs[i].GetComponentsInChildren<Transform>();
if (m_childTransformArray.Length> 0)
{
for (int j = 0; j < m_childTransformArray.Length; j++)
{
ChangeObjectMaterialEmission(m_childTransformArray[j].gameObject);
}
}
else
{
ChangeObjectMaterialEmission(Prefabs[i]);
}
}
}
static void ChangeObjectMaterialEmission( GameObject m_selectObject)
{
if (m_selectObject.GetComponent<MeshRenderer>())
{
Material[] selfMaterial = m_selectObject.GetComponent<MeshRenderer>().sharedMaterials;
if (selfMaterial.Length > 1)
{
for (int k = 0; k < selfMaterial.Length; k++)
{
Texture m_mainTex = selfMaterial[k].mainTexture;
Debug.Log(m_mainTex.name+"+++++");
selfMaterial[k].EnableKeyword("_EMISSION");
// selfMaterial[0].IsKeywordEnabled("_EMISSION") = true;
// Debug.Log(selfMaterial[k].IsKeywordEnabled("_EMISSION"));
selfMaterial[k].SetTexture("_EmissionMap", m_mainTex);
selfMaterial[k].SetColor("_EmissionColor", Color.white * 0.75f);
selfMaterial[k].SetFloat("_EMISSION", 0.5f);
m_selectObject.GetComponent<MeshRenderer>().sharedMaterials[k] = selfMaterial[k];
}
}
else
{
Texture m_mainTex = selfMaterial[0].mainTexture;
selfMaterial[0].EnableKeyword("_EMISSION");
// selfMaterial[0].IsKeywordEnabled("_EMISSION") = true;
//Debug.Log(selfMaterial[0].IsKeywordEnabled("_EMISSION"));
selfMaterial[0].SetTexture("_EmissionMap", m_mainTex);
selfMaterial[0].SetColor("_EmissionColor", Color.white*0.75f);
selfMaterial[0].SetFloat("_EMISSION", 0.5f);
m_selectObject.GetComponent<MeshRenderer>().sharedMaterials[0] = selfMaterial[0];
}
}
}
override public void OnInspectorGUI()
{
//更新保存数据
serializedObject.Update();
//刷新修改的值
serializedObject.ApplyModifiedProperties();
}
}
|