using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using LzFramework.FSM.Msg; using LzFramework; namespace DisComputer { /* * 模型控制 * @author lz */ public class ModelControl : Singleton { public LayerMask clickableLayer; //模糊后背景 public SpriteRenderer vagueMask_spr; // Start is called before the first frame update void Start() { curRotPlatform = destopPlatform; InitMsg(); } // Update is called once per frame void Update() { if (MouseControl.Instance.isFocuseState) return; MouseThroughCamera(); RotationTarget(); } /// /// 模型角度复原 /// public void RewindTargetRoatation() { curRotPlatform.transform.DORotate(new Vector3(0, 0, 0), 0.5f); } /// /// 注册消息 /// void InitMsg() { MessageDispatcher.AddListener(MsgName.ModelControlShowCenter, OnRecShowPlatform); MessageDispatcher.AddListener(MsgName.ModelControlShowDesktop, OnRecDesktopPlatform); } /// /// 展示中心平台 /// /// void OnRecShowPlatform(IMessage message) { StartCoroutine(MoveToCenter(dragTarget.transform)); } /// /// 展示桌面平台 /// /// void OnRecDesktopPlatform(IMessage message) { StartCoroutine(MoveToDesktop(dragTarget.transform)); } //拖拽物体 public GameObject dragTarget; //是否点击到物体 private bool isClickedObj; private Vector3 screenSpace; private Vector3 offset; /// /// 鼠标控制 /// void MouseThroughCamera() { if (Input.GetMouseButtonDown(0)) { if (ChectClickTarget()) { screenSpace = Camera.main.WorldToScreenPoint(dragTarget.transform.position); offset = dragTarget.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z)); } } if (isClickedObj) { Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z); Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset; dragTarget.transform.position = curPosition; } if (Input.GetMouseButtonUp(0)) { isClickedObj = false; } } /// /// 检测是否点击目标物体 /// /// bool ChectClickTarget() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 50, clickableLayer.value)) { dragTarget = hit.collider.gameObject; isClickedObj = true; return true; } return false; } //旋转速度 public float rotationSpeed; //桌面旋转平台 public Transform destopPlatform; //拆装旋转平台 public Transform showPlatform; //当前旋转平台 private Transform curRotPlatform; private bool isFree = false; /// /// 旋转目标 /// void RotationTarget() { if (curRotPlatform == null) return; Vector3 fwd = Camera.main.transform.forward; fwd.Normalize(); if (Input.GetMouseButton(1)) { Vector3 vaxis = Vector3.Cross(fwd, Vector3.right); curRotPlatform.Rotate(Vector3.up, -Input.GetAxis("Mouse X")* rotationSpeed, Space.World); if (isFree) { Vector3 haxis = Vector3.Cross(fwd, Vector3.up); curRotPlatform.Rotate(haxis, -Input.GetAxis("Mouse Y") * rotationSpeed, Space.World); } } } /// /// 移动到中间位置 /// /// /// IEnumerator MoveToCenter(Transform target) { yield return new WaitForEndOfFrame(); Tween tw_move = target.DOMove(showPlatform.position, 0.5f); tw_move.SetEase(Ease.OutQuart); vagueMask_spr.DOFade(1.0f, 0.5f); tw_move.OnComplete(()=> { isFree = true; target.SetParent(showPlatform,true); curRotPlatform = showPlatform; destopPlatform.localEulerAngles = Vector3.zero; }); target.DORotate(new Vector3(0, 300, 0), 0.5f); } /// /// 移动到桌面位置 /// /// /// IEnumerator MoveToDesktop(Transform target) { yield return new WaitForEndOfFrame(); Tween tw_move = target.DOMove(destopPlatform.position, 0.5f); vagueMask_spr.DOFade(0.0f, 0.5f); tw_move.SetEase(Ease.InSine); tw_move.OnComplete(() => { isFree = false; target.SetParent(destopPlatform, true); curRotPlatform = destopPlatform; showPlatform.localEulerAngles = Vector3.zero; }); target.DORotate(new Vector3(0, -40, 0), 0.5f); } } }