WorldTransformAxis.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using System;
  2. using UnityEngine;
  3. public class WorldTransformAxis : MonoBehaviour
  4. {
  5. public Vector2 ScaleMinMax
  6. {
  7. get
  8. {
  9. return this.ScaleMinMax_;
  10. }
  11. set
  12. {
  13. this.ScaleMinMax_ = value;
  14. GameObject[] array = new GameObject[]
  15. {
  16. this.XAxisObject,
  17. this.YAxisObject,
  18. this.ZAxisObject,
  19. this.AllAxisObject
  20. };
  21. for (int i = 0; i < array.Length; i++)
  22. {
  23. if (array[i] != null && array[i].GetComponent<WorldTransformAxis>() != null)
  24. {
  25. array[i].GetComponent<WorldTransformAxis>().ScaleMinMax = value;
  26. }
  27. }
  28. }
  29. }
  30. public void Awake()
  31. {
  32. GameObject[] array = new GameObject[]
  33. {
  34. this.XAxisObject,
  35. this.YAxisObject,
  36. this.ZAxisObject,
  37. this.AllAxisObject
  38. };
  39. WorldTransformAxis.MoveType[] array2 = new WorldTransformAxis.MoveType[]
  40. {
  41. WorldTransformAxis.MoveType.X,
  42. WorldTransformAxis.MoveType.Y,
  43. WorldTransformAxis.MoveType.Z,
  44. WorldTransformAxis.MoveType.All
  45. };
  46. for (int i = 0; i < array.Length; i++)
  47. {
  48. if (array[i] != null && array[i].GetComponent<WorldTransformAxis>() == null)
  49. {
  50. WorldTransformAxis worldTransformAxis = array[i].AddComponent<WorldTransformAxis>();
  51. worldTransformAxis.move_type_ = array2[i];
  52. worldTransformAxis.parent_obj_ = this;
  53. worldTransformAxis.ScaleMinMax = this.ScaleMinMax;
  54. }
  55. }
  56. this.init_position_ = base.transform.localPosition;
  57. this.init_scale_ = base.transform.localScale;
  58. this.arrow_mesh_ = base.gameObject.GetComponent<MeshRenderer>();
  59. }
  60. public void Update()
  61. {
  62. if (this.move_type_ != WorldTransformAxis.MoveType.Null && this.target != null)
  63. {
  64. if (NInput.GetMouseButtonDown(0) && !this.is_click_)
  65. {
  66. if (UICamera.Raycast(Input.mousePosition) || GizmoRender.control_lock)
  67. {
  68. return;
  69. }
  70. Ray ray = GameMain.Instance.MainCamera.camera.ScreenPointToRay(Input.mousePosition);
  71. RaycastHit raycastHit = default(RaycastHit);
  72. if (!Physics.Raycast(ray, out raycastHit, float.PositiveInfinity, LayerMask.GetMask(new string[]
  73. {
  74. LayerMask.LayerToName(base.gameObject.layer)
  75. })) || raycastHit.transform != base.transform)
  76. {
  77. return;
  78. }
  79. this.OnMouseDownEvent();
  80. return;
  81. }
  82. else if (this.is_click_)
  83. {
  84. if (NInput.GetMouseButton(0))
  85. {
  86. this.OnMouseDragEvent();
  87. return;
  88. }
  89. this.is_click_ = (this.parent_obj_.is_drag_ = false);
  90. GizmoRender.global_control_lock = false;
  91. }
  92. }
  93. if (this.target == null || this.parent_obj_ != null)
  94. {
  95. return;
  96. }
  97. base.transform.position = this.target.transform.position + this.offset_position_;
  98. if (GameMain.Instance.VRMode)
  99. {
  100. base.transform.rotation = this.target.transform.rotation;
  101. base.transform.localScale = this.target.transform.localScale;
  102. }
  103. }
  104. private void OnMouseDownEvent()
  105. {
  106. Transform transform = this.target.transform;
  107. GizmoRender.global_control_lock = true;
  108. this.screen_point_ = Camera.main.WorldToScreenPoint(transform.position);
  109. float x = Input.mousePosition.x;
  110. float y = Input.mousePosition.y;
  111. this.object_offset_ = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(x, y, this.screen_point_.z));
  112. this.is_click_ = true;
  113. }
  114. private void OnMouseDragEvent()
  115. {
  116. if (this.move_type_ == WorldTransformAxis.MoveType.Null || this.target == null || !this.is_click_)
  117. {
  118. return;
  119. }
  120. this.parent_obj_.is_drag_ = true;
  121. this.is_drag_ = true;
  122. float x = Input.mousePosition.x;
  123. float y = Input.mousePosition.y;
  124. Vector3 position = new Vector3(x, y, this.screen_point_.z);
  125. Vector3 position2 = Camera.main.ScreenToWorldPoint(position) + this.object_offset_;
  126. Transform transform = this.target.transform;
  127. Vector3 position3 = transform.position;
  128. if (this.move_type_ != WorldTransformAxis.MoveType.All)
  129. {
  130. if (this.move_type_ != WorldTransformAxis.MoveType.X)
  131. {
  132. position2.x = position3.x;
  133. }
  134. if (this.move_type_ != WorldTransformAxis.MoveType.Y)
  135. {
  136. position2.y = position3.y;
  137. }
  138. if (this.move_type_ != WorldTransformAxis.MoveType.Z)
  139. {
  140. position2.z = position3.z;
  141. }
  142. }
  143. transform.position = position2;
  144. if (this.parent_obj_ != null)
  145. {
  146. this.parent_obj_.transform.position = position2;
  147. }
  148. else
  149. {
  150. base.transform.position = position2;
  151. }
  152. }
  153. public void Apply()
  154. {
  155. if (this.target == null || this.target.transform == null)
  156. {
  157. return;
  158. }
  159. Transform transform = (!(this.parent_obj_ != null)) ? base.transform : this.parent_obj_.transform;
  160. this.target.transform.position = transform.position;
  161. this.target.transform.rotation = transform.rotation;
  162. this.target.transform.localScale = transform.localScale;
  163. }
  164. public bool is_drag
  165. {
  166. get
  167. {
  168. return this.is_drag_;
  169. }
  170. }
  171. public bool is_grip
  172. {
  173. get
  174. {
  175. return this.is_grip_;
  176. }
  177. set
  178. {
  179. this.is_grip_ = value;
  180. if (this.parent_obj_ != null)
  181. {
  182. this.parent_obj_.is_grip = value;
  183. }
  184. }
  185. }
  186. public Vector3 offsetPosition
  187. {
  188. get
  189. {
  190. return this.offset_position_;
  191. }
  192. set
  193. {
  194. this.offset_position_ = value;
  195. }
  196. }
  197. public float offsetScale
  198. {
  199. get
  200. {
  201. return this.offset_scale_;
  202. }
  203. set
  204. {
  205. this.offset_scale_ = value;
  206. base.transform.localScale = new Vector3(this.offset_scale_, this.offset_scale_, this.offset_scale_);
  207. }
  208. }
  209. public bool Visible
  210. {
  211. get
  212. {
  213. return base.gameObject.activeSelf;
  214. }
  215. set
  216. {
  217. base.gameObject.SetActive(value);
  218. if (value)
  219. {
  220. this.Update();
  221. }
  222. }
  223. }
  224. public bool VisibleAxisArrow
  225. {
  226. get
  227. {
  228. return !(this.arrow_mesh_ == null) && this.arrow_mesh_.enabled;
  229. }
  230. set
  231. {
  232. if (this.arrow_mesh_ == null)
  233. {
  234. return;
  235. }
  236. this.arrow_mesh_.enabled = value;
  237. if (this.XAxisObject != null)
  238. {
  239. this.XAxisObject.SetActive(value);
  240. }
  241. if (this.YAxisObject != null)
  242. {
  243. this.YAxisObject.SetActive(value);
  244. }
  245. if (this.ZAxisObject != null)
  246. {
  247. this.ZAxisObject.SetActive(value);
  248. }
  249. }
  250. }
  251. private GameObject target
  252. {
  253. get
  254. {
  255. return (!(this.parent_obj_ != null)) ? this.TargetObject : this.parent_obj_.target;
  256. }
  257. }
  258. public GameObject TargetObject;
  259. public GameObject XAxisObject;
  260. public GameObject YAxisObject;
  261. public GameObject ZAxisObject;
  262. public GameObject AllAxisObject;
  263. private Vector2 ScaleMinMax_;
  264. private bool is_grip_;
  265. private WorldTransformAxis.MoveType move_type_;
  266. private Vector3 screen_point_;
  267. private Vector3 object_offset_;
  268. private WorldTransformAxis parent_obj_;
  269. private MeshRenderer arrow_mesh_;
  270. private Vector3 init_position_;
  271. private Vector3 init_scale_;
  272. private Vector3 offset_position_;
  273. private float offset_scale_;
  274. private bool is_drag_;
  275. private bool is_click_;
  276. private enum MoveType
  277. {
  278. Null,
  279. X,
  280. Y,
  281. Z,
  282. All
  283. }
  284. }