MeshCenterSelect.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Sirenix.OdinInspector;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class MeshCenterSelect : MonoBehaviour
  7. {
  8. public GameObject modelA;
  9. public GameObject modelB;
  10. // 模型A的层级树对象
  11. public objTree ObjATree;
  12. public objTree ObjBTree;
  13. [DictionaryDrawerSettings]
  14. public Dictionary<Vector3, objTree> objADic;
  15. [DictionaryDrawerSettings]
  16. public Dictionary<Vector3, objTree> objBDic;
  17. [Button("测试")]
  18. void CreateTree()
  19. {
  20. ObjATree = new objTree(modelA);
  21. ObjBTree = new objTree(modelB);
  22. objADic = new Dictionary<Vector3, objTree>();
  23. objBDic = new Dictionary<Vector3, objTree>();
  24. FindChild(ObjATree, objADic);
  25. FindChild(ObjBTree, objBDic);
  26. GameObject newFenzumubiao = new GameObject("分组目标");
  27. HierarchicalMatching(ObjATree, newFenzumubiao);
  28. modelB.transform.SetParent(newFenzumubiao.transform, newFenzumubiao);
  29. modelB.name = "没配对上的";
  30. }
  31. void FindChild(objTree child, Dictionary<Vector3, objTree> pairs) //收集未分组的mesh
  32. {
  33. if(child.meshRenderer != null)
  34. {
  35. if (!pairs.ContainsKey(child.CenterV3))
  36. pairs.Add(child.CenterV3, child);
  37. else Debug.LogError(child.nodeObj.name);
  38. }
  39. // 递归遍历所有子物体
  40. for (int i = 0; i < child.children.Count; i++)
  41. FindChild(child.children[i], pairs);
  42. }
  43. void HierarchicalMatching(objTree tree, GameObject NowPoint) //按层级匹配
  44. {
  45. GameObject matchTmpObj;
  46. if (tree.meshRenderer != null && objBDic.TryGetValue(tree.CenterV3, out objTree tmpObj))
  47. matchTmpObj = tmpObj.nodeObj;
  48. else
  49. {
  50. matchTmpObj = new GameObject();
  51. matchTmpObj.name = tree.nodeObj.name;
  52. }
  53. matchTmpObj.transform.SetParent(NowPoint.transform);
  54. foreach (objTree tmp in tree.children)
  55. HierarchicalMatching(tmp, matchTmpObj);
  56. }
  57. [Serializable]
  58. public class objTree
  59. {
  60. public GameObject nodeObj;
  61. public List<objTree> children = new List<objTree>();
  62. public Vector3 CenterV3;
  63. public MeshRenderer meshRenderer;
  64. public objTree(GameObject gameObj)
  65. {
  66. nodeObj = gameObj;//节点
  67. if (nodeObj.GetComponent<MeshRenderer>() != null)
  68. {
  69. meshRenderer = nodeObj.GetComponent<MeshRenderer>();
  70. Vector3 tmpV3 = meshRenderer.bounds.center;
  71. CenterV3 = new Vector3(Mathf.Ceil(tmpV3.x*10000), Mathf.Ceil(tmpV3.y * 10000), Mathf.Ceil(tmpV3.z * 10000));
  72. }
  73. addObjNode();
  74. }
  75. public void addObjNode()
  76. {
  77. for (int i = 0; i < nodeObj.transform.childCount; i++)
  78. {
  79. children.Add(new objTree(nodeObj.transform.GetChild(i).gameObject));
  80. }
  81. }
  82. }
  83. }