| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | public class UIRainbowController : MonoBehaviour |
| | 7 | | { |
| | 8 | | Material targetMat; |
| | 9 | | public Image img; |
| | 10 | | public Texture mask; |
| | 11 | | public bool useTexture; |
| | 12 | | public Texture gradientTexture; |
| | 13 | | public Gradient gradient; |
| | 14 | | public Vector2 speed; |
| | 15 | | [Range(-360, 360)] |
| | 16 | | public float rotation; |
| | 17 | | [Range(0,1)] |
| 126 | 18 | | public float fill = 1f; |
| | 19 | |
|
| | 20 | | public enum rainbowT {linear, radial}; |
| | 21 | | public enum fillT {left, right, up, down}; |
| | 22 | |
|
| | 23 | | public rainbowT rainbowType; |
| | 24 | | public fillT fillType; |
| | 25 | |
|
| | 26 | | void Start() |
| | 27 | | { |
| 114 | 28 | | if(img.maskable) |
| | 29 | | { |
| 114 | 30 | | targetMat = img.materialForRendering; |
| 114 | 31 | | } |
| | 32 | | else |
| | 33 | | { |
| 0 | 34 | | targetMat = new Material(img.material); |
| 0 | 35 | | img.material = targetMat; |
| | 36 | | } |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | |
|
| | 40 | | void FixedUpdate() |
| | 41 | | { |
| 250 | 42 | | if(targetMat) |
| | 43 | | { |
| 250 | 44 | | SetValues(); |
| | 45 | | } |
| | 46 | |
|
| 250 | 47 | | } |
| | 48 | |
|
| | 49 | | void SetValues() |
| | 50 | | { |
| 250 | 51 | | targetMat.SetTexture("_Mask", mask); |
| 250 | 52 | | targetMat.SetTexture("_Ramp", gradientTexture); |
| 250 | 53 | | targetMat.SetVector("_Speed", speed); |
| 250 | 54 | | targetMat.SetFloat("_Rotation", rotation); |
| 250 | 55 | | targetMat.SetFloat("_Fill", fill); |
| | 56 | |
|
| 250 | 57 | | if (useTexture) |
| | 58 | | { |
| 0 | 59 | | targetMat.SetFloat("_UseTexture", 1); |
| 0 | 60 | | } |
| | 61 | | else |
| | 62 | | { |
| 250 | 63 | | targetMat.SetFloat("_UseTexture", 0); |
| | 64 | | } |
| | 65 | |
|
| 250 | 66 | | targetMat.SetFloat("_GradientMode", ((int)rainbowType)); |
| 250 | 67 | | targetMat.SetFloat("_FillDirection", ((int)fillType)); |
| | 68 | |
|
| 250 | 69 | | targetMat.SetFloat("_ColorAmount", gradient.colorKeys.Length); |
| | 70 | |
|
| 250 | 71 | | float[] tempPos01 = new float[4]; |
| 250 | 72 | | float[] tempPos02 = new float[4]; |
| | 73 | |
|
| 2500 | 74 | | for (int i = 0; i < 4; i++) |
| | 75 | | { |
| 1000 | 76 | | tempPos01[i] = 1; |
| 1000 | 77 | | tempPos02[i] = 1; |
| | 78 | | } |
| | 79 | |
|
| 2500 | 80 | | for (int i = 0; i < gradient.colorKeys.Length; i++) |
| | 81 | | { |
| 1000 | 82 | | targetMat.SetColor("_Color0" + (i + 1).ToString(), gradient.colorKeys[i].color); |
| 1000 | 83 | | if (i <= 3) |
| | 84 | | { |
| 1000 | 85 | | tempPos01[i] = gradient.colorKeys[i].time; |
| 1000 | 86 | | } |
| | 87 | | else |
| | 88 | | { |
| 0 | 89 | | tempPos02[i - 4] = gradient.colorKeys[i].time; |
| | 90 | | } |
| | 91 | | } |
| 250 | 92 | | Vector4 pos01 = new Vector4(tempPos01[0], tempPos01[1], tempPos01[2], tempPos01[3]); |
| 250 | 93 | | Vector4 pos02 = new Vector4(tempPos02[0], tempPos02[1], tempPos02[2], tempPos02[3]); |
| | 94 | |
|
| 250 | 95 | | targetMat.SetVector("_GradientPositions01", pos01); |
| 250 | 96 | | targetMat.SetVector("_GradientPositions02", pos02); |
| 250 | 97 | | } |
| | 98 | | } |