Tanoda
CUIImage.cs
Go to the documentation of this file.
1
3
4using System.Collections.Generic;
5
7{
8 [RequireComponent(typeof(RectTransform))]
9 [RequireComponent(typeof(Image))]
10 [AddComponentMenu("UI/Effects/Extensions/Curly UI Image")]
11 public class CUIImage : CUIGraphic
12 {
13 #region Nature
14
15 public static int SlicedImageCornerRefVertexIdx = 2;
16 public static int FilledImageCornerRefVertexIdx = 0;
17
23 public static int ImageTypeCornerRefVertexIdx(Image.Type _type)
24 {
25 if (_type == Image.Type.Sliced)
26 {
28 }
29 else
30 {
32 }
33 }
34 #endregion
35
36 #region Description
37
38 [Tooltip("For changing the size of the corner for tiled or sliced Image")]
39 [HideInInspector]
40 [SerializeField]
41 public Vector2 cornerPosRatio = Vector2.one * -1; // -1 means unset value
42
43 [HideInInspector]
44 [SerializeField]
45 protected Vector2 oriCornerPosRatio = Vector2.one * -1;
46 public Vector2 OriCornerPosRatio
47 {
48 get
49 {
50 return oriCornerPosRatio;
51 }
52 }
53
54 #endregion
55
57 {
58 get
59 {
60 return (Image)uiGraphic;
61 }
62 }
63
64 #region Configurations
65
66 public override void ReportSet()
67 {
68
69 if (uiGraphic == null)
70 uiGraphic = GetComponent<Image>();
71
72 base.ReportSet();
73 }
74
75 protected override void modifyVertices(List<UIVertex> _verts)
76 {
77 if (!IsActive())
78 return;
79
80 if (UIImage.type == Image.Type.Filled)
81 {
82 Debug.LogWarning("Might not work well Radial Filled at the moment!");
83
84 }
85 else if (UIImage.type == Image.Type.Sliced || UIImage.type == Image.Type.Tiled)
86 {
87 // setting the starting cornerRatio
88 if (cornerPosRatio == Vector2.one * -1)
89 {
90 cornerPosRatio = _verts[ImageTypeCornerRefVertexIdx(UIImage.type)].position;
91 cornerPosRatio.x = (cornerPosRatio.x + rectTrans.pivot.x * rectTrans.rect.width) / rectTrans.rect.width;
92 cornerPosRatio.y = (cornerPosRatio.y + rectTrans.pivot.y * rectTrans.rect.height) / rectTrans.rect.height;
93
95
96 }
97
98 // constraining the corner ratio
99 if (cornerPosRatio.x < 0)
100 {
101 cornerPosRatio.x = 0;
102 }
103 if (cornerPosRatio.x >= 0.5f)
104 {
105 cornerPosRatio.x = 0.5f;
106 }
107 if (cornerPosRatio.y < 0)
108 {
109 cornerPosRatio.y = 0;
110 }
111 if (cornerPosRatio.y >= 0.5f)
112 {
113 cornerPosRatio.y = 0.5f;
114 }
115
116 for (int index = 0; index < _verts.Count; index++)
117 {
118 var uiVertex = _verts[index];
119
120 // finding the horizontal ratio position (0.0 - 1.0) of a vertex
121 float horRatio = (uiVertex.position.x + rectTrans.rect.width * rectTrans.pivot.x) / rectTrans.rect.width;
122 float verRatio = (uiVertex.position.y + rectTrans.rect.height * rectTrans.pivot.y) / rectTrans.rect.height;
123
124 if (horRatio < oriCornerPosRatio.x)
125 {
126 horRatio = Mathf.Lerp(0, cornerPosRatio.x, horRatio / oriCornerPosRatio.x);
127 }
128 else if (horRatio > 1 - oriCornerPosRatio.x)
129 {
130 horRatio = Mathf.Lerp(1 - cornerPosRatio.x, 1, (horRatio - (1 - oriCornerPosRatio.x)) / oriCornerPosRatio.x);
131 }
132 else
133 {
134 horRatio = Mathf.Lerp(cornerPosRatio.x, 1 - cornerPosRatio.x, (horRatio - oriCornerPosRatio.x) / (1 - oriCornerPosRatio.x * 2));
135 }
136
137 if (verRatio < oriCornerPosRatio.y)
138 {
139 verRatio = Mathf.Lerp(0, cornerPosRatio.y, verRatio / oriCornerPosRatio.y);
140 }
141 else if (verRatio > 1 - oriCornerPosRatio.y)
142 {
143 verRatio = Mathf.Lerp(1 - cornerPosRatio.y, 1, (verRatio - (1 - oriCornerPosRatio.y)) / oriCornerPosRatio.y);
144 }
145 else
146 {
147 verRatio = Mathf.Lerp(cornerPosRatio.y, 1 - cornerPosRatio.y, (verRatio - oriCornerPosRatio.y) / (1 - oriCornerPosRatio.y * 2));
148 }
149
150 uiVertex.position.x = horRatio * rectTrans.rect.width - rectTrans.rect.width * rectTrans.pivot.x;
151 uiVertex.position.y = verRatio * rectTrans.rect.height - rectTrans.rect.height * rectTrans.pivot.y;
152 //uiVertex.position.z = pos.z;
153
154 _verts[index] = uiVertex;
155 }
156 }
157
158 base.modifyVertices(_verts);
159
160 }
161
162 #endregion
163
164 }
165}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
System.Drawing.Image Image
Definition: TestScript.cs:37
RectTransform rectTrans
Reference to other objects that are needed by this object.
Definition: CUIGraphic.cs:65
override void modifyVertices(List< UIVertex > _verts)
Definition: CUIImage.cs:75
static int FilledImageCornerRefVertexIdx
Definition: CUIImage.cs:16
static int SlicedImageCornerRefVertexIdx
Definition: CUIImage.cs:15
static int ImageTypeCornerRefVertexIdx(Image.Type _type)
For sliced and filled image only.
Definition: CUIImage.cs:23
override void ReportSet()
Check, prepare and set everything needed.
Definition: CUIImage.cs:66
Credit Erdener Gonenc - @PixelEnvision.