|
|
@@ -0,0 +1,88 @@
|
|
|
+using Org.BouncyCastle.Asn1.Ocsp;
|
|
|
+using Sirenix.OdinInspector;
|
|
|
+using System;
|
|
|
+using System.Collections;
|
|
|
+using System.Collections.Generic;
|
|
|
+using UnityEngine;
|
|
|
+
|
|
|
+public class Meshselect : MonoBehaviour
|
|
|
+{
|
|
|
+ public GameObject modelA;
|
|
|
+ public GameObject modelB;
|
|
|
+ //public GameObject fenzumubiao;
|
|
|
+ objTree ObjATree;
|
|
|
+ public Dictionary<string, GameObject> objBDic;
|
|
|
+ [Button("测试")]
|
|
|
+ void CreateTree()
|
|
|
+ {
|
|
|
+ ObjATree = new objTree(modelA);
|
|
|
+ objBDic = new Dictionary<string, GameObject>();
|
|
|
+ FindChild(modelB);
|
|
|
+ GameObject newFenzumubiao = new GameObject("分组目标");
|
|
|
+ HierarchicalMatching(ObjATree, newFenzumubiao);
|
|
|
+ modelB.transform.SetParent(newFenzumubiao.transform);
|
|
|
+ modelB.name = "没配对上的";
|
|
|
+ }
|
|
|
+ void FindChild(GameObject child) //收集未分组的mesh
|
|
|
+ {
|
|
|
+ MeshFilter meshFilter = child.GetComponent<MeshFilter>();
|
|
|
+ if (meshFilter != null && meshFilter.sharedMesh != null)
|
|
|
+ {
|
|
|
+ if (!objBDic.ContainsKey(meshFilter.sharedMesh.name))
|
|
|
+ objBDic.Add(meshFilter.sharedMesh.name, child);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ for (int i = 0; i < child.transform.childCount; i++)
|
|
|
+ FindChild(child.transform.GetChild(i).gameObject);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ void HierarchicalMatching(objTree tree, GameObject NowPoint) //按层级匹配
|
|
|
+ {
|
|
|
+ GameObject matchTmpObj;
|
|
|
+ if (tree.nodeMesh != null && objBDic.TryGetValue(tree.nodeMesh.name, out GameObject tmpObj))
|
|
|
+ matchTmpObj = tmpObj;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ matchTmpObj = new GameObject();
|
|
|
+ matchTmpObj.name = tree.nodeObj.name;
|
|
|
+ }
|
|
|
+ matchTmpObj.transform.SetParent(NowPoint.transform);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ foreach (objTree tmp in tree.children)
|
|
|
+ HierarchicalMatching(tmp, matchTmpObj);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+ [Serializable]
|
|
|
+ public class objTree
|
|
|
+ {
|
|
|
+ public Mesh nodeMesh;
|
|
|
+ public GameObject nodeObj;
|
|
|
+ public List<objTree> children = new List<objTree>();
|
|
|
+ public objTree(GameObject gameObj)
|
|
|
+ {
|
|
|
+ nodeObj = gameObj;//节点
|
|
|
+
|
|
|
+ MeshFilter meshFilter = nodeObj.GetComponent<MeshFilter>();
|
|
|
+ if (meshFilter != null && meshFilter.sharedMesh != null)
|
|
|
+ nodeMesh = nodeObj.GetComponent<MeshFilter>().sharedMesh;
|
|
|
+
|
|
|
+ addObjNode();
|
|
|
+ }
|
|
|
+ public void addObjNode()
|
|
|
+ {
|
|
|
+ for (int i = 0; i < nodeObj.transform.childCount; i++)
|
|
|
+ {
|
|
|
+ children.Add(new objTree(nodeObj.transform.GetChild(i).gameObject));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|