- Home /
How to highlight a GameObject in the Scene view from an Inspector script?
In an Inspector script I've been working on, there's a drop-down list of child GameObjects related to the selected GameObject (essentially the parent). When one of them is selected, a reference of the child GameObject is stored inside the script.
Now, without deselecting the "parent" GameObject, I would like to highlight those specific GameObjects. A sphere, a circle, bounding box, anything really!
So far, I've been pointed to the OnDrawGizmo()
message method, but it never gets called inside Inspector / Editor scripts. I tried using the OnSceneGUI()
, which DOES get called frequently, but CANNOT use the Gizmos class to draw the highlighting-shapes.
Any work around?
Thanks!
Answer by chamberlainpi · Aug 22, 2013 at 04:40 PM
Alright I figured a solution!
Since the OnDrawGizmos()
method is only invoked on runtime scripts (not Editor scripts), I went back inside the Script that the Inspector acts on, and put the following lines:
.............................................................................
MonoBehaviour derived class:
// Above in the USING/ IMPORT statements:
#if UNITY_EDITOR
using UnityEditor;
#endif
// ... somewhere else within the CLASS definition:
#if UNITY_EDITOR
private const float _OFFSET_CROSS_SIZE = 0.1f;
private static Vector3 _OFFSET_X_POS = new Vector3(_OFFSET_CROSS_SIZE, 0f, 0f);
private static Vector3 _OFFSET_Y_POS = new Vector3(0f, _OFFSET_CROSS_SIZE, 0f);
[HideInInspector] public GameObject inspectorObject;
[HideInInspector] public int inspectorObjectIndex; //Optional
void OnDrawGizmos() {
if (inspectorObject == null ||
Selection.objects.Length == 0 ||
Selection.objects[0] != this.gameObject) return;
//Draw a "crosshair" on the selected child GameObject:
Vector3 pos = inspectorObject.transform.position;
Gizmos.color = Color.cyan;
Gizmos.DrawLine(pos + _OFFSET_X_POS, pos - _OFFSET_X_POS);
Gizmos.DrawLine(pos + _OFFSET_Y_POS, pos - _OFFSET_Y_POS);
}
#endif
........................................................................
Editor / Custom-Inspector derived class:
public override void OnInspectorGUI() {
mTarget = this.target as NAME_OF_YOUR_CLASS_HERE;
//Somewhere after the GameObject DropDown list...
mTarget.inspectorObject = theSelectedListItem.gameObject;
//Check if the DropDown selection has changed...
if(lastSelectedListItem!=theSelectedListItem) {
SceneView.RepaintAll(); //Refreshes the gizmos on the Scene view.
lastSelectedListItem = theSelectedListItem;
}
}
.................................................................................................
For example, this would draw a cyan colored cross like this:
^^Preview of Gizmos drawn according to the "Current Layer:" selection in the Inspector
Your answer
Follow this Question
Related Questions
What is the best way to fix texture z-fighting? 5 Answers
Drawing a jump distance gizmo 0 Answers
Editor Gizmo Transform 0 Answers
how to create handles like editor gizmos in runtime? 6 Answers