| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class o0UIRawImageTester : MonoBehaviour
- {
- static HashSet<o0UIRawImageTester> Testers = new HashSet<o0UIRawImageTester>();
- 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<RawImage>();
- var rect = GetComponent<RectTransform>().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<Color, Color[]> ColorBuffer = new Dictionary<Color, Color[]>();
- 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));
- }
- }
|