-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicSprite.cs
120 lines (102 loc) · 2.73 KB
/
DynamicSprite.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
#if UNITY_EDITOR
[ExecuteInEditMode]
public class DynamicSprite : MonoBehaviour
{
[Range(0.5f, 25f)] //change this for larger objects
public float objectHeight, objectWidth;
private SpriteRenderer SR;
public List<DynamicSubSprite> subManagers = new List<DynamicSubSprite>();
private bool validating;
//private void Awake()
//{
// Debug.Log("Awake has been called");
// SR = this.gameObject.GetComponent<SpriteRenderer>();
// objectHeight = SR.size.y;
// objectWidth = SR.size.x;
// storeSize();
// //_subManagers = gameObject.GetComponentsInChildren<DynamicSubSprite>();
// //foreach(DynamicSubSprite manager in _subManagers)
// //{
// // subManagers.Add(manager);
// //}
//}
private void OnEnable()
{
//Debug.Log("OnEnable has been called");
SR = this.gameObject.GetComponent<SpriteRenderer>();
objectHeight = SR.size.y;
objectWidth = SR.size.x;
if(SR.drawMode != SpriteDrawMode.Tiled)
{
SR.drawMode = SpriteDrawMode.Tiled;
}
}
private void OnDestroy()
{
foreach(DynamicSubSprite subManager in subManagers)
{
DestroyImmediate(subManager);
}
}
private void OnValidate()
{
//Debug.Log("OnValidate Called");
if (Application.isEditor)
{
EditorApplication.update += _OnValidate;
return;
}
}
private void _OnValidate()
{
EditorApplication.update -= _OnValidate;
setSize();
changeSubManagerValues();
}
private void changeSubManagerValues()
{
if(subManagers.Count == 0)
{
return;
}
foreach(DynamicSubSprite manager in subManagers)
{
manager.adapt(objectWidth, objectHeight);
}
}
private void setSize()
{
//Debug.Log("Setting Size");
if (SR == null)
{
return;
}
SR.size = new Vector2(objectWidth, objectHeight);
}
public void addMe(DynamicSubSprite subManager)
{
if (!subManagers.Contains(subManager))
{
subManagers.Add(subManager);
}
}
public void removeMe(DynamicSubSprite subManager)
{
if (subManagers.Contains(subManager))
{
subManagers.Remove(subManager);
}
}
//public void prepareForDelete()
//{
// foreach(DynamicSubSprite subManager in subManagers)
// {
// subManager.waitingForDelete = true;
// }
//}
}
#endif //if unity_editor