using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// 用于批量替换 模型的lod 按名字相同替换
/// </summary>
[ExecuteInEditMode]
public class LodsEditorWindow : EditorWindow
{
[SerializeField]
public LODGroup Lods;
[SerializeField]
public List<LODGroup> LodsList = new List<LODGroup>();
public List<Renderer> MeshRendererList = new List<Renderer>();
public SerializedProperty _Lods;
public SerializedProperty _LodsList;
public SerializedProperty _MeshRendererList;
public SerializedObject LodsHelper;
void OnEnable()
{
LodsHelper = new SerializedObject(this);
_Lods = LodsHelper.FindProperty("Lods");
_LodsList = LodsHelper.FindProperty("LodsList");
_MeshRendererList = LodsHelper.FindProperty("MeshRendererList");
}
[MenuItem("Window/QL/Lod批量修改器")]
static void AddWindow()
{
LodsEditorWindow window = (LodsEditorWindow)EditorWindow.GetWindow(typeof(LodsEditorWindow), false, "Lod批量修改器");
window.Show();
}
private string WaringText = "";
void OnGUI()
{
GUILayout.Label("第一步: 在一个组件上添加 LODGroup 组件 并拖拽到 模板LODs 窗口");
GUILayout.Label("第二步: 按住shift 选择所有需要添加lod 的游戏对象 ");
GUILayout.Label("第三步: 点击复制lod组件");
GUILayout.Label("第四步: 将 lod 模型 拖拽到 Hierarchy 面板 按ctr + D 复制出来一份 (为了避免 没有实例化的麻烦)");
GUILayout.Label("第五步: 按住 Shift 选中所有 实例化的 lod mesh 点击添加Mesh ");
GUILayout.Label("第六步: 替换lod 注意 这里使用的是同名的替换 不同名字的手动替换就好了");
EditorGUILayout.LabelField(WaringText);
LodsHelper.Update();
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(_Lods, new GUIContent("模板LODs"), true);
EditorGUILayout.PropertyField(_LodsList, new GUIContent("需要修改的LODs列表"), true);
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("添加列表", GUILayout.Width(150)))
{
for (int i = 0; i < Selection.gameObjects.Length; i++)
{
if (Selection.gameObjects[i].GetComponent<LODGroup>()==null)
{
Selection.gameObjects[i].AddComponent<LODGroup>();
}
LodsList.Add(Selection.gameObjects[i].GetComponent<LODGroup>());
}
}
EditorGUILayout.Space();
if (GUILayout.Button("清除列表", GUILayout.Width(150)))
{
LodsList.Clear();
}
EditorGUILayout.EndHorizontal();
if (GUILayout.Button("复制LOD组件", GUILayout.Width(200)))
{
if (Lods)
{
string[,] LodName = new string[10, 10];
for (int i = 0; i < Lods.GetLODs().Length; i++)
{
for (int j = 0; j < Lods.GetLODs()[i].renderers.Length; j++)
{
LodName[i, j] = Lods.GetLODs()[i].renderers[j].name;
}
}
for (int i = 0; i < LodsList.Count; i++)
{
LOD[] LodGroup = new LOD[Lods.GetLODs().Length];
for (int j = 0; j < Lods.GetLODs().Length; j++)
{
Renderer[] render = new Renderer[Lods.GetLODs()[j].renderers.Length];
for (int n = 0; n < Lods.GetLODs()[j].renderers.Length; n++)
{
foreach (var t in LodsList[i].gameObject.GetComponentsInChildren<Renderer>())
{
if (t.name == LodName[j, n])
{
render[n] = t;
}
}
}
LodGroup[j] = new LOD(Lods.GetLODs()[j].screenRelativeTransitionHeight, render);
}
LodsList[i].SetLODs(LodGroup);
}
}
}
EditorGUILayout.PropertyField(_MeshRendererList, new GUIContent("添加的Mesh列表"), true);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("添加Mesh", GUILayout.Width(150)))
{
for (int i = 0; i < Selection.gameObjects.Length; i++)
{
MeshRendererList.Add(Selection.gameObjects[i].GetComponent<Renderer>());
}
}
EditorGUILayout.Space();
if (GUILayout.Button("清除列表", GUILayout.Width(150)))
{
MeshRendererList.Clear();
}
EditorGUILayout.EndHorizontal();
if (GUILayout.Button("替换lod", GUILayout.Width(150)))
{
for (int i = 0; i < LodsList.Count; i++)
{
for (int j = 0; j < MeshRendererList.Count; j++)
{
if (LodsList[i].gameObject.name== MeshRendererList[j].gameObject.name)
{
// LOD[] lODs = LodsList[i].GetLODs();
LOD[] oD = new LOD[2];
Renderer[] renderers = new Renderer[1];
renderers[0] = LodsList[i].GetComponent<Renderer>();
oD[0] = new LOD(0.05f,renderers);
Renderer[] renderers2 = new Renderer[1];
renderers2[0] = MeshRendererList[j].GetComponent<Renderer>();
oD[1] = new LOD(0.01f, renderers2);
LodsList[i].SetLODs(oD);
Transform L = LodsList[i].transform;
Transform M = MeshRendererList[j].transform;
M.SetParent(L);
}
}
}
}
if (EditorGUI.EndChangeCheck())
{//提交修改
LodsHelper.ApplyModifiedProperties();
_Lods = LodsHelper.FindProperty("Lods");
_LodsList = LodsHelper.FindProperty("LodsList");
_MeshRendererList = LodsHelper.FindProperty("MeshRendererList");
}
}
[MenuItem("LSJ/setLod")]
public static void setLodDistance()
{
GameObject[] gameObjects = Selection.gameObjects;
for (int i = 0; i < gameObjects.Length; i++)
{
LODGroup lODGroup = gameObjects[i].GetComponent<LODGroup>();
if (lODGroup != null)
{
LOD[] lODs = lODGroup.GetLODs();
if (lODs.Length == 2)
{
lODs[0].screenRelativeTransitionHeight = 0.3f;
lODs[1].screenRelativeTransitionHeight = 0.01f;
lODGroup.SetLODs(lODs);
}
}
}
}
void OnInspectorUpdate()
{
this.Repaint();
}
//[MenuItem("Test/test")]
//static void test1()
//{
// GameObject parentPrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Editor/Prefabs/parent.prefab");
// GameObject parent = Instantiate(parentPrefab);
// GameObject childPrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Editor/Prefabs/child.prefab");
// GameObject child = Instantiate(childPrefab);
// child.transform.parent = parent.transform;
// PrefabUtility.SaveAsPrefabAsset(parent, "Assets/Editor/Prefabs/parent.prefab");
// DestroyImmediate(parent);
//}
}
网友评论