using System.Collections; using System.Collections.Generic; using UnityEngine; using LzFramework; using UnityEngine.UI; using DG.Tweening; namespace DisComputer { /** * @func 鼠标控制器 * @author lz * @date 2020/05/28 */ public class MouseControl : Singleton { //加载环形图片 [SerializeField] private Texture2D loadCircle_tex; [SerializeField] private Color loadCircleColor; private bool isDelTemp = false; [HideInInspector] //是否进入聚焦状态 public bool isFocuseState = false; // Update is called once per frame void Update() { if (MainControl.Instance.mainState != MainState.Active) return; if (isFocuseState) return; //缩放摄像机视距 RollCameraView(); if (tempGridGo) { //抓取 if (Input.GetMouseButton(0)) { //Cursor.visible = false; tempGridGo.transform.localPosition = CoordinateTrasition.ScreenPosToUguiPos(Input.mousePosition,canvas,true); //发射射线检测 SendRayToLoadPc(); } if (Input.GetMouseButtonUp(0)) { //Cursor.visible = true; isDelTemp = true; Sequence sequence = DOTween.Sequence(); sequence.Append(tempGridGo.transform.DOLocalMove(selectGrid.initPos, 0.15f)); sequence.Append(tempGridGo.transform.DOScale(1.0f, 0.25f).SetEase(Ease.OutBack).SetDelay(0.1f)); sequence.OnComplete(()=> { isDelTemp = false; tempGridGo.SetActive(false); DestroyImmediate(tempGridGo); tempGridGo = null; selectGrid.ShowSpareImg(); }); } }else if(tempToolGo) { } else { //Cursor.visible = true; } if (tempToolGo) { //抓取 if (Input.GetMouseButton(0)) { //Cursor.visible = false; tempToolGo.transform.localPosition = CoordinateTrasition.ScreenPosToUguiPos(Input.mousePosition, canvas, true); //发射射线检测 SendRayToUnloadPc(); } if (Input.GetMouseButtonUp(0)) { //Cursor.visible = true; isDelTemp = true; Tween move = tempToolGo.transform.DOScale(0.0f, 0.15f); move.SetEase(Ease.InSine); move.OnComplete(() => { isDelTemp = false; tempToolGo.SetActive(false); DestroyImmediate(tempToolGo); tempToolGo = null; selectTool.ResetTool(); }); } }else if (tempGridGo) { } else { //Cursor.visible = true; } } /// /// 拉近摄像头 /// void RollCameraView() { //缩小 if(Input.GetAxis("Mouse ScrollWheel")<0) { Camera.main.fieldOfView += 2; if (Camera.main.fieldOfView >= 60) { Camera.main.fieldOfView = 60; } } //放大 if(Input.GetAxis("Mouse ScrollWheel") > 0) { Camera.main.fieldOfView -= 2; if(Camera.main.fieldOfView <= 10) { Camera.main.fieldOfView = 10; } } } /// /// 恢复相机视距 /// public void RewindCameraField() { Camera.main.DOFieldOfView(60,0.3f).SetEase(Ease.InSine); } //选择的零件 private Grid selectGrid; private Canvas canvas; private GameObject tempGridGo; /// /// 设置抓取零件 /// public void SetDragSpare(Grid grid,System.Action hideSp) { if (isDelTemp|| tempGridGo != null || Input.GetMouseButton(1)) return; hideSp?.Invoke(); if (canvas == null) { canvas = GameObject.Find("UIRoot").GetComponent(); } selectGrid = grid; tempGridGo = Instantiate(grid.spare_img.gameObject); tempGridGo.transform.SetParent(canvas.transform); tempGridGo.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f); loading = tempGridGo.AddComponent(); loading.InitLoading(loadCircle_tex, loadCircleColor); tempGridGo.SetActive(true); } /// /// 零件加载 /// /// public void SpareLoading(System.Action finish) { if (isDelTemp) return; if (loading != null) { loading.UpdateLoading(() => { finish?.Invoke(); tempGridGo = null; selectGrid.CleraGird(); //Cursor.visible = true; loading.CancelLoading(); loading = null; }); } } //选择的工具 private ToolItem selectTool; private GameObject tempToolGo; //进度显示 private WorkLoading loading; /// /// 设置抓取工具 /// /// public void SetDragTool(ToolItem item) { if (tempToolGo != null || Input.GetMouseButton(1)) { return; } if(canvas == null) { canvas = GameObject.Find("UIRoot").GetComponent(); } selectTool = item; tempToolGo = Instantiate(item.tool_img.gameObject); tempToolGo.transform.SetParent(canvas.transform); tempToolGo.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f); loading = tempToolGo.AddComponent(); loading.InitLoading(loadCircle_tex,loadCircleColor); tempToolGo.SetActive(true); } /// /// 工具加载 /// /// public void ToolLoading(System.Action finish) { if (loading != null) { loading.UpdateLoading(()=> { finish?.Invoke(); tempToolGo = null; selectTool.ResetTool(); //Cursor.visible = true; loading.CancelLoading(); loading = null; }); } } /// /// 重置 /// public void ClearSelectTarget() { tempToolGo = null; tempGridGo = null; isFocuseState = false; } //零件检测层 public LayerMask checkLayer; private RaycastHit hit; /// /// 发射射线检测安装电脑 /// @reciever SpareItemControl /// private void SendRayToLoadPc() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray,out hit,50,checkLayer.value)) { selectGrid.chectSpareTag = hit.collider.gameObject.tag; hit.collider.gameObject.SendMessageUpwards("OnRecLoadColliderMsg", selectGrid, SendMessageOptions.DontRequireReceiver); hit.collider.gameObject.SendMessageUpwards("OnRecSpareColliderMessage", selectGrid,SendMessageOptions.DontRequireReceiver); } else { if(loading != null) { loading.RewindLoading(); } } } /// /// 发射射线检测拆除电脑 /// @reciever SpareItemControl /// private void SendRayToUnloadPc() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 50, checkLayer.value)) { selectTool.chectSpareTag = hit.collider.gameObject.tag; hit.collider.gameObject.SendMessageUpwards("OnRecUnloadColliderMsg", selectTool, SendMessageOptions.DontRequireReceiver); hit.collider.gameObject.SendMessageUpwards("OnRecChildColliderMessage",selectTool, SendMessageOptions.DontRequireReceiver); } else { if (loading != null) { loading.RewindLoading(); } } } } }