123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- using DG.Tweening;
- using PublicTools.Unity;
- using SC.XR.Unity.Module_InputSystem;
- using ShadowStudio.Tool;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using XRTool.Util;
- namespace ShadowStudio.Model
- {
- /// <summary>
- /// 模型容器
- /// 一个简易模型或者其他的模型资源的容器
- /// 自动计算包围盒,大小位置中心等等,支持模型打组编辑(多个模型合为一个模型进行编辑)
- /// 拖拽移动缩放组件
- /// </summary>
- public class ModelContainer : GoodsContainer
- {
- public GameObject ChooseKuang;
- private BoxCollider colliderBody;
- private Transform modelRoot;
- //private NearInterationGrabbable nearInteration;
- public Vector3 offset = Vector3.one * 0.1f;
- private bool isInit;
- private Renderer[] modelRenderers;
- /// <summary>
- /// 初始化模型的辅助工具
- /// 仅在计算模型Bound时使用,用完即销毁
- /// </summary>
- private GameObject containerHelper;
- private GameObject downInfo;
- private TextMesh processLabel;
- public override void Start()
- {
- base.Start();
- CalculateRendererBounds();
- TimerMgr.Instance.CreateTimer(
- PlayEffect, 0);
- }
- public BoxCollider ColliderBody
- {
- get
- {
- if (!colliderBody)
- {
- colliderBody = UnityUtil.GetBreadthChild<BoxCollider>(transform, "ColliderBody");
- }
- return colliderBody;
- }
- }
- public Transform ModelRoot
- {
- get
- {
- if (!modelRoot)
- {
- modelRoot = UnityUtil.GetBreadthChild<Transform>(transform, "ModelRoot");
- }
- return modelRoot;
- }
- }
- //private void Awake()
- //{
- // ToggleComponent(false);
- // //if (Transfer && Transfer.Box)
- // //{
- // //}
- //}
- //public override void OnStopScale()
- //{
- // base.OnStopScale();
- // while (ColliderBody.size.magnitude * transform.localScale.x < (Vector3.one * 0.1f).magnitude / 2)
- // {
- // transform.localScale *= 1.5f;
- // }
- //}
- public override void OnZoomTransfer(TransferState state)
- {
- base.OnZoomTransfer(state);
- if (state == TransferState.Stop)
- {
- while (ColliderBody.size.magnitude * transform.localScale.x < (Vector3.one * 0.1f).magnitude)
- {
- transform.localScale *= 1.5f;
- }
- SendTransfer(2, (int)state);
- }
- }
- public void CalculateRendererBounds()
- {
- if (ModelRoot.childCount < 1)
- {
- DownInfo.SetActive(true);
- return;
- }
- DownInfo.SetActive(false);
- ChooseKuang.SetActive(false);
- //transform.DOKill(true);
- if (ModelRenderers == null || ModelRenderers.Length < 1)
- ModelRenderers = ModelRoot.GetComponentsInChildren<Renderer>();
- 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<Collider>();
- 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<ArtTaskAction>()&&!this.GetComponentInChildren<ArtTaskChild>())
- //{
- while (ColliderBody.size.magnitude > (Vector3.one * 0.2f).magnitude)
- {
- ColliderBody.size /= 2;
- ModelRoot.transform.localScale /= 2;
- ModelRoot.transform.localPosition /= 2;
- // yield return new WaitForFixedUpdate();
- }
- // 根据ColliderBody设置包围盒大小
- GetComponent<BoxCollider>().size = transform.Find("ColliderBody").GetComponent<BoxCollider>().size;
- //}
- 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;
- Transfer.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);
- if (Transfer && Transfer.IsInit)
- {
- CalculateRendererBounds();
- }
- //body.SetParent(ModelRoot.transform);
- //body.transform.localPosition = Vector3.zero;
- //body.localRotation = Quaternion.identity;
- //body.transform.localScale = Vector3.one;
- }
-
- }
- //private void Update()
- //{
- // if (Input.GetKeyDown(KeyCode.O))
- // {
- // CalculateRendererBounds();
- // }
- //}
- /// <summary>
- /// 添加到容器
- /// </summary>
- /// <param name="art"></param>
- public override void AddToContainer(UnityEngine.Object art, ArtInfo info)
- {
- base.AddToContainer(art, info);
- if (art is GameObject)
- {
- GameObject obj = art as GameObject;
- AddModel(obj.transform);
- }
- }
- 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<TextMesh>(DownInfo.transform, "ProcessLabel");
- }
- return processLabel;
- }
- }
- /// <summary>
- /// 更新下载进度
- /// </summary>
- /// <param name="process"></param>
- public override void UpdateProcess(float process)
- {
- if (ProcessLabel)
- {
- ProcessLabel.text = process.ToString("P1");
- if (process > 0.99f)
- {
- ProcessLabel.text = 99 + "%";
- }
- }
- }
- }
- }
|