using System; using UnityEngine; public class WorldTransformRotate : MonoBehaviour { public void Awake() { GameObject[] array = new GameObject[] { this.XAxisObject, this.YAxisObject, this.ZAxisObject }; WorldTransformRotate.RotateType[] array2 = new WorldTransformRotate.RotateType[] { WorldTransformRotate.RotateType.X, WorldTransformRotate.RotateType.Y, WorldTransformRotate.RotateType.Z }; for (int i = 0; i < array.Length; i++) { if (array[i] != null && array[i].GetComponent() == null) { WorldTransformRotate worldTransformRotate = array[i].AddComponent(); worldTransformRotate.rotate_type_ = array2[i]; worldTransformRotate.parent_obj_ = this; } } this.init_position_ = base.transform.localPosition; this.init_scale_ = base.transform.localScale; } public void OnEnable() { this.Update(); } public void Update() { if (this.target == null || this.parent_obj_ != null) { return; } base.transform.position = this.target.transform.position + this.offset_position_; base.transform.localRotation = this.target.transform.localRotation; } private void OnMouseDown() { if (this.rotate_type_ == WorldTransformRotate.RotateType.Null || this.target == null) { return; } Vector3 vector = Camera.main.WorldToScreenPoint(base.transform.position); float x = Input.mousePosition.x; float y = Input.mousePosition.y; this.init_clicl_pos_ = new Vector2(x, y); Camera main = Camera.main; Vector3 rhs = Vector3.zero; if (this.rotate_type_ == WorldTransformRotate.RotateType.X) { rhs = base.transform.right; } else if (this.rotate_type_ == WorldTransformRotate.RotateType.Y) { rhs = base.transform.up; } else if (this.rotate_type_ == WorldTransformRotate.RotateType.Z) { rhs = base.transform.forward; } Ray ray = main.ScreenPointToRay(Input.mousePosition); RaycastHit raycastHit = default(RaycastHit); if (!Physics.Raycast(ray, out raycastHit)) { return; } float num = Vector3.Dot(ray.direction, rhs); ray = main.ScreenPointToRay(Input.mousePosition); raycastHit = default(RaycastHit); if (Physics.Raycast(ray, out raycastHit)) { if (vector.y >= Camera.main.WorldToScreenPoint(raycastHit.point).y + 10f) { num *= -1f; } this.complement_ = ((0f >= num) ? 1 : -1); this.backup_qua_ = this.target.transform.rotation; return; } } private void OnMouseDrag() { if (this.rotate_type_ == WorldTransformRotate.RotateType.Null || this.target == null) { return; } float x = Input.mousePosition.x; float y = Input.mousePosition.y; float num = (x - this.init_clicl_pos_.x) / 1000f * (float)this.complement_; if (this.rotate_type_ == WorldTransformRotate.RotateType.X) { this.target.transform.localRotation = this.backup_qua_ * Quaternion.Euler(180f * num, 0f, 0f); } else if (this.rotate_type_ == WorldTransformRotate.RotateType.Y) { this.target.transform.localRotation = this.backup_qua_ * Quaternion.Euler(0f, 180f * num, 0f); } else if (this.rotate_type_ == WorldTransformRotate.RotateType.Z) { this.target.transform.localRotation = this.backup_qua_ * Quaternion.Euler(0f, 0f, 180f * num); } } private GameObject target { get { return (!(this.parent_obj_ != null)) ? this.TargetObject : this.parent_obj_.target; } } public Vector3 offsetPosition { get { return this.offset_position_; } set { this.offset_position_ = value; } } public Vector3 offsetScale { get { return this.offset_scale_; } set { this.offset_scale_ = value; base.transform.localScale = this.offset_scale_; } } public GameObject TargetObject; public GameObject XAxisObject; public GameObject YAxisObject; public GameObject ZAxisObject; private WorldTransformRotate.RotateType rotate_type_; private Vector3 init_position_; private Vector3 init_scale_; private Vector3 offset_position_; private Vector3 offset_scale_; private WorldTransformRotate parent_obj_; private Quaternion backup_qua_; private Vector2 init_clicl_pos_; private int complement_; private enum RotateType { Null, X, Y, Z } }