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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
| using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using NextFramework;
public class SCold : MaskableGraphic, IPointerClickHandler { [SerializeField] Sprite m_Sprite; [SerializeField] Origin m_FillOrigin = Origin.Right; [Tooltip("是否是顺时针出现")] [SerializeField] bool m_FillClockwise = true; [Tooltip("冷却时间")] [SerializeField] float m_ColdTime = 3f; [Range(0, 1)] [SerializeField] float m_FillAmount;
[SerializeField] ButtonClickEvent m_OnPositiveClick = new ButtonClickEvent(); [SerializeField] ButtonClickEvent m_OnNegativeClick = new ButtonClickEvent();
static readonly Vector3[] s_Xy = new Vector3[4]; static readonly Vector3[] s_Uv = new Vector3[4]; CoroutineTween.FloatTween m_Tween;
public Sprite sprite { get { return m_Sprite; } set { if (m_Sprite == value) return; m_Sprite = value; SetMaterialDirty(); } } public float fillAmount { get { return m_FillAmount; } set { if (m_FillAmount == value) return; m_FillAmount = value; SetVerticesDirty(); } }
public override Texture mainTexture { get { if (m_Sprite == null) { return material?.mainTexture ?? s_WhiteTexture; } return m_Sprite.texture; } }
public override void SetNativeSize() { if (m_Sprite != null) { float w = m_Sprite.rect.width; float h = m_Sprite.rect.height; rectTransform.anchorMax = rectTransform.anchorMin; rectTransform.sizeDelta = new Vector2(w, h); SetAllDirty(); } } protected override void OnPopulateMesh(VertexHelper vh) { if (m_Sprite == null) return;
Vector4 v = GetDrawingDimensions(true); var uv = (m_Sprite != null) ? UnityEngine.Sprites.DataUtility.GetOuterUV(m_Sprite) : Vector4.zero;
Color32 gery = new Color(color.r * 0.299f, color.g * 0.587f, color.b * 0.114f);
vh.Clear(); AddQuad(vh, new Vector3(v.x, v.y), new Vector3(v.z, v.w), Color.grey, new Vector2(uv.x, uv.y), new Vector2(uv.z, uv.w)); if (m_FillAmount < 0.001f) return;
float tx0 = uv.x; float ty0 = uv.y; float tx1 = uv.z; float ty1 = uv.w;
s_Xy[0] = new Vector2(v.x, v.y); s_Xy[1] = new Vector2(v.x, v.w); s_Xy[2] = new Vector2(v.z, v.w); s_Xy[3] = new Vector2(v.z, v.y);
s_Uv[0] = new Vector2(tx0, ty0); s_Uv[1] = new Vector2(tx0, ty1); s_Uv[2] = new Vector2(tx1, ty1); s_Uv[3] = new Vector2(tx1, ty0);
float fx0, fx1, fy0, fy1; bool temp = false; for (int corner = 0; corner < 4; ++corner) { temp = corner < 2; fx0 = temp ? 0 : 0.5f; fx1 = temp ? 0.5f : 1f;
temp = corner == 0 || corner == 3; fy0 = temp ? 0 : 0.5f; fy1 = temp ? 0.5f : 1f;
s_Xy[0].x = Mathf.Lerp(v.x, v.z, fx0); s_Xy[1].x = s_Xy[0].x; s_Xy[2].x = Mathf.Lerp(v.x, v.z, fx1); s_Xy[3].x = s_Xy[2].x;
s_Xy[0].y = Mathf.Lerp(v.y, v.w, fy0); s_Xy[1].y = Mathf.Lerp(v.y, v.w, fy1); s_Xy[2].y = s_Xy[1].y; s_Xy[3].y = s_Xy[0].y;
s_Uv[0].x = Mathf.Lerp(tx0, tx1, fx0); s_Uv[1].x = s_Uv[0].x; s_Uv[2].x = Mathf.Lerp(tx0, tx1, fx1); s_Uv[3].x = s_Uv[2].x;
s_Uv[0].y = Mathf.Lerp(ty0, ty1, fy0); s_Uv[1].y = Mathf.Lerp(ty0, ty1, fy1); s_Uv[2].y = s_Uv[1].y; s_Uv[3].y = s_Uv[0].y;
float val = m_FillClockwise ? m_FillAmount * 4f - ((corner + (int)m_FillOrigin) % 4) : m_FillAmount * 4f - (3 - ((corner + (int)m_FillOrigin) % 4));
if (RadialCut(s_Xy, s_Uv, Mathf.Clamp01(val), m_FillClockwise, ((corner + 2) % 4))) AddQuad(vh, s_Xy, color, s_Uv); } }
private Vector4 GetDrawingDimensions(bool shouldPreserveAspect) { var padding = m_Sprite == null ? Vector4.zero : UnityEngine.Sprites.DataUtility.GetPadding(m_Sprite); var size = m_Sprite == null ? Vector2.zero : new Vector2(m_Sprite.rect.width, m_Sprite.rect.height);
Rect r = GetPixelAdjustedRect();
int spriteW = Mathf.RoundToInt(size.x); int spriteH = Mathf.RoundToInt(size.y);
var v = new Vector4( padding.x / spriteW, padding.y / spriteH, (spriteW - padding.z) / spriteW, (spriteH - padding.w) / spriteH);
if (shouldPreserveAspect && size.sqrMagnitude > 0.0f) { PreserveSpriteAspectRatio(ref r, size); }
v = new Vector4( r.x + r.width * v.x, r.y + r.height * v.y, r.x + r.width * v.z, r.y + r.height * v.w );
return v; } private void PreserveSpriteAspectRatio(ref Rect rect, Vector2 spriteSize) { var spriteRatio = spriteSize.x / spriteSize.y; var rectRatio = rect.width / rect.height;
if (spriteRatio > rectRatio) { var oldHeight = rect.height; rect.height = rect.width * (1.0f / spriteRatio); rect.y += (oldHeight - rect.height) * rectTransform.pivot.y; } else { var oldWidth = rect.width; rect.width = rect.height * spriteRatio; rect.x += (oldWidth - rect.width) * rectTransform.pivot.x; } }
static void AddQuad(VertexHelper vertexHelper, Vector3[] quadPositions, Color32 color, Vector3[] quadUVs) { int startIndex = vertexHelper.currentVertCount;
for (int i = 0; i < 4; ++i) vertexHelper.AddVert(quadPositions[i], color, quadUVs[i]);
vertexHelper.AddTriangle(startIndex, startIndex + 1, startIndex + 2); vertexHelper.AddTriangle(startIndex + 2, startIndex + 3, startIndex); } static void AddQuad(VertexHelper vertexHelper, Vector2 posMin, Vector2 posMax, Color32 color, Vector2 uvMin, Vector2 uvMax) { var quadArray = new Vector3[4] { new Vector3(posMin.x, posMin.y, 0), new Vector3(posMin.x, posMax.y, 0), new Vector3(posMax.x, posMax.y, 0), new Vector3(posMax.x, posMin.y, 0), }; var uvArray = new Vector3[4] { new Vector2(uvMin.x, uvMin.y), new Vector2(uvMin.x, uvMax.y), new Vector2(uvMax.x, uvMax.y), new Vector2(uvMax.x, uvMin.y)}; AddQuad(vertexHelper, quadArray, color, uvArray); }
static bool RadialCut(Vector3[] xy, Vector3[] uv, float fill, bool invert, int corner) { if (fill < 0.001f) return false;
if ((corner & 1) == 1) invert = !invert;
if (!invert && fill > 0.999f) return true;
float angle = Mathf.Clamp01(fill); if (invert) angle = 1f - angle; angle *= 90f * Mathf.Deg2Rad;
float cos = Mathf.Cos(angle); float sin = Mathf.Sin(angle);
RadialCut(xy, cos, sin, invert, corner); RadialCut(uv, cos, sin, invert, corner); return true; }
static void RadialCut(Vector3[] xy, float cos, float sin, bool invert, int corner) { int i0 = corner; int i1 = ((corner + 1) % 4); int i2 = ((corner + 2) % 4); int i3 = ((corner + 3) % 4);
if ((corner & 1) == 1) { if (sin > cos) { cos /= sin; sin = 1f;
if (invert) { xy[i1].x = Mathf.Lerp(xy[i0].x, xy[i2].x, cos); xy[i2].x = xy[i1].x; } } else if (cos > sin) { sin /= cos; cos = 1f;
if (!invert) { xy[i2].y = Mathf.Lerp(xy[i0].y, xy[i2].y, sin); xy[i3].y = xy[i2].y; } } else { cos = 1f; sin = 1f; }
if (!invert) xy[i3].x = Mathf.Lerp(xy[i0].x, xy[i2].x, cos); else xy[i1].y = Mathf.Lerp(xy[i0].y, xy[i2].y, sin); } else { if (cos > sin) { sin /= cos; cos = 1f;
if (!invert) { xy[i1].y = Mathf.Lerp(xy[i0].y, xy[i2].y, sin); xy[i2].y = xy[i1].y; } } else if (sin > cos) { cos /= sin; sin = 1f;
if (invert) { xy[i2].x = Mathf.Lerp(xy[i0].x, xy[i2].x, cos); xy[i3].x = xy[i2].x; } } else { cos = 1f; sin = 1f; }
if (invert) xy[i3].y = Mathf.Lerp(xy[i0].y, xy[i2].y, sin); else xy[i1].x = Mathf.Lerp(xy[i0].x, xy[i2].x, cos); } }
#region IPointerClickHandler [System.Serializable] public class ButtonClickEvent : UnityEngine.Events.UnityEvent { } public void OnPointerClick(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left) return;
Press(); } void Press() { if (!IsActive() || !raycastTarget) return;
if (fillAmount >= 0.9999f) { Debug.Log("Positive state"); fillAmount = 0; m_OnPositiveClick?.Invoke(); } else { Debug.Log("Negative state"); m_OnNegativeClick?.Invoke(); } } #endregion
#region Enum public enum Origin { Bottom, Right, Top, Left, } #endregion }
|