using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class o0UIRawImageTester : MonoBehaviour { static HashSet Testers = new HashSet(); public static void UpdateAllOffset() { foreach (var i in Testers) i.UpdateOffset(); } RawImage Image; Texture2D Texture; int Offset = 0; // Start is called before the first frame update void Awake() { Testers.Add(this); Image = GetComponent(); var rect = GetComponent().rect; Texture = new Texture2D((int)rect.width, (int)rect.height); Image.texture = Texture; ColorBuffer[new Color(1, 0, 0)] = new Color[] { new Color(1, 0, 0) , new Color(1, 0, 0) }; //Debug.Log(ColorBuffer[new Color(1, 0, 0)].Length); } // Update is called once per frame void Update() { } static Dictionary ColorBuffer = new Dictionary(); static Color[] GetColorArray(Color color, int length) { if (ColorBuffer.ContainsKey(color)) { if (ColorBuffer[color].Length >= length) return ColorBuffer[color]; else ColorBuffer.Remove(color); } var c = new Color[length]; for (var i = 0; i < c.Length; ++i) c[i] = color; ColorBuffer.Add(color, c); return c; } public void DrawPoint(int height, Color color) { if (height > Texture.height) height = Texture.height; else if (height < 0) height = 0; Texture.SetPixel(Offset, height, color); Texture.Apply(); } public void DrawLine(int height, Color color) { if (height > Texture.height) height = Texture.height; else if (height < 0) height = 0; Texture.SetPixels(Offset, 0, 1, height, GetColorArray(color, height)); Texture.Apply(); } public void DrawPoint(float height, Color color) { DrawPoint((int)(height * Texture.height), color); } public void DrawLine(float height, Color color) { DrawLine((int)(height * Texture.height), color); } public virtual void UpdateOffset() { ++Offset; if (Offset >= Texture.width) Offset = 0; var rect = Image.uvRect; rect.x = (float)(Offset+1) / Texture.width; Image.uvRect = rect; DrawLine(1f,new Color(1,1,1,0.5f)); } }