Created
September 29, 2012 15:40
-
-
Save AngryAnt/3804386 to your computer and use it in GitHub Desktop.
Example code for "Pick me! Pick me!" blog post on AngryAnt.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class SelectableObject : MonoBehaviour | |
{ | |
public Rect m_SelectionWindowRect = new Rect (10.0f, 10.0f, 300.0f, 100.0f); | |
public void OnMouseDown () | |
{ | |
SelectionManager.Select (gameObject, !SelectionManager.IsSelected (gameObject)); | |
} | |
public void OnDisable () | |
{ | |
SelectionManager.Deselect (gameObject); | |
} | |
public void Update () | |
{ | |
renderer.material.color = SelectionManager.IsSelected (gameObject) ? Color.green : Color.white; | |
} | |
public void OnGUI () | |
{ | |
if (SelectionManager.IsSelected (gameObject)) | |
{ | |
m_SelectionWindowRect = GUI.Window (GetInstanceID (), m_SelectionWindowRect, SelectionWindow, gameObject.name); | |
} | |
} | |
void SelectionWindow (int id) | |
{ | |
GUILayout.Box ("I am the selection and my name is " + gameObject.name); | |
GUI.DragWindow (); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class SelectionManager | |
{ | |
private static GameObject s_ActiveSelection; | |
public static GameObject ActiveSelection | |
{ | |
get | |
{ | |
return s_ActiveSelection; | |
} | |
set | |
{ | |
s_ActiveSelection = value; | |
} | |
} | |
public static void Select (GameObject gameObject, bool selectionValue) | |
{ | |
if (selectionValue) | |
{ | |
Select (gameObject); | |
} | |
else | |
{ | |
Deselect (gameObject); | |
} | |
} | |
public static void Select (GameObject gameObject) | |
{ | |
ActiveSelection = gameObject; | |
} | |
public static void Deselect (GameObject gameObject) | |
{ | |
if (ActiveSelection == gameObject) | |
{ | |
ActiveSelection = null; | |
} | |
} | |
public static bool IsSelected (GameObject gameObject) | |
{ | |
return ActiveSelection == gameObject; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment