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 objBDic; [Button("测试")] void CreateTree() { ObjATree = new objTree(modelA); objBDic = new Dictionary(); 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(); 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 children = new List(); public objTree(GameObject gameObj) { nodeObj = gameObj;//节点 MeshFilter meshFilter = nodeObj.GetComponent(); if (meshFilter != null && meshFilter.sharedMesh != null) nodeMesh = nodeObj.GetComponent().sharedMesh; addObjNode(); } public void addObjNode() { for (int i = 0; i < nodeObj.transform.childCount; i++) { children.Add(new objTree(nodeObj.transform.GetChild(i).gameObject)); } } } }