-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberLabel.cs
More file actions
88 lines (78 loc) · 2.38 KB
/
NumberLabel.cs
File metadata and controls
88 lines (78 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Collections.Generic;
using UnityEngine;
namespace Agricosmic.Utilities
{
/// <summary>
/// Represents a single-digit number from sprites with optional outlining
/// </summary>
public class NumberLabel : MonoBehaviour
{
[Tooltip("Sprites in order [0-9]")]
[SerializeField] private List<Sprite> _numbers;
[Tooltip("Sprites in order [0-9] outlined")]
[SerializeField] private List<Sprite> _outlinedNumbers;
[Tooltip("The value to show")]
[SerializeField] private int _value;
[Tooltip("Use the outlined sprites?")]
[SerializeField] private bool _outline;
[Tooltip("The color of the text")]
[SerializeField] private Color _color;
[Tooltip("Reference to the renderer to change. Located on start-up if null")]
[SerializeField] private SpriteRenderer _sprite;
private void Awake()
{
if (_sprite == null) _sprite = GetComponent<SpriteRenderer>();
}
private void UpdateSprite()
{
_sprite.sprite = _outline ? _outlinedNumbers[_value] : _numbers[_value];
_sprite.color = _color;
}
/// <summary>
/// Quick shortcut to hiding the sprite
/// </summary>
public void HideLabel()
{
_sprite.gameObject.SetActive(false);
}
/// <summary>
/// Quick shortcut to showing the sprite
/// </summary>
public void ShowLabel()
{
_sprite.gameObject.SetActive(true);
}
/// <summary>
/// Sets the outline mode
/// </summary>
/// <param name="isOutlined">yay or nay</param>
public void SetOutlined(bool isOutlined)
{
_outline = isOutlined;
}
/// <summary>
/// Set the value to represent
/// </summary>
/// <param name="value"></param>
public void SetValue(int value)
{
_value = value;
UpdateSprite();
}
public void SetColor(Color color)
{
_color = color;
UpdateSprite();
}
public void SetOpacity(float opacity)
{
_color.a = opacity;
UpdateSprite();
}
private void Update()
{
if (!Application.isPlaying)
UpdateSprite();
}
}
}