327 lines
8.9 KiB
C#
327 lines
8.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
[CustomEditor(typeof(RebuildMesh))]
|
|
public class RebuildMeshEditor : Editor
|
|
{
|
|
RebuildMesh inst;
|
|
|
|
private void OnEnable()
|
|
{
|
|
inst = (RebuildMesh)target;
|
|
}
|
|
|
|
public override void OnInspectorGUI()
|
|
{
|
|
base.OnInspectorGUI();
|
|
|
|
//
|
|
if (GUILayout.Button("DestroyByNameMesh", GUILayout.Height(50)))
|
|
{
|
|
DestroyByNameMesh();
|
|
}
|
|
|
|
if (GUILayout.Button("ReNameMesh", GUILayout.Height(50)))
|
|
{
|
|
ReNameMesh();
|
|
}
|
|
|
|
if (GUILayout.Button("GetMeshRef", GUILayout.Height(50)))
|
|
{
|
|
//GetMeshRef();
|
|
}
|
|
|
|
//
|
|
if (GUILayout.Button("ReBuildMesh", GUILayout.Height(50)))
|
|
{
|
|
ReBuildMeshs();
|
|
|
|
}
|
|
|
|
//
|
|
if (GUILayout.Button("SetRefMesh", GUILayout.Height(50)))
|
|
{
|
|
//SetRefMesh();
|
|
}
|
|
|
|
if (GUILayout.Button("DetachRefMesh", GUILayout.Height(50)))
|
|
{
|
|
DetachRefMesh();
|
|
}
|
|
|
|
if (GUILayout.Button("SetRefMeshEditorble", GUILayout.Height(50)))
|
|
{
|
|
SetRefMeshEditorble();
|
|
}
|
|
|
|
if (GUILayout.Button("SetMeshLightMapScale", GUILayout.Height(50)))
|
|
{
|
|
SetMeshLightMapScale();
|
|
}
|
|
|
|
if (GUILayout.Button("GeneraterLightMapingUVs", GUILayout.Height(50)))
|
|
{
|
|
GeneraterLightMapingUVs();
|
|
}
|
|
|
|
if (GUILayout.Button("CloseShadow", GUILayout.Height(50)))
|
|
{
|
|
CloseShadow();
|
|
}
|
|
|
|
GUILayout.BeginHorizontal();
|
|
if (GUILayout.Button("SetToBake", GUILayout.Height(50)))
|
|
{
|
|
SetToBake();
|
|
}
|
|
|
|
if (GUILayout.Button("SetToRunTime", GUILayout.Height(50)))
|
|
{
|
|
SetToRunTime();
|
|
}
|
|
GUILayout.EndHorizontal();
|
|
}
|
|
|
|
void ReNameMesh()
|
|
{
|
|
int index = 0;
|
|
foreach (Transform t in inst.root.GetComponentsInChildren<Transform>())
|
|
{
|
|
if (t != inst.root && t.gameObject.activeSelf && t.GetComponent<MeshFilter>())
|
|
{
|
|
MeshFilter meshFilter = t.GetComponent<MeshFilter>();
|
|
t.name = meshFilter.sharedMesh.name + "_" + index.ToString();
|
|
++index;
|
|
}
|
|
}
|
|
}
|
|
|
|
void DestroyByNameMesh()
|
|
{
|
|
foreach (Transform t in inst.root.GetComponentsInChildren<Transform>())
|
|
{
|
|
if (t != inst.root && t.gameObject.activeSelf && t.GetComponent<MeshFilter>())
|
|
{
|
|
if(t.name.Contains(inst.meshRef.name))
|
|
{
|
|
DestroyImmediate(t.gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void GetMeshRef()
|
|
{
|
|
inst.meshRefList.Clear();
|
|
int index = 0;
|
|
foreach (Transform t in inst.root.GetComponentsInChildren<Transform>())
|
|
{
|
|
if (t != inst.root && t.gameObject.activeSelf && t.GetComponent<MeshFilter>())
|
|
{
|
|
MeshFilter meshFilter = t.GetComponent<MeshFilter>();
|
|
Mesh mesh = Instantiate(meshFilter.sharedMesh);
|
|
|
|
char[] names = inst.root.name.ToCharArray();
|
|
string name = "";
|
|
for (int i = 0; i < names.Length; ++i)
|
|
{
|
|
if(i == 0)
|
|
{
|
|
name += names[0].ToString().ToLower();
|
|
}
|
|
else
|
|
{
|
|
name += names[i].ToString();
|
|
}
|
|
}
|
|
|
|
mesh.name = name + "_" + index.ToString();
|
|
|
|
inst.meshRefList.Add(mesh);
|
|
++index;
|
|
}
|
|
}
|
|
}
|
|
|
|
void ReBuildMesh()
|
|
{
|
|
foreach (Mesh m in inst.meshRefList)
|
|
{
|
|
AssetDatabase.CreateAsset(m, "Assets/CartoonOriental/Scene/Meshs/Meshs/" + m.name + ".mesh");
|
|
}
|
|
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
}
|
|
|
|
void ReBuildMeshs()
|
|
{
|
|
foreach (Transform t in inst.root.GetComponentsInChildren<Transform>())
|
|
{
|
|
if (t != inst.root && t.gameObject.activeSelf && t.GetComponent<MeshFilter>())
|
|
{
|
|
MeshFilter meshFilter = t.GetComponent<MeshFilter>();
|
|
|
|
if(meshFilter.sharedMesh == inst.meshRef)
|
|
{
|
|
meshFilter.sharedMesh = inst.meshRes;
|
|
}
|
|
}
|
|
}
|
|
|
|
Debug.Log("Done!");
|
|
}
|
|
|
|
void ReBuildMeshss()
|
|
{
|
|
foreach (Transform t in inst.root.GetComponentsInChildren<Transform>())
|
|
{
|
|
if (t != inst.root && t.gameObject.activeSelf && t.GetComponent<MeshFilter>())
|
|
{
|
|
MeshFilter meshFilter = t.GetComponent<MeshFilter>();
|
|
|
|
if (meshFilter.sharedMesh.GetTriangles(0).Length == inst.meshRef.GetTriangles(0).Length)
|
|
{
|
|
meshFilter.sharedMesh = inst.meshRef;
|
|
}
|
|
}
|
|
}
|
|
|
|
Debug.Log("Done!");
|
|
}
|
|
|
|
|
|
void SetRefMesh()
|
|
{
|
|
int index = 0;
|
|
foreach (Transform t in inst.root.GetComponentsInChildren<Transform>())
|
|
{
|
|
if (t != inst.root && t.gameObject.activeSelf && t.GetComponent<MeshFilter>())
|
|
{
|
|
MeshFilter meshFilter = t.GetComponent<MeshFilter>();
|
|
meshFilter.sharedMesh = inst.meshRefList[index];
|
|
|
|
++index;
|
|
}
|
|
}
|
|
}
|
|
|
|
void DetachRefMesh()
|
|
{
|
|
int index = 0;
|
|
foreach (Transform t in inst.root.GetComponentsInChildren<Transform>())
|
|
{
|
|
if (t != inst.root && t.gameObject.activeSelf && t.GetComponent<MeshFilter>())
|
|
{
|
|
t.SetParent(inst.root);
|
|
|
|
++index;
|
|
}
|
|
}
|
|
}
|
|
|
|
void SetRefMeshEditorble()
|
|
{
|
|
MeshFilter meshFilter = inst.root.GetComponent<MeshFilter>();
|
|
Vector3[] verts = meshFilter.sharedMesh.vertices;
|
|
int[] tris = meshFilter.sharedMesh.triangles;
|
|
Vector3[] nors = meshFilter.sharedMesh.normals;
|
|
Vector4[] tans = meshFilter.sharedMesh.tangents;
|
|
Vector2[] uvs0 = meshFilter.sharedMesh.uv;
|
|
Vector2[] uvs1 = meshFilter.sharedMesh.uv2;
|
|
Vector2[] uvs2 = meshFilter.sharedMesh.uv3;
|
|
Color[] cols = meshFilter.sharedMesh.colors;
|
|
|
|
Mesh mesh = new Mesh();
|
|
mesh.SetVertices(verts);
|
|
mesh.SetTriangles(tris, 0);
|
|
mesh.SetNormals(nors);
|
|
mesh.SetTangents(tans);
|
|
mesh.SetUVs(0, uvs0);
|
|
mesh.SetUVs(1, uvs1);
|
|
mesh.SetUVs(2, uvs2);
|
|
mesh.SetColors(cols);
|
|
mesh.RecalculateBounds();
|
|
|
|
AssetDatabase.CreateAsset(mesh, "Assets/CartoonOriental/Scene/Meshs/Others/" + meshFilter.sharedMesh.name + "_Copy.mesh");
|
|
|
|
meshFilter.sharedMesh = mesh;
|
|
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
}
|
|
|
|
void SetMeshLightMapScale()
|
|
{
|
|
foreach (Transform t in inst.root.GetComponentsInChildren<Transform>())
|
|
{
|
|
if (t != inst.root && t.gameObject.activeSelf && t.GetComponent<MeshRenderer>())
|
|
{
|
|
t.GetComponent<MeshRenderer>().scaleInLightmap = inst.lightMapScale;
|
|
}
|
|
}
|
|
}
|
|
|
|
void GeneraterLightMapingUVs()
|
|
{
|
|
/*foreach (Transform t in inst.root.GetComponentsInChildren<Transform>())
|
|
{
|
|
if (t != inst.root && t.gameObject.activeSelf && t.GetComponent<MeshRenderer>())
|
|
{
|
|
Unwrapping.GenerateSecondaryUVSet(t.GetComponent<MeshFilter>().sharedMesh);
|
|
}
|
|
}*/
|
|
|
|
GameObject obj = Selection.activeGameObject;
|
|
bool sss = Unwrapping.GenerateSecondaryUVSet(obj.GetComponent<MeshFilter>().sharedMesh);
|
|
Debug.Log(sss);
|
|
}
|
|
|
|
void CloseShadow()
|
|
{
|
|
foreach (Transform t in inst.root.GetComponentsInChildren<Transform>())
|
|
{
|
|
if (t != inst.root && t.gameObject.activeSelf && t.GetComponent<MeshRenderer>())
|
|
{
|
|
t.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
|
}
|
|
}
|
|
}
|
|
|
|
void SetToBake()
|
|
{
|
|
foreach (Transform t in inst.root.GetComponentsInChildren<Transform>())
|
|
{
|
|
if(t.name == "Plants")
|
|
{
|
|
foreach (Transform tt in t.GetComponentsInChildren<Transform>())
|
|
{
|
|
tt.gameObject.isStatic = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
RenderSettings.ambientIntensity = 1.0f;
|
|
}
|
|
|
|
void SetToRunTime()
|
|
{
|
|
foreach (Transform t in inst.root.GetComponentsInChildren<Transform>())
|
|
{
|
|
if (t.name == "Plants")
|
|
{
|
|
foreach (Transform tt in t.GetComponentsInChildren<Transform>())
|
|
{
|
|
tt.gameObject.isStatic = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
RenderSettings.ambientIntensity = 1.5f;
|
|
}
|
|
}
|