|
1 |
| -using System; |
| 1 | +/* |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2017 Lewis Johnson |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +*/ |
| 24 | +using System; |
2 | 25 | using System.Collections;
|
3 | 26 | using System.Collections.Generic;
|
4 |
| -using System.Security.Cryptography; |
5 | 27 | using UnityEngine;
|
6 | 28 | using UnityEngine.UI;
|
7 | 29 | using Random = System.Random;
|
8 | 30 |
|
9 |
| - |
10 | 31 | namespace Assets.ScreenSpace.FizzleFade.Scripts {
|
11 | 32 |
|
12 | 33 | public class FizzleFade : MonoBehaviour {
|
13 | 34 |
|
14 |
| - public GameObject Image; |
| 35 | + public Texture2D FizzleTexture2D; |
| 36 | + private bool _isFilled; |
| 37 | + |
15 | 38 | public void Start() {
|
16 | 39 | Fizzle(new Color(255, 0, 0, 255));
|
17 | 40 | }
|
18 | 41 |
|
19 |
| - public void StartFizzleFade(Color pixelColour) { |
20 |
| - |
21 |
| - int width = (int)Math.Ceiling(GetComponent<RectTransform>().sizeDelta.x); |
22 |
| - int height = (int)Math.Ceiling(GetComponent<RectTransform>().sizeDelta.y); |
23 |
| - |
24 |
| - var xAxisFilled = new List<int>(); |
25 |
| - var yAxisFilled = new List<int>(); |
26 |
| - var rand = new Random(); |
27 |
| - |
28 |
| - while (xAxisFilled.Count != width && yAxisFilled.Count != height) { |
29 |
| - |
30 |
| - int x = rand.Next(0, width); |
31 |
| - if (xAxisFilled.Contains(x)) |
32 |
| - continue; |
33 |
| - |
34 |
| - int y = rand.Next(0, height); |
35 |
| - if (yAxisFilled.Contains(y)) |
36 |
| - continue; |
37 |
| - |
38 |
| - var img = Instantiate(Image, transform); |
39 |
| - img.GetComponent<Image>().color = pixelColour; |
40 |
| - img.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y); |
41 |
| - |
42 |
| - xAxisFilled.Add(x); |
43 |
| - yAxisFilled.Add(y); |
44 |
| - |
45 |
| - } |
46 |
| - } |
47 |
| - |
48 |
| - public void StartFizzleFade2(Color pixelColour) { |
49 |
| - |
50 |
| - int width = (int)Math.Ceiling(GetComponent<RectTransform>().sizeDelta.x) / 100; |
51 |
| - int height = (int)Math.Ceiling(GetComponent<RectTransform>().sizeDelta.y) / 100; |
52 |
| - |
53 |
| - var xAxisFilled = new List<int>(width); |
54 |
| - var xrandom = new Random(); |
55 |
| - xAxisFilled.Add(0); |
56 |
| - for (var i = 1; i < width; i++) { |
57 |
| - var swap = xrandom.Next(i - 1); |
58 |
| - xAxisFilled.Add(xAxisFilled[swap]); |
59 |
| - xAxisFilled[swap] = i; |
60 |
| - } |
61 |
| - |
62 |
| - var yAxisFilled = new List<int>(height); |
63 |
| - var random = new Random(); |
64 |
| - yAxisFilled.Add(0); |
65 |
| - for (var i = 1; i < height; i++) { |
66 |
| - var swap = random.Next(i - 1); |
67 |
| - yAxisFilled.Add(yAxisFilled[swap]); |
68 |
| - yAxisFilled[swap] = i; |
69 |
| - } |
70 |
| - |
71 |
| - for (int x = 0; x < xAxisFilled.Count; x++) { |
72 |
| - for (int y = 0; y < yAxisFilled.Count; y++) { |
73 |
| - var img = Instantiate(Image, transform); |
74 |
| - img.GetComponent<Image>().color = pixelColour; |
75 |
| - img.GetComponent<RectTransform>().anchoredPosition = new Vector2(xAxisFilled[x], yAxisFilled[y]); |
76 |
| - } |
77 |
| - |
78 |
| - } |
79 |
| - } |
80 |
| - |
81 | 42 | public void Fizzle(Color pixelColour) {
|
82 | 43 |
|
83 | 44 | int width = (int)Math.Ceiling(GetComponent<RectTransform>().sizeDelta.x);
|
84 | 45 | int height = (int)Math.Ceiling(GetComponent<RectTransform>().sizeDelta.y);
|
| 46 | + FizzleTexture2D = new Texture2D(height, width, TextureFormat.ARGB32, false); |
85 | 47 |
|
86 |
| - var xList = new List<int>(); |
87 |
| - var yList = new List<int>(); |
88 |
| - for (int i = 0; i <= ((int)Math.Round(width / 10.0)) * 10; i += 10) { |
89 |
| - xList.Add(i); |
90 |
| - } |
| 48 | + var fillColorArray = FizzleTexture2D.GetPixels(); |
91 | 49 |
|
92 |
| - for (int i = 0; i <= ((int)Math.Round(height / 10.0)) * 10; i += 10) { |
93 |
| - yList.Add(i); |
| 50 | + for (var i = 0; i < fillColorArray.Length; ++i) { |
| 51 | + fillColorArray[i] = new Color(0, 0, 0, 0); |
94 | 52 | }
|
95 | 53 |
|
96 |
| - Shuffle(xList); |
97 |
| - Shuffle(yList); |
| 54 | + FizzleTexture2D.SetPixels(fillColorArray); |
| 55 | + FizzleTexture2D.Apply(); |
| 56 | + GetComponent<Image>().material.mainTexture = FizzleTexture2D; |
98 | 57 |
|
99 |
| - for (int x = 0; x < xList.Count; x++) { |
100 |
| - for (int y = 0; y < yList.Count; y++) { |
101 |
| - var img = Instantiate(Image, transform); |
102 |
| - img.GetComponent<Image>().color = pixelColour; |
103 |
| - img.GetComponent<RectTransform>().anchoredPosition = new Vector2(xList[x], -yList[y]); |
104 |
| - img.SetActive(false); |
105 |
| - StartCoroutine(WaitAndPrint(img, (x * y) / 1000)); |
| 58 | + for (int x = 0; x <= width; x++) { |
| 59 | + for (int y = 0; y <= height; y++) { |
| 60 | + //StartCoroutine(WaitAndPrint(x, y, pixelColour, 1)); |
| 61 | + //FizzleTexture2D.SetPixel(x, y, pixelColour); |
106 | 62 | }
|
107 |
| - |
108 | 63 | }
|
109 | 64 |
|
110 |
| - //var child = new List<int>(transform.childCount); |
111 |
| - //var xrandom = new Random(); |
112 |
| - //child.Add(0); |
113 |
| - //for (var i = 1; i < width; i++) { |
114 |
| - // var swap = xrandom.Next(i - 1); |
115 |
| - // child.Add(child[swap]); |
116 |
| - // child[swap] = i; |
117 |
| - //} |
118 |
| - |
119 |
| - //for (int i = 0; i < child.Count; i++) { |
120 |
| - // StartCoroutine(WaitAndPrint(child[i], 1)); |
121 |
| - //} |
122 |
| - |
| 65 | + |
123 | 66 | }
|
124 | 67 |
|
125 | 68 |
|
126 |
| - private IEnumerator WaitAndPrint(GameObject k, float waitTime) { |
| 69 | + private IEnumerator WaitAndPrint(int x, int y, Color pixelColour, float waitTime) { |
| 70 | + |
| 71 | + FizzleTexture2D.SetPixel(x, y, pixelColour); |
| 72 | + |
127 | 73 | yield return new WaitForSeconds(waitTime);
|
128 |
| - k.SetActive(true); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + public void FixedUpdate() |
| 78 | + { |
| 79 | + if (!_isFilled) |
| 80 | + { |
| 81 | + FizzleTexture2D.Apply(); |
| 82 | + } |
| 83 | + |
129 | 84 | }
|
130 | 85 |
|
131 | 86 | public static void Shuffle<T>(IList<T> list) {
|
|
0 commit comments