111
This commit is contained in:
21
Assets/ThirdParty/Tools/CombineMeshs/CombineMeshs.cs
vendored
Normal file
21
Assets/ThirdParty/Tools/CombineMeshs/CombineMeshs.cs
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CombineMeshs : MonoBehaviour
|
||||
{
|
||||
public Transform root;
|
||||
public bool isCombineSubmesh;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/ThirdParty/Tools/CombineMeshs/CombineMeshs.cs.meta
vendored
Normal file
11
Assets/ThirdParty/Tools/CombineMeshs/CombineMeshs.cs.meta
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f74cd4f52d2aa54e86313a2f7dcb745
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/ThirdParty/Tools/CombineMeshs/Editor.meta
vendored
Normal file
8
Assets/ThirdParty/Tools/CombineMeshs/Editor.meta
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6dc3c1a68240cfc43b19ccfe2a4f496c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
116
Assets/ThirdParty/Tools/CombineMeshs/Editor/CombineMeshsEditor.cs
vendored
Normal file
116
Assets/ThirdParty/Tools/CombineMeshs/Editor/CombineMeshsEditor.cs
vendored
Normal file
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
[CustomEditor(typeof(CombineMeshs))]
|
||||
public class CombineMeshsEditor : Editor
|
||||
{
|
||||
CombineMeshs inst;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
inst = (CombineMeshs)target;
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
base.OnInspectorGUI();
|
||||
|
||||
//
|
||||
if(GUILayout.Button("CombineMeshs", GUILayout.Height(50)))
|
||||
{
|
||||
if(!inst.isCombineSubmesh)
|
||||
{
|
||||
AAA();
|
||||
}
|
||||
else
|
||||
{
|
||||
AAB();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AAA()
|
||||
{
|
||||
List< CombineInstance > combineInstances = new List< CombineInstance >();
|
||||
List<Material> materials = new List< Material >();
|
||||
foreach(Transform t in inst.root.GetComponent<Transform>())
|
||||
{
|
||||
if(t.gameObject.activeSelf && t.GetComponent<MeshFilter>())
|
||||
{
|
||||
CombineInstance combineInstance = new CombineInstance();
|
||||
|
||||
combineInstance.mesh = t.GetComponent<MeshFilter>().sharedMesh;
|
||||
combineInstance.transform = inst.root.worldToLocalMatrix * t.localToWorldMatrix;
|
||||
combineInstances.Add(combineInstance);
|
||||
|
||||
materials.AddRange(t.GetComponent<MeshRenderer>().sharedMaterials);
|
||||
|
||||
t.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
Mesh mesh = new Mesh();
|
||||
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
|
||||
mesh.CombineMeshes(combineInstances.ToArray(), false);
|
||||
|
||||
GameObject obj = new GameObject();
|
||||
obj.transform.position = inst.root.transform.position;
|
||||
obj.transform.SetParent(inst.root.transform);
|
||||
obj.AddComponent<MeshFilter>();
|
||||
obj.AddComponent<MeshRenderer>();
|
||||
|
||||
obj.GetComponent<MeshFilter>().sharedMesh = mesh;
|
||||
obj.GetComponent<MeshRenderer>().sharedMaterials = materials.ToArray();
|
||||
}
|
||||
|
||||
void AAB()
|
||||
{
|
||||
List<CombineInstance> combineInstances = new List<CombineInstance>();
|
||||
List<Material> materials = new List<Material>();
|
||||
foreach (Transform t in inst.root.GetComponentsInChildren<Transform>())
|
||||
{
|
||||
if (t.gameObject.activeSelf && t.GetComponent<MeshFilter>() && t.GetComponent<MeshFilter>().sharedMesh)
|
||||
{
|
||||
CombineInstance combineInstance = new CombineInstance();
|
||||
|
||||
for (int i = 0; i < t.GetComponent<MeshFilter>().sharedMesh.subMeshCount; ++i)
|
||||
{
|
||||
combineInstance.mesh = t.GetComponent<MeshFilter>().sharedMesh;
|
||||
combineInstance.transform = inst.root.worldToLocalMatrix * t.localToWorldMatrix;
|
||||
combineInstance.subMeshIndex = i;
|
||||
combineInstances.Add(combineInstance);
|
||||
}
|
||||
|
||||
//
|
||||
for (int i = 0; i < t.GetComponent<MeshRenderer>().sharedMaterials.Length; ++i)
|
||||
{
|
||||
if (!materials.Contains(t.GetComponent<MeshRenderer>().sharedMaterials[i]))
|
||||
{
|
||||
materials.Add(t.GetComponent<MeshRenderer>().sharedMaterials[i]);
|
||||
}
|
||||
}
|
||||
|
||||
t.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
Mesh mesh = new Mesh();
|
||||
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
|
||||
mesh.CombineMeshes(combineInstances.ToArray(), true);
|
||||
|
||||
GameObject obj = new GameObject();
|
||||
obj.transform.position = inst.root.transform.position;
|
||||
obj.transform.SetParent(inst.root.transform);
|
||||
obj.AddComponent<MeshFilter>();
|
||||
obj.AddComponent<MeshRenderer>();
|
||||
|
||||
obj.GetComponent<MeshFilter>().sharedMesh = mesh;
|
||||
obj.GetComponent<MeshRenderer>().sharedMaterials = materials.ToArray();
|
||||
|
||||
Debug.Log("Done!");
|
||||
}
|
||||
}
|
||||
11
Assets/ThirdParty/Tools/CombineMeshs/Editor/CombineMeshsEditor.cs.meta
vendored
Normal file
11
Assets/ThirdParty/Tools/CombineMeshs/Editor/CombineMeshsEditor.cs.meta
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6bea1a3d6d78bce4f893b2ceac8d2405
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user