| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 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));
- }
- }
- }
- }
|