Tanoda
ExampleSelectable.cs
Go to the documentation of this file.
1
5
7{
8 public class ExampleSelectable : MonoBehaviour, IBoxSelectable
9 {
10 #region Implemented members of IBoxSelectable
11 bool _selected = false;
12 public bool selected
13 {
14 get
15 {
16 return _selected;
17 }
18
19 set
20 {
21 _selected = value;
22 }
23 }
24
25 bool _preSelected = false;
26 public bool preSelected
27 {
28 get
29 {
30 return _preSelected;
31 }
32
33 set
34 {
35 _preSelected = value;
36 }
37 }
38 #endregion
39
40 //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
41 SpriteRenderer spriteRenderer;
42 Image image;
43 Text text;
44
45 void Start()
46 {
47 spriteRenderer = transform.GetComponent<SpriteRenderer>();
48 image = transform.GetComponent<Image>();
49 text = transform.GetComponent<Text>();
50 }
51
52 void Update()
53 {
54
55 //What the game object does with the knowledge that it is selected is entirely up to it.
56 //In this case we're just going to change the color.
57
58 //White if deselected.
59 Color color = Color.white;
60
61 if (preSelected)
62 {
63 //Yellow if preselected
64 color = Color.yellow;
65 }
66 if (selected)
67 {
68 //And green if selected.
69 color = Color.green;
70 }
71
72 //Set the color depending on what the game object has.
73 if (spriteRenderer)
74 {
75 spriteRenderer.color = color;
76 }
77 else if (text)
78 {
79 text.color = color;
80 }
81 else if (image)
82 {
83 image.color = color;
84 }
85 else if (GetComponent<UnityEngine.Renderer>())
86 {
87 GetComponent<UnityEngine.Renderer>().material.color = color;
88 }
89
90
91 }
92 }
93}
System.Drawing.Image Image
Definition: TestScript.cs:37
UnityEngine.Color Color
Definition: TestScript.cs:32
Credit Erdener Gonenc - @PixelEnvision.