123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using DG.Tweening;
- using UnityEngine.UI;
- public enum UI_TriggerItemType
- {
- none,
- hand,
- gloves,
- handWithGloves,
- screwdriver,//螺丝刀
- chassisCover,//机箱盖
- chassisCoverLock,//机箱盖卡扣
- chassisCoverSetPos,//机箱盖位置
- fan,
- fanSetPos,//风扇安装位置
- power,
- AntiStaticBag,//防静电袋
- powerLock,//电源卡扣
- powerSetPos,//电源插槽
- netWorkCard,
- netWorkCardSetPos,
- lightModel,
- lightModelSetPos,
- opticalFiber,//光纤
- valve,//阀门
- coolCover,//制冷机盖子
- cleaningRod,//清洁棒
- refrigerant,//冷媒
- refrigerantPos,//冷媒添加口
- powerSwitch,//电闸
- fuse,//保险丝
- fusePos,
- battery,//电池
- batteryPos,
- ram,//内存条
- ramPos,
- hardDisk,
- hardDiskPos,
- screw,//螺丝
- }
- [RequireComponent(typeof(Rigidbody),typeof(BoxCollider))]
- public class UI_ItemMoveTool : MonoBehaviour,IDragHandler,IEndDragHandler,IBeginDragHandler
- {
- private float xOffset = 0;
- private float yOffset = 0;
- private Image img;
- private Transform moveContent;
- private Transform t_parent;
- private bool isDrag = false;
- public UI_TriggerItemType itemType;
- private UI_TriggerItemType lastTriggerType;
- public bool dragHandlerActive = true;
-
- private void Awake()
- {
- xOffset = Screen.width * 0.5f;
- yOffset = Screen.height * 0.5f;
- img = this.GetComponent<Image>();
- t_parent = this.transform.parent;
- moveContent = GameObject.FindWithTag("MoveItemContent").transform;
- }
-
- public void OnDrag(PointerEventData eventData)
- {
- if (!dragHandlerActive)
- {
- return;
- }
- float saclex = 1920.0f / Screen.width;
- float sacley = 1080.0f / Screen.height;
- Vector3 pos = eventData.position;
- pos.x -= xOffset;
- pos.y -= yOffset;
- pos.x *= saclex;
- pos.y *= sacley;
- this.transform.localPosition = pos;
- }
- public void OnEndDrag(PointerEventData eventData)
- {
- if (!dragHandlerActive)
- {
- return;
- }
- this.transform.SetParent(t_parent);
- this.transform.DOLocalMove(Vector3.zero, 0.2f);
- isDrag = false;
- if (lastTriggerType != UI_TriggerItemType.none)
- {
- GameMain.Event.Fire(this,UI_ItemMoveDoneEvent.Create(itemType,lastTriggerType));
- }
- }
- public void OnBeginDrag(PointerEventData eventData)
- {
- if (!dragHandlerActive)
- {
- return;
- }
- this.transform.SetParent(moveContent);
- lastTriggerType = UI_TriggerItemType.none;
- isDrag = true;
- }
- private void OnTriggerEnter(Collider other)
- {
- if (isDrag)
- {
- UI_ItemMoveTool targetItem;
- if (other.TryGetComponent<UI_ItemMoveTool>(out targetItem))
- {
- lastTriggerType = targetItem.itemType;
- }
- }
- }
-
- private void OnTriggerExit(Collider other)
- {
- if (isDrag)
- {
- UI_ItemMoveTool targetItem;
- if (other.TryGetComponent<UI_ItemMoveTool>(out targetItem))
- {
- lastTriggerType = UI_TriggerItemType.none;
- }
- }
- }
- }
|