ModelC.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using BeinLab.Util;
  2. using ShadowStudio.Tool;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using XRTool.Util;
  7. public class ModelC : MonoBehaviour
  8. {
  9. private BoxCollider colliderBody;
  10. private Transform modelRoot;
  11. //private NearInterationGrabbable nearInteration;
  12. public Vector3 offset = Vector3.one * 0.1f;
  13. private bool isInit;
  14. private Renderer[] modelRenderers;
  15. /// <summary>
  16. /// 初始化模型的辅助工具
  17. /// 仅在计算模型Bound时使用,用完即销毁
  18. /// </summary>
  19. private GameObject containerHelper;
  20. private GameObject downInfo;
  21. private TextMesh processLabel;
  22. public BoxCollider ColliderBody
  23. {
  24. get
  25. {
  26. if (!colliderBody)
  27. {
  28. colliderBody = UnityUtil.GetBreadthChild<BoxCollider>(transform, "ColliderBody");
  29. }
  30. return colliderBody;
  31. }
  32. }
  33. public Transform ModelRoot
  34. {
  35. get
  36. {
  37. if (!modelRoot)
  38. {
  39. modelRoot = UnityUtil.GetBreadthChild<Transform>(transform, "ModelRoot");
  40. }
  41. return modelRoot;
  42. }
  43. }
  44. public void CalculateRendererBounds()
  45. {
  46. if (ModelRoot.childCount < 1)
  47. {
  48. DownInfo.SetActive(true);
  49. return;
  50. }
  51. DownInfo.SetActive(false);
  52. //transform.DOKill(true);
  53. if (ModelRenderers == null || ModelRenderers.Length < 1)
  54. ModelRenderers = ModelRoot.GetComponentsInChildren<Renderer>();
  55. Vector3 center = Vector3.zero;
  56. Vector3 curScale = transform.localScale;
  57. Vector3 curPos = transform.position;
  58. Quaternion curRot = transform.rotation;
  59. transform.localScale = Vector3.one;
  60. transform.position = Vector3.zero;
  61. transform.rotation = Quaternion.identity;
  62. ModelRoot.localScale = Vector3.one;
  63. ModelRoot.localPosition = Vector3.zero;
  64. ModelRoot.localRotation = Quaternion.identity;
  65. for (int i = 0; i < ModelRenderers.Length; i++)
  66. {
  67. Renderer rend = ModelRenderers[i];
  68. var collider = rend.GetComponent<Collider>();
  69. if (collider)
  70. {
  71. DestroyImmediate(collider);
  72. }
  73. Vector3 tmpCenter = rend.bounds.center;
  74. center += tmpCenter;
  75. }
  76. //ModelRoot.gameObject.SetActive(false);
  77. Vector3 bScale = transform.lossyScale;
  78. Vector3 bPos = center / ModelRenderers.Length - ModelRoot.transform.position;
  79. bPos.x /= bScale.x;
  80. bPos.y /= bScale.y;
  81. bPos.z /= bScale.z;
  82. ModelRoot.transform.localPosition = bPos;
  83. Bounds bounds = new Bounds(ModelRoot.transform.position, Vector3.zero);
  84. for (int i = 0; i < ModelRenderers.Length; i++)
  85. {
  86. bounds.Encapsulate(ModelRenderers[i].bounds);
  87. }
  88. Vector3 bsize = bounds.size;
  89. bsize.x /= bScale.x;
  90. bsize.y /= bScale.y;
  91. bsize.z /= bScale.z;
  92. ColliderBody.size = bsize;
  93. if (!this.GetComponentInChildren<ArtTaskAction>() && !this.GetComponentInChildren<ArtTaskChild>())
  94. {
  95. while (ColliderBody.size.magnitude > (Vector3.one * 0.5f).magnitude)
  96. {
  97. ColliderBody.size /= 2;
  98. ModelRoot.transform.localScale /= 2;
  99. ModelRoot.transform.localPosition /= 2;
  100. // yield return new WaitForFixedUpdate();
  101. }
  102. }
  103. while (ColliderBody.size.magnitude < (Vector3.one * 0.1f).magnitude)
  104. {
  105. ColliderBody.size *= 2;
  106. ModelRoot.transform.localScale *= 2;
  107. ModelRoot.transform.localPosition *= 2;
  108. //yield return new WaitForFixedUpdate();
  109. }
  110. ModelRoot.transform.localPosition = -ModelRoot.transform.localPosition;
  111. ColliderBody.size += offset;
  112. //ModelRoot.gameObject.SetActive(true);
  113. transform.position = curPos;
  114. transform.rotation = curRot;
  115. transform.localScale = curScale;
  116. GetComponent<TransferCell>().RemoveBound();
  117. //PlayEffect();
  118. //TimerMgr.Instance.CreateTimer(() =>
  119. //{
  120. // //yield return new WaitForSeconds(1f);
  121. // //transform.DOKill(true);
  122. // //ToggleComponent(true);
  123. // Transfer.RemoveBound();
  124. // //yield return new WaitForEndOfFrame();
  125. // //Transfer.SetBoundActive(false);
  126. // ResetEditorPosition();
  127. //}, 1.05f);
  128. }
  129. public void AddModel(Transform body)
  130. {
  131. if (ModelRoot)
  132. {
  133. UnityUtil.SetParent(ModelRoot, body);
  134. CalculateRendererBounds();
  135. //body.SetParent(ModelRoot.transform);
  136. //body.transform.localPosition = Vector3.zero;
  137. //body.localRotation = Quaternion.identity;
  138. //body.transform.localScale = Vector3.one;
  139. }
  140. }
  141. public Renderer[] ModelRenderers { get => modelRenderers; set => modelRenderers = value; }
  142. public GameObject DownInfo
  143. {
  144. get
  145. {
  146. if (!downInfo)
  147. {
  148. downInfo = UnityUtil.GetBreadthChild(transform, "DownInfo");
  149. }
  150. return downInfo;
  151. }
  152. }
  153. public TextMesh ProcessLabel
  154. {
  155. get
  156. {
  157. if (!processLabel && DownInfo)
  158. {
  159. processLabel = UnityUtil.GetBreadthChild<TextMesh>(DownInfo.transform, "ProcessLabel");
  160. }
  161. return processLabel;
  162. }
  163. }
  164. }