WorldTransformRotate.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using UnityEngine;
  3. public class WorldTransformRotate : MonoBehaviour
  4. {
  5. public void Awake()
  6. {
  7. GameObject[] array = new GameObject[]
  8. {
  9. this.XAxisObject,
  10. this.YAxisObject,
  11. this.ZAxisObject
  12. };
  13. WorldTransformRotate.RotateType[] array2 = new WorldTransformRotate.RotateType[]
  14. {
  15. WorldTransformRotate.RotateType.X,
  16. WorldTransformRotate.RotateType.Y,
  17. WorldTransformRotate.RotateType.Z
  18. };
  19. for (int i = 0; i < array.Length; i++)
  20. {
  21. if (array[i] != null && array[i].GetComponent<WorldTransformRotate>() == null)
  22. {
  23. WorldTransformRotate worldTransformRotate = array[i].AddComponent<WorldTransformRotate>();
  24. worldTransformRotate.rotate_type_ = array2[i];
  25. worldTransformRotate.parent_obj_ = this;
  26. }
  27. }
  28. this.init_position_ = base.transform.localPosition;
  29. this.init_scale_ = base.transform.localScale;
  30. }
  31. public void OnEnable()
  32. {
  33. this.Update();
  34. }
  35. public void Update()
  36. {
  37. if (this.target == null || this.parent_obj_ != null)
  38. {
  39. return;
  40. }
  41. base.transform.position = this.target.transform.position + this.offset_position_;
  42. base.transform.localRotation = this.target.transform.localRotation;
  43. }
  44. private void OnMouseDown()
  45. {
  46. if (this.rotate_type_ == WorldTransformRotate.RotateType.Null || this.target == null)
  47. {
  48. return;
  49. }
  50. Vector3 vector = Camera.main.WorldToScreenPoint(base.transform.position);
  51. float x = Input.mousePosition.x;
  52. float y = Input.mousePosition.y;
  53. this.init_clicl_pos_ = new Vector2(x, y);
  54. Camera main = Camera.main;
  55. Vector3 rhs = Vector3.zero;
  56. if (this.rotate_type_ == WorldTransformRotate.RotateType.X)
  57. {
  58. rhs = base.transform.right;
  59. }
  60. else if (this.rotate_type_ == WorldTransformRotate.RotateType.Y)
  61. {
  62. rhs = base.transform.up;
  63. }
  64. else if (this.rotate_type_ == WorldTransformRotate.RotateType.Z)
  65. {
  66. rhs = base.transform.forward;
  67. }
  68. Ray ray = main.ScreenPointToRay(Input.mousePosition);
  69. RaycastHit raycastHit = default(RaycastHit);
  70. if (!Physics.Raycast(ray, out raycastHit))
  71. {
  72. return;
  73. }
  74. float num = Vector3.Dot(ray.direction, rhs);
  75. ray = main.ScreenPointToRay(Input.mousePosition);
  76. raycastHit = default(RaycastHit);
  77. if (Physics.Raycast(ray, out raycastHit))
  78. {
  79. if (vector.y >= Camera.main.WorldToScreenPoint(raycastHit.point).y + 10f)
  80. {
  81. num *= -1f;
  82. }
  83. this.complement_ = ((0f >= num) ? 1 : -1);
  84. this.backup_qua_ = this.target.transform.rotation;
  85. return;
  86. }
  87. }
  88. private void OnMouseDrag()
  89. {
  90. if (this.rotate_type_ == WorldTransformRotate.RotateType.Null || this.target == null)
  91. {
  92. return;
  93. }
  94. float x = Input.mousePosition.x;
  95. float y = Input.mousePosition.y;
  96. float num = (x - this.init_clicl_pos_.x) / 1000f * (float)this.complement_;
  97. if (this.rotate_type_ == WorldTransformRotate.RotateType.X)
  98. {
  99. this.target.transform.localRotation = this.backup_qua_ * Quaternion.Euler(180f * num, 0f, 0f);
  100. }
  101. else if (this.rotate_type_ == WorldTransformRotate.RotateType.Y)
  102. {
  103. this.target.transform.localRotation = this.backup_qua_ * Quaternion.Euler(0f, 180f * num, 0f);
  104. }
  105. else if (this.rotate_type_ == WorldTransformRotate.RotateType.Z)
  106. {
  107. this.target.transform.localRotation = this.backup_qua_ * Quaternion.Euler(0f, 0f, 180f * num);
  108. }
  109. }
  110. private GameObject target
  111. {
  112. get
  113. {
  114. return (!(this.parent_obj_ != null)) ? this.TargetObject : this.parent_obj_.target;
  115. }
  116. }
  117. public Vector3 offsetPosition
  118. {
  119. get
  120. {
  121. return this.offset_position_;
  122. }
  123. set
  124. {
  125. this.offset_position_ = value;
  126. }
  127. }
  128. public Vector3 offsetScale
  129. {
  130. get
  131. {
  132. return this.offset_scale_;
  133. }
  134. set
  135. {
  136. this.offset_scale_ = value;
  137. base.transform.localScale = this.offset_scale_;
  138. }
  139. }
  140. public GameObject TargetObject;
  141. public GameObject XAxisObject;
  142. public GameObject YAxisObject;
  143. public GameObject ZAxisObject;
  144. private WorldTransformRotate.RotateType rotate_type_;
  145. private Vector3 init_position_;
  146. private Vector3 init_scale_;
  147. private Vector3 offset_position_;
  148. private Vector3 offset_scale_;
  149. private WorldTransformRotate parent_obj_;
  150. private Quaternion backup_qua_;
  151. private Vector2 init_clicl_pos_;
  152. private int complement_;
  153. private enum RotateType
  154. {
  155. Null,
  156. X,
  157. Y,
  158. Z
  159. }
  160. }