ProgressEdit.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using ShadowKit;
  6. using System;
  7. using SC.InputSystem;
  8. public abstract class ProgressEdit : MonoBehaviour, ISCPointerDragHandler, ISCPointerDownHandler, ISCPointerUpHandler
  9. {
  10. [HideInInspector]
  11. public GameObject scrollbar;
  12. [HideInInspector]
  13. public GameObject control;
  14. private float initX;
  15. private float initY;
  16. private float initZ;
  17. // Use this for initialization
  18. BoxCollider box;
  19. [HideInInspector]
  20. public float perValue;
  21. [HideInInspector]
  22. public bool isClick;
  23. Vector3[] v3;
  24. float toto;
  25. public Action<float> callBack;
  26. public Action<float> cEndBack;
  27. // 定义枚举
  28. [Serializable]
  29. public enum ProgressTypeEnum
  30. {
  31. [EnumAttirbute("X")]
  32. TypeX = 0,
  33. [EnumAttirbute("Y")]
  34. TypeY = 1,
  35. [EnumAttirbute("Z")]
  36. TypeZ = 2
  37. }
  38. // 定义枚举属性
  39. [EnumAttirbute("拖动方向")]
  40. public ProgressTypeEnum editType;
  41. // 定义枚举
  42. [Serializable]
  43. public enum ProgressEditEnum
  44. {
  45. [EnumAttirbute("无进度条")]
  46. NoScroll = 0,
  47. [EnumAttirbute("有进度条")]
  48. Scroll = 1,
  49. [EnumAttirbute("滚动")]
  50. Normal = 2
  51. }
  52. // 定义枚举属性
  53. [EnumAttirbute("类型")]
  54. public ProgressEditEnum editScroll;
  55. public virtual void Start()
  56. {
  57. box = this.GetComponent<BoxCollider>();
  58. switch (editScroll)
  59. {
  60. case ProgressEditEnum.Scroll:
  61. initX = scrollbar.transform.localScale.x;
  62. initY = scrollbar.transform.localScale.y;
  63. initZ = scrollbar.transform.localScale.z;
  64. break;
  65. }
  66. initPro();
  67. }
  68. public virtual void ChangeValue(float f)
  69. {
  70. }
  71. // Update is called once per frame
  72. void Update()
  73. {
  74. }
  75. public void GetSocller(Vector3 pv3)
  76. {
  77. Vector3[] v3 = GetBoxColliderVertexPositions(box);
  78. float temp = 0;
  79. float pv3temp = 0;
  80. float v3temp = 0;
  81. switch (editType)
  82. {
  83. case ProgressTypeEnum.TypeX:
  84. temp = Vector3.Distance(new Vector3(box.transform.InverseTransformPoint(pv3).x, 0, 0), new Vector3(box.transform.InverseTransformPoint(v3[1]).x, 0, 0));
  85. pv3temp = box.transform.InverseTransformPoint(pv3).x;
  86. v3temp = box.transform.InverseTransformPoint(v3[1]).x;
  87. break;
  88. case ProgressTypeEnum.TypeY:
  89. temp = Vector3.Distance(new Vector3(0, box.transform.InverseTransformPoint(pv3).y, 0), new Vector3(0, box.transform.InverseTransformPoint(v3[1]).y, 0));
  90. pv3temp = box.transform.InverseTransformPoint(pv3).y;
  91. v3temp = box.transform.InverseTransformPoint(v3[1]).y;
  92. break;
  93. case ProgressTypeEnum.TypeZ:
  94. temp = Vector3.Distance(new Vector3(0, 0, box.transform.InverseTransformPoint(pv3).z), new Vector3(0, 0, box.transform.InverseTransformPoint(v3[1]).z));
  95. pv3temp = box.transform.InverseTransformPoint(pv3).z;
  96. v3temp = box.transform.InverseTransformPoint(v3[1]).z;
  97. break;
  98. }
  99. perValue = temp / toto;
  100. //if (callBack != null)
  101. //{
  102. // callBack(perValue);
  103. //}
  104. if (perValue > 1 && pv3temp > v3temp)
  105. {
  106. setValue(1);
  107. }
  108. else if (pv3temp < v3temp)
  109. {
  110. setValue(0);
  111. }
  112. else
  113. {
  114. setValue(perValue);
  115. }
  116. }
  117. public void initPro()
  118. {
  119. v3 = GetBoxColliderVertexPositions(box);
  120. toto = Vector3.Distance(box.transform.InverseTransformPoint(v3[0]), box.transform.InverseTransformPoint(v3[1]));
  121. }
  122. public virtual void setValue(float bfb)
  123. {
  124. if (bfb >= 0 && bfb <= 1)
  125. {
  126. if (callBack != null && isClick)
  127. {
  128. callBack(bfb);
  129. }
  130. perValue = bfb;
  131. Vector3 position;
  132. switch (editScroll)
  133. {
  134. case ProgressEditEnum.Scroll:
  135. switch (editType)
  136. {
  137. case ProgressTypeEnum.TypeX:
  138. control.transform.localPosition = new Vector3(toto * bfb - toto * 0.5f, 0, 0);
  139. position = -new Vector3(1, 0, 0) * (toto * 0.5f - toto * bfb * 0.5f);// left justified;
  140. scrollbar.transform.localPosition = position;
  141. scrollbar.transform.localScale = new Vector3(initX * bfb, initY, initZ);
  142. break;
  143. case ProgressTypeEnum.TypeY:
  144. control.transform.localPosition = new Vector3(0, toto * bfb - toto * 0.5f, 0);
  145. position = -new Vector3(0, 1, 0) * (toto * 0.5f - toto * bfb * 0.5f); // left justified;
  146. scrollbar.transform.localPosition = position;
  147. scrollbar.transform.localScale = new Vector3(initX, initY * bfb, initZ);
  148. break;
  149. case ProgressTypeEnum.TypeZ:
  150. control.transform.localPosition = new Vector3(0, 0, toto * bfb - toto * 0.5f);
  151. position = -new Vector3(0, 0, 1) * (toto * 0.5f - toto * bfb * 0.5f); // left justified;
  152. scrollbar.transform.localPosition = position;
  153. scrollbar.transform.localScale = new Vector3(initX, initY, initZ * bfb);
  154. break;
  155. }
  156. break;
  157. case ProgressEditEnum.NoScroll:
  158. switch (editType)
  159. {
  160. case ProgressTypeEnum.TypeX:
  161. control.transform.localPosition = new Vector3(toto * bfb - toto * 0.5f, 0, 0);
  162. break;
  163. case ProgressTypeEnum.TypeY:
  164. control.transform.localPosition = new Vector3(0, toto * bfb - toto * 0.5f, 0);
  165. break;
  166. case ProgressTypeEnum.TypeZ:
  167. control.transform.localPosition = new Vector3(0, 0, toto * bfb - toto * 0.5f);
  168. break;
  169. }
  170. break;
  171. }
  172. }
  173. }
  174. Vector3[] GetBoxColliderVertexPositions(BoxCollider boxcollider)
  175. {
  176. var vertices = new Vector3[2];
  177. switch (editType)
  178. {
  179. case ProgressTypeEnum.TypeX:
  180. //下面4个点
  181. vertices[0] = boxcollider.transform.TransformPoint(boxcollider.center + new Vector3(boxcollider.size.x, boxcollider.size.y, boxcollider.size.z) * 0.5f);
  182. //上面4个点
  183. vertices[1] = boxcollider.transform.TransformPoint(boxcollider.center + new Vector3(-boxcollider.size.x, boxcollider.size.y, boxcollider.size.z) * 0.5f);
  184. break;
  185. case ProgressTypeEnum.TypeY:
  186. //下面4个点
  187. vertices[0] = boxcollider.transform.TransformPoint(boxcollider.center + new Vector3(boxcollider.size.x, boxcollider.size.y, boxcollider.size.z) * 0.5f);
  188. //上面4个点
  189. vertices[1] = boxcollider.transform.TransformPoint(boxcollider.center + new Vector3(boxcollider.size.x, -boxcollider.size.y, boxcollider.size.z) * 0.5f);
  190. break;
  191. case ProgressTypeEnum.TypeZ:
  192. //下面4个点
  193. vertices[0] = boxcollider.transform.TransformPoint(boxcollider.center + new Vector3(boxcollider.size.x, boxcollider.size.y, boxcollider.size.z) * 0.5f);
  194. //上面4个点
  195. vertices[1] = boxcollider.transform.TransformPoint(boxcollider.center + new Vector3(boxcollider.size.x, boxcollider.size.y, -boxcollider.size.z) * 0.5f);
  196. break;
  197. }
  198. return vertices;
  199. }
  200. public void enterbox()
  201. {
  202. isClick = true;
  203. }
  204. public void exitbox()
  205. {
  206. ChangeValue(perValue);
  207. if (cEndBack != null)
  208. {
  209. cEndBack(perValue);
  210. }
  211. isClick = false;
  212. }
  213. public virtual void OnSCPointerDrag(InputDevicePartBase part, SCPointEventData eventData)
  214. {
  215. //eventData.hitAnchorPointer.transform.position = eventData.currentHitPoint;
  216. //GetSocller(eventData.currentHitPoint);
  217. if (eventData.target != null)
  218. {
  219. eventData.hitAnchorPointer.transform.position = eventData.currentHitPoint;
  220. GetSocller(eventData.currentHitPoint);
  221. }
  222. else if (eventData.target == null)
  223. {
  224. eventData.hitAnchorPointer.transform.position = eventData.hitPointer.transform.position;
  225. GetSocller(eventData.hitPointer.transform.position);
  226. }
  227. }
  228. public virtual void OnSCPointerDown(InputDevicePartBase part, SCPointEventData eventData)
  229. {
  230. GetSocller(eventData.currentHitPoint);
  231. enterbox();
  232. }
  233. public virtual void OnSCPointerUp(InputDevicePartBase part, SCPointEventData eventData)
  234. {
  235. exitbox();
  236. }
  237. }