Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

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

Permanent handles (when object is not selected) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges