123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 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<WorldTransformRotate>() == null)
- {
- WorldTransformRotate worldTransformRotate = array[i].AddComponent<WorldTransformRotate>();
- 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
- }
- }
|