| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | public class UIChargeWheel : MonoBehaviour |
| | 7 | | { |
| | 8 | | Material _material; |
| | 9 | | Image _img; |
| 0 | 10 | | public float speed = 1; |
| | 11 | |
|
| | 12 | | public Color[] colors; |
| | 13 | | int colorIndex = 0; |
| | 14 | |
|
| | 15 | |
|
| | 16 | | float head; |
| | 17 | | float tail; |
| | 18 | |
|
| | 19 | | void Start() |
| | 20 | | { |
| 0 | 21 | | _img = GetComponent<Image>(); |
| | 22 | |
|
| 0 | 23 | | if (_img.maskable) |
| | 24 | | { |
| 0 | 25 | | _material = _img.materialForRendering; |
| 0 | 26 | | } |
| | 27 | | else |
| | 28 | | { |
| 0 | 29 | | _material = new Material(_img.material); |
| 0 | 30 | | _img.material = _material; |
| | 31 | | } |
| | 32 | |
|
| 0 | 33 | | _material.SetColor("_color01", colors[colorIndex]); |
| | 34 | |
|
| 0 | 35 | | head = Random.Range(0, 0.15f); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | |
|
| | 39 | | void Update() |
| | 40 | | { |
| 0 | 41 | | float tempSpeedH = Mathf.Abs(0.5f + head) * speed; |
| 0 | 42 | | float tempSpeedT = Mathf.Abs(0.5f + tail) * speed; |
| | 43 | |
|
| 0 | 44 | | if (head < 1) |
| | 45 | | { |
| 0 | 46 | | head += tempSpeedH * Time.deltaTime; |
| 0 | 47 | | head = Mathf.Clamp01(head); |
| 0 | 48 | | _material.SetFloat("_fillHead", head); |
| 0 | 49 | | } |
| 0 | 50 | | else if(tail < 1) |
| | 51 | | { |
| 0 | 52 | | tail += tempSpeedT * Time.deltaTime; |
| 0 | 53 | | tail = Mathf.Clamp01(tail); |
| 0 | 54 | | _material.SetFloat("_fillTail", tail); |
| 0 | 55 | | } |
| | 56 | | else |
| | 57 | | { |
| 0 | 58 | | head = 0; |
| 0 | 59 | | tail = 0; |
| | 60 | |
|
| 0 | 61 | | _material.SetFloat("_fillHead", head); |
| 0 | 62 | | _material.SetFloat("_fillTail", tail); |
| | 63 | |
|
| 0 | 64 | | ColorChange(); |
| | 65 | | } |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | void ColorChange() |
| | 69 | | { |
| 0 | 70 | | if(colorIndex < colors.Length - 1) |
| | 71 | | { |
| 0 | 72 | | colorIndex++; |
| 0 | 73 | | } |
| | 74 | | else |
| | 75 | | { |
| 0 | 76 | | colorIndex = 0; |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | _material.SetColor("_color01", colors[colorIndex]); |
| 0 | 80 | | } |
| | 81 | | } |