-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicSubSprite.cs
224 lines (183 loc) · 6.58 KB
/
DynamicSubSprite.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
#if UNITY_EDITOR
[ExecuteInEditMode]
public class DynamicSubSprite : MonoBehaviour
{
public enum AnchorPoint{TopLeft, TopCenter, TopRight, RightCenter, BottomLeft, BottomCenter, BottomRight, LeftCenter};
[SerializeField]private AnchorPoint Anchor;
public enum ElementType { Stretch, Static };
[SerializeField] private ElementType Type;
public float horizontalOffset, verticalOffset;
private SpriteRenderer SR;
private GameObject GO;
private float parentWidth, parentHeight;
private float leftDis, topDis, rightDis, bottomDis;
private Vector3 prevPos;
[HideInInspector] public bool inPrefabStage = false;
private void Awake()
{
//Debug.Log("Setting GO");
GO = this.gameObject;
SR = GO.GetComponent<SpriteRenderer>();
}
private void OnEnable()
{
GO.GetComponentInParent<DynamicSprite>().addMe(this);
prevPos = GO.transform.localPosition;
askForParentSize();
checkDistances();
}
void OnDisable()
{
//if (UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage() != null && GO.transform.parent != null)
//{
//}
try
{
GO.GetComponentInParent<DynamicSprite>().removeMe(this);
}
catch
{
return;
}
}
public void adapt(float _parentWidth, float _parentHeight)
{
//Debug.Log("Adapting...");
if(GO == null)
{
return;
}
if (GO.transform.localPosition != prevPos)
{
prevPos = GO.transform.localPosition;
checkDistances();
}
parentWidth = _parentWidth;
parentHeight = _parentHeight;
if (Type == ElementType.Static) //don't want to edit scale if static
{
if (SR.drawMode != SpriteDrawMode.Simple)
{
SR.drawMode = SpriteDrawMode.Simple;
}
GO.transform.localPosition = findNewPos();
checkDistances();
}
if (Type == ElementType.Stretch) //want to edit width dynamically
{
if(SR.drawMode != SpriteDrawMode.Tiled)
{
SR.drawMode = SpriteDrawMode.Tiled;
}
if(Anchor == AnchorPoint.TopCenter || Anchor == AnchorPoint.TopLeft || Anchor == AnchorPoint.TopRight)
{
GO.transform.localPosition = new Vector2(0, (parentHeight / 2) + verticalOffset);
SR.size = new Vector2((parentWidth / GO.transform.localScale.x) + horizontalOffset, SR.size.y);
}
else if(Anchor == AnchorPoint.LeftCenter)
{
GO.transform.localPosition = new Vector2((-parentWidth / 2) + horizontalOffset, 0);
SR.size = new Vector2(SR.size.x,(parentHeight / GO.transform.localScale.y) + verticalOffset);
}
else if (Anchor == AnchorPoint.RightCenter)
{
GO.transform.localPosition = new Vector2((parentWidth / 2) + horizontalOffset, 0);
SR.size = new Vector2(SR.size.x, (parentHeight / GO.transform.localScale.y) + verticalOffset);
}
else if (Anchor == AnchorPoint.BottomCenter || Anchor == AnchorPoint.BottomLeft || Anchor == AnchorPoint.BottomRight)
{
GO.transform.localPosition = new Vector2(0, (-parentHeight / 2) + verticalOffset);
SR.size = new Vector2((parentWidth / GO.transform.localScale.x) + horizontalOffset, SR.size.y);
}
}
return;
}
private Vector2 findNewPos()
{
if(Anchor == AnchorPoint.TopLeft)
{
float posX = (-parentWidth / 2) - (leftDis);
float posY = (parentHeight / 2) - (topDis);
return new Vector2(posX, posY);
}
else if (Anchor == AnchorPoint.TopRight)
{
float posX = (parentWidth / 2) - (rightDis);
float posY = (parentHeight / 2) - (topDis);
return new Vector2(posX, posY);
}
else if (Anchor == AnchorPoint.BottomRight)
{
float posX = (parentWidth / 2) - (rightDis);
float posY = (-parentHeight / 2) - (bottomDis);
return new Vector2(posX, posY);
}
else if (Anchor == AnchorPoint.BottomLeft)
{
float posX = (-parentWidth / 2) - (leftDis);
float posY = (-parentHeight / 2) - (bottomDis);
return new Vector2(posX, posY);
}
else if (Anchor == AnchorPoint.BottomCenter)
{
float posX = prevPos.x;
float posY = (-parentHeight / 2) - (bottomDis);
return new Vector2(posX, posY);
}
else if (Anchor == AnchorPoint.TopCenter)
{
float posX = prevPos.x;
float posY = (parentHeight / 2) - (topDis);
return new Vector2(posX, posY);
}
else if (Anchor == AnchorPoint.LeftCenter)
{
float posX = (parentWidth / 2) - (leftDis);
float posY = prevPos.y;
return new Vector2(posX, posY);
}
else if (Anchor == AnchorPoint.RightCenter)
{
float posX = (parentWidth / 2) - (rightDis);
float posY = prevPos.y;
return new Vector2(posX, posY);
}
return new Vector2(0,0);
//float posX = (parentWidth / 2) - findDifference().x;
//float posY = (parentHeight / 2) - findDifference().y;
} //find new position after parent has been edited
private void checkDistances() //update distances
{
leftDis = (-parentWidth / 2) - (GO.transform.localPosition.x);
rightDis = (parentWidth / 2) - (GO.transform.localPosition.x);
topDis = (parentHeight / 2) - (GO.transform.localPosition.y);
bottomDis = (-parentHeight / 2) - (GO.transform.localPosition.y);
}
private void askForParentSize()
{
parentWidth = GO.GetComponentInParent<DynamicSprite>().objectWidth;
parentHeight = GO.GetComponentInParent<DynamicSprite>().objectHeight;
}
public void flip(string axis)
{
if(axis == "X")
{
//Debug.Log("Flipped X");
SR.flipX = !SR.flipX;
}
else if (axis == "Y")
{
//Debug.Log("Flipped Y");
SR.flipY = !SR.flipY;
}
}
private void changeStage()
{
inPrefabStage = false;
}
}
#endif //if unity_editor