Tanoda
unity-ui-extensions/Runtime/Scripts/Controls/ColorPicker/TiltWindow.cs
Go to the documentation of this file.
1
3
5
7{
8 public class TiltWindow : MonoBehaviour, IDragHandler
9 {
10 public Vector2 range = new Vector2(5f, 3f);
11
12 private Transform mTrans;
13 private Quaternion mStart;
14 private Vector2 mRot = Vector2.zero;
15 private Vector2 m_screenPos;
16
17
18 void Start()
19 {
20 mTrans = transform;
21 mStart = mTrans.localRotation;
22 }
23
24 void Update()
25 {
26 Vector3 pos = m_screenPos;
27
28 float halfWidth = Screen.width * 0.5f;
29 float halfHeight = Screen.height * 0.5f;
30 float x = Mathf.Clamp((pos.x - halfWidth) / halfWidth, -1f, 1f);
31 float y = Mathf.Clamp((pos.y - halfHeight) / halfHeight, -1f, 1f);
32 mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * 5f);
33
34 mTrans.localRotation = mStart * Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f);
35 }
36
37
38 public void OnDrag(PointerEventData eventData)
39 {
40 m_screenPos = eventData.position;
41 }
42 }
43}
Credit Erdener Gonenc - @PixelEnvision.