using Sirenix.OdinInspector; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class MeshCenterSelect : MonoBehaviour { public GameObject modelA; public GameObject modelB; // 模型A的层级树对象 public objTree ObjATree; public objTree ObjBTree; [DictionaryDrawerSettings] public Dictionary objADic; [DictionaryDrawerSettings] public Dictionary objBDic; [Button("测试")] void CreateTree() { ObjATree = new objTree(modelA); ObjBTree = new objTree(modelB); objADic = new Dictionary(); objBDic = new Dictionary(); FindChild(ObjATree, objADic); FindChild(ObjBTree, objBDic); GameObject newFenzumubiao = new GameObject("分组目标"); HierarchicalMatching(ObjATree, newFenzumubiao); modelB.transform.SetParent(newFenzumubiao.transform, newFenzumubiao); modelB.name = "没配对上的"; } void FindChild(objTree child, Dictionary pairs) //收集未分组的mesh { if(child.meshRenderer != null) { if (!pairs.ContainsKey(child.CenterV3)) pairs.Add(child.CenterV3, child); else Debug.LogError(child.nodeObj.name); } // 递归遍历所有子物体 for (int i = 0; i < child.children.Count; i++) FindChild(child.children[i], pairs); } void HierarchicalMatching(objTree tree, GameObject NowPoint) //按层级匹配 { GameObject matchTmpObj; if (tree.meshRenderer != null && objBDic.TryGetValue(tree.CenterV3, out objTree tmpObj)) matchTmpObj = tmpObj.nodeObj; 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 GameObject nodeObj; public List children = new List(); public Vector3 CenterV3; public MeshRenderer meshRenderer; public objTree(GameObject gameObj) { nodeObj = gameObj;//节点 if (nodeObj.GetComponent() != null) { meshRenderer = nodeObj.GetComponent(); Vector3 tmpV3 = meshRenderer.bounds.center; CenterV3 = new Vector3(Mathf.Ceil(tmpV3.x*10000), Mathf.Ceil(tmpV3.y * 10000), Mathf.Ceil(tmpV3.z * 10000)); } addObjNode(); } public void addObjNode() { for (int i = 0; i < nodeObj.transform.childCount; i++) { children.Add(new objTree(nodeObj.transform.GetChild(i).gameObject)); } } } }