com.unity.uiextensions/Runtime/Scripts/Controls/SelectionBox/ExampleSelectable.cs

99 lines
2.5 KiB
C#
Raw Normal View History

/// Original Credit Korindian
/// Sourced from - http://forum.unity3d.com/threads/rts-style-drag-selection-box.265739/
/// Updated Credit BenZed
/// Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
namespace UnityEngine.UI.Extensions
{
public class ExampleSelectable : MonoBehaviour, IBoxSelectable
{
#region Implemented members of IBoxSelectable
bool _selected = false;
public bool selected
{
get
{
return _selected;
}
set
{
_selected = value;
}
}
bool _preSelected = false;
public bool preSelected
{
get
{
return _preSelected;
}
set
{
_preSelected = value;
}
}
#endregion
//We want the test object to be either a UI element, a 2D element or a 3D element, so we'll get the appropriate components
SpriteRenderer spriteRenderer;
Image image;
2023-02-04 19:22:31 +08:00
#if UNITY_2022_1_OR_NEWER
TMPro.TMP_Text text;
#else
Text text;
2023-02-04 19:22:31 +08:00
#endif
void Start()
{
spriteRenderer = transform.GetComponent<SpriteRenderer>();
image = transform.GetComponent<Image>();
2023-02-04 19:22:31 +08:00
#if UNITY_2022_1_OR_NEWER
text = transform.GetComponent<TMPro.TMP_Text>();
#else
text = transform.GetComponent<Text>();
2023-02-04 19:22:31 +08:00
#endif
}
void Update()
{
//What the game object does with the knowledge that it is selected is entirely up to it.
//In this case we're just going to change the color.
//White if deselected.
Color color = Color.white;
if (preSelected)
{
//Yellow if preselected
color = Color.yellow;
}
if (selected)
{
//And green if selected.
color = Color.green;
}
//Set the color depending on what the game object has.
if (spriteRenderer)
{
spriteRenderer.color = color;
}
else if (text)
{
text.color = color;
}
else if (image)
{
image.color = color;
}
else if (GetComponent<UnityEngine.Renderer>())
{
GetComponent<UnityEngine.Renderer>().material.color = color;
}
}
}
}