UI_ItemMoveTool.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using DG.Tweening;
  7. using UnityEngine.UI;
  8. public enum UI_TriggerItemType
  9. {
  10. none,
  11. hand,
  12. gloves,
  13. handWithGloves,
  14. screwdriver,//螺丝刀
  15. chassisCover,//机箱盖
  16. chassisCoverLock,//机箱盖卡扣
  17. chassisCoverSetPos,//机箱盖位置
  18. fan,
  19. fanSetPos,//风扇安装位置
  20. power,
  21. AntiStaticBag,//防静电袋
  22. powerLock,//电源卡扣
  23. powerSetPos,//电源插槽
  24. netWorkCard,
  25. netWorkCardSetPos,
  26. lightModel,
  27. lightModelSetPos,
  28. opticalFiber,//光纤
  29. valve,//阀门
  30. coolCover,//制冷机盖子
  31. cleaningRod,//清洁棒
  32. refrigerant,//冷媒
  33. refrigerantPos,//冷媒添加口
  34. powerSwitch,//电闸
  35. fuse,//保险丝
  36. fusePos,
  37. battery,//电池
  38. batteryPos,
  39. ram,//内存条
  40. ramPos,
  41. hardDisk,
  42. hardDiskPos,
  43. screw,//螺丝
  44. }
  45. [RequireComponent(typeof(Rigidbody),typeof(BoxCollider))]
  46. public class UI_ItemMoveTool : MonoBehaviour,IDragHandler,IEndDragHandler,IBeginDragHandler
  47. {
  48. private float xOffset = 0;
  49. private float yOffset = 0;
  50. private Image img;
  51. private Transform moveContent;
  52. private Transform t_parent;
  53. private bool isDrag = false;
  54. public UI_TriggerItemType itemType;
  55. private UI_TriggerItemType lastTriggerType;
  56. public bool dragHandlerActive = true;
  57. private void Awake()
  58. {
  59. xOffset = Screen.width * 0.5f;
  60. yOffset = Screen.height * 0.5f;
  61. img = this.GetComponent<Image>();
  62. t_parent = this.transform.parent;
  63. moveContent = GameObject.FindWithTag("MoveItemContent").transform;
  64. }
  65. public void OnDrag(PointerEventData eventData)
  66. {
  67. if (!dragHandlerActive)
  68. {
  69. return;
  70. }
  71. float saclex = 1920.0f / Screen.width;
  72. float sacley = 1080.0f / Screen.height;
  73. Vector3 pos = eventData.position;
  74. pos.x -= xOffset;
  75. pos.y -= yOffset;
  76. pos.x *= saclex;
  77. pos.y *= sacley;
  78. this.transform.localPosition = pos;
  79. }
  80. public void OnEndDrag(PointerEventData eventData)
  81. {
  82. if (!dragHandlerActive)
  83. {
  84. return;
  85. }
  86. this.transform.SetParent(t_parent);
  87. this.transform.DOLocalMove(Vector3.zero, 0.2f);
  88. isDrag = false;
  89. if (lastTriggerType != UI_TriggerItemType.none)
  90. {
  91. GameMain.Event.Fire(this,UI_ItemMoveDoneEvent.Create(itemType,lastTriggerType));
  92. }
  93. }
  94. public void OnBeginDrag(PointerEventData eventData)
  95. {
  96. if (!dragHandlerActive)
  97. {
  98. return;
  99. }
  100. this.transform.SetParent(moveContent);
  101. lastTriggerType = UI_TriggerItemType.none;
  102. isDrag = true;
  103. }
  104. private void OnTriggerEnter(Collider other)
  105. {
  106. if (isDrag)
  107. {
  108. UI_ItemMoveTool targetItem;
  109. if (other.TryGetComponent<UI_ItemMoveTool>(out targetItem))
  110. {
  111. lastTriggerType = targetItem.itemType;
  112. }
  113. }
  114. }
  115. private void OnTriggerExit(Collider other)
  116. {
  117. if (isDrag)
  118. {
  119. UI_ItemMoveTool targetItem;
  120. if (other.TryGetComponent<UI_ItemMoveTool>(out targetItem))
  121. {
  122. lastTriggerType = UI_TriggerItemType.none;
  123. }
  124. }
  125. }
  126. }