• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by chamberlainpi · Aug 22, 2013 at 03:19 PM · editordrawinggizmohighlight

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 layer selection in the Inspector

^^Preview of Gizmos drawn according to the "Current Layer:" selection in the Inspector


flash2json_ss_gizmodemo.png (165.3 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use UnityEditor.Highlight properly? 0 Answers

Objects Appear in the wrong place when built 1 Answer

How to disable the default transform gizmo in editor? 1 Answer

How to draw a textured plane in Editor? 2 Answers

What's up with my transform gizmo here? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges