using BeinLab.Util; using ShadowStudio.Tool; using System.Collections; using System.Collections.Generic; using UnityEngine; using XRTool.Util; public class ModelC : MonoBehaviour { private BoxCollider colliderBody; private Transform modelRoot; //private NearInterationGrabbable nearInteration; public Vector3 offset = Vector3.one * 0.1f; private bool isInit; private Renderer[] modelRenderers; /// /// 初始化模型的辅助工具 /// 仅在计算模型Bound时使用,用完即销毁 /// private GameObject containerHelper; private GameObject downInfo; private TextMesh processLabel; public BoxCollider ColliderBody { get { if (!colliderBody) { colliderBody = UnityUtil.GetBreadthChild(transform, "ColliderBody"); } return colliderBody; } } public Transform ModelRoot { get { if (!modelRoot) { modelRoot = UnityUtil.GetBreadthChild(transform, "ModelRoot"); } return modelRoot; } } public void CalculateRendererBounds() { if (ModelRoot.childCount < 1) { DownInfo.SetActive(true); return; } DownInfo.SetActive(false); //transform.DOKill(true); if (ModelRenderers == null || ModelRenderers.Length < 1) ModelRenderers = ModelRoot.GetComponentsInChildren(); Vector3 center = Vector3.zero; Vector3 curScale = transform.localScale; Vector3 curPos = transform.position; Quaternion curRot = transform.rotation; transform.localScale = Vector3.one; transform.position = Vector3.zero; transform.rotation = Quaternion.identity; ModelRoot.localScale = Vector3.one; ModelRoot.localPosition = Vector3.zero; ModelRoot.localRotation = Quaternion.identity; for (int i = 0; i < ModelRenderers.Length; i++) { Renderer rend = ModelRenderers[i]; var collider = rend.GetComponent(); if (collider) { DestroyImmediate(collider); } Vector3 tmpCenter = rend.bounds.center; center += tmpCenter; } //ModelRoot.gameObject.SetActive(false); Vector3 bScale = transform.lossyScale; Vector3 bPos = center / ModelRenderers.Length - ModelRoot.transform.position; bPos.x /= bScale.x; bPos.y /= bScale.y; bPos.z /= bScale.z; ModelRoot.transform.localPosition = bPos; Bounds bounds = new Bounds(ModelRoot.transform.position, Vector3.zero); for (int i = 0; i < ModelRenderers.Length; i++) { bounds.Encapsulate(ModelRenderers[i].bounds); } Vector3 bsize = bounds.size; bsize.x /= bScale.x; bsize.y /= bScale.y; bsize.z /= bScale.z; ColliderBody.size = bsize; if (!this.GetComponentInChildren() && !this.GetComponentInChildren()) { while (ColliderBody.size.magnitude > (Vector3.one * 0.5f).magnitude) { ColliderBody.size /= 2; ModelRoot.transform.localScale /= 2; ModelRoot.transform.localPosition /= 2; // yield return new WaitForFixedUpdate(); } } while (ColliderBody.size.magnitude < (Vector3.one * 0.1f).magnitude) { ColliderBody.size *= 2; ModelRoot.transform.localScale *= 2; ModelRoot.transform.localPosition *= 2; //yield return new WaitForFixedUpdate(); } ModelRoot.transform.localPosition = -ModelRoot.transform.localPosition; ColliderBody.size += offset; //ModelRoot.gameObject.SetActive(true); transform.position = curPos; transform.rotation = curRot; transform.localScale = curScale; GetComponent().RemoveBound(); //PlayEffect(); //TimerMgr.Instance.CreateTimer(() => //{ // //yield return new WaitForSeconds(1f); // //transform.DOKill(true); // //ToggleComponent(true); // Transfer.RemoveBound(); // //yield return new WaitForEndOfFrame(); // //Transfer.SetBoundActive(false); // ResetEditorPosition(); //}, 1.05f); } public void AddModel(Transform body) { if (ModelRoot) { UnityUtil.SetParent(ModelRoot, body); CalculateRendererBounds(); //body.SetParent(ModelRoot.transform); //body.transform.localPosition = Vector3.zero; //body.localRotation = Quaternion.identity; //body.transform.localScale = Vector3.one; } } public Renderer[] ModelRenderers { get => modelRenderers; set => modelRenderers = value; } public GameObject DownInfo { get { if (!downInfo) { downInfo = UnityUtil.GetBreadthChild(transform, "DownInfo"); } return downInfo; } } public TextMesh ProcessLabel { get { if (!processLabel && DownInfo) { processLabel = UnityUtil.GetBreadthChild(DownInfo.transform, "ProcessLabel"); } return processLabel; } } }