-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPercentagePosition.cs
141 lines (113 loc) · 2.79 KB
/
PercentagePosition.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Percentage Position for Unity2D
// Sets/Gets position relative by percentage according to screen width/height
// Original Author: MohHeader
// Modified by: ahmed-shehata
using UnityEngine;
using System.Collections;
public class PercentagePosition : MonoBehaviour {
public float VerticalPercent;
public float HorizontalPercent;
public Horizontal horizontal;
public Vertical vertical;
private float CameraAspectRatio = 0;
// x
public enum Horizontal{
Right,
Center,
Left
}
// y
public enum Vertical{
Top,
Center,
Bottom
}
void Awake(){
CameraAspectRatio = Camera.main.aspect;
RePosition ();
#if UNITY_EDITOR
UnityEditor.EditorApplication.update += UpdateOnChangeAspectRatio;
#endif
}
void UpdateOnChangeAspectRatio(){
if (CameraAspectRatio != Camera.main.aspect) {
CameraAspectRatio = Camera.main.aspect;
RePosition ();
}
}
[ContextMenu("RePosition")]
public void RePosition(){
float cheight = 2f * Camera.main.orthographicSize;
float cwidth = cheight * Camera.main.aspect;
Vector3 world = Camera.main.ScreenToWorldPoint(new Vector3 (Camera.main.pixelWidth
, Camera.main.pixelHeight
, 0));
if (transform != null) {
float x = transform.position.x;
float y = transform.position.y;
switch (horizontal) {
case Horizontal.Right:
x = world.x - HorizontalPercent*cwidth;
break;
case Horizontal.Left:
x = - world.x + HorizontalPercent*cwidth;
break;
case Horizontal.Center:
x = HorizontalPercent*cwidth;
break;
default:
break;
}
switch (vertical) {
case Vertical.Top:
y = world.y - VerticalPercent*cheight;
break;
case Vertical.Bottom:
y = - world.y + VerticalPercent*cheight;
break;
case Vertical.Center:
y = VerticalPercent*cheight;
break;
default:
break;
}
transform.position = new Vector3 (x, y, transform.position.z);
}
}
[ContextMenu("GetPositionFromCurrent")]
public void GetPositionFromCurrent(){
Vector3 world = Camera.main.ScreenToWorldPoint(new Vector3 (Camera.main.pixelWidth
, Camera.main.pixelHeight
, 0));
float x = transform.position.x;
float y = transform.position.y;
float cheight = 2f * Camera.main.orthographicSize;
float cwidth = cheight * Camera.main.aspect;
switch (horizontal) {
case Horizontal.Right:
HorizontalPercent = (world.x - x)/cwidth;
break;
case Horizontal.Left:
HorizontalPercent = (world.x + x)/cwidth;
break;
case Horizontal.Center:
HorizontalPercent = x/cwidth;
break;
default:
break;
}
switch (vertical) {
case Vertical.Top:
VerticalPercent = (world.y - y)/cheight;
break;
case Vertical.Bottom:
VerticalPercent = (world.y + y)/cheight;
break;
case Vertical.Center:
VerticalPercent = (y)/cheight ;
break;
default:
break;
}
}
}