1
Fork 0
mirror of https://github.com/Steffo99/better-tee.git synced 2024-11-22 15:24:18 +00:00
better-tee/Assets/Code/DrawableFrame.cs

76 lines
2 KiB
C#
Raw Normal View History

2019-09-15 22:28:36 +00:00
using UnityEngine;
2019-09-10 15:01:29 +00:00
public class DrawableFrame : MonoBehaviour
{
2019-09-11 13:13:31 +00:00
[Header("Configuration")]
2019-09-10 15:01:29 +00:00
public Vector2Int resolution;
2019-09-11 13:13:31 +00:00
public Color startingColor = Color.white;
2019-09-11 13:51:05 +00:00
[Header("State")]
2019-09-14 16:30:16 +00:00
public bool interactable = false;
2019-09-11 13:51:05 +00:00
public bool hasChanged = false;
2019-09-11 13:13:31 +00:00
[Header("References")]
2019-09-11 13:51:05 +00:00
protected Texture2D texture = null;
protected Sprite sprite = null;
2019-09-11 14:12:59 +00:00
protected SpriteRenderer spriteRenderer;
2019-09-10 15:01:29 +00:00
public Rect Bounds {
get {
return new Rect(transform.position.x - (transform.localScale.x / 2),
transform.position.y - (transform.localScale.y / 2),
transform.localScale.x,
transform.localScale.y);
}
}
protected void Start()
{
texture = new Texture2D(resolution.x, resolution.y);
if(resolution.x <= 128 || resolution.y <= 128) {
texture.filterMode = FilterMode.Point;
}
else {
texture.filterMode = FilterMode.Trilinear;
}
2019-09-11 13:13:31 +00:00
Color[] colors = texture.GetPixels();
for(int i = 0; i < colors.Length; i++) {
colors[i] = startingColor;
}
texture.SetPixels(colors);
texture.Apply();
spriteRenderer = GetComponent<SpriteRenderer>();
sprite = Sprite.Create(texture, new Rect(0, 0, resolution.x, resolution.y), new Vector2(0.5f, 0.5f), resolution.x);
spriteRenderer.sprite = sprite;
2019-09-10 15:01:29 +00:00
}
2019-09-11 13:51:05 +00:00
public Color[] GetPixels() {
return texture.GetPixels();
}
public void SetPixels(Color[] colors) {
2019-09-14 16:30:16 +00:00
if(interactable) {
2019-09-11 13:51:05 +00:00
texture.SetPixels(colors);
hasChanged = true;
}
}
2019-09-11 14:12:59 +00:00
public byte[] ToPNG() {
return texture.EncodeToPNG();
}
2019-09-11 13:51:05 +00:00
protected void Update() {
if(hasChanged) {
texture.Apply();
}
}
2019-09-10 15:01:29 +00:00
protected void OnDrawGizmos()
{
Gizmos.DrawWireCube(transform.position, transform.localScale);
}
}