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 mattbenic · Jul 10, 2013 at 10:25 AM · editor-scripting

How to highlight an object in the heirarchy from code?

I have an editor script that finds objects in the scene which reference the currently selected object, adds them to a List and then sets Selection.objects to those objects:

     foreach (UnityObject sceneObject in sceneObjects)
     {
         // Iterate over the fields in the object
         FieldInfo[] fInfos = sceneObject.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
         bool addedObjectToReferences = false;
         foreach (FieldInfo fInfo in fInfos)
         {
             bool fieldReferencesValue = false;
             SysObject fieldValue = fInfo.GetValue(sceneObject);
             if (fieldValue == selection)
             {
                 fieldReferencesValue = true;
             }
             else if (fInfo.FieldType.IsArray && fInfo.FieldType.GetElementType().IsAssignableFrom(selectionType))
             {
                 // This is an array field, check each of it's elements for a reference
                 foreach (System.Object element in (System.Object[]) fieldValue)
                 {
                     if (element == selection)
                     {
                         fieldReferencesValue = true;
                         break;
                     }
                 }
             }
             if (fieldReferencesValue)
             {
                 if (!addedObjectToReferences)
                 {
                     EditorGUIUtility.PingObject(sceneObject);
                     references.Add(sceneObject);
                     addedObjectToReferences = true;
                 }

                 // Log with context for easy use
                 Log.Log(string.Format("{0} referenced by {1} ({2}.{3})", selection, sceneObject.name, sceneObject.GetType().Name, fInfo.Name), sceneObject);
             }
         }
     }

     if (references.Count == 0)
     {
         EditorUtility.DisplayDialog("No references", "No references found to selection", "Ok");
     }
     else
     {
         Log.Log(string.Format("Found {0} references to {1}", references.Count, selection.name));
         Selection.objects = references.ToArray();
     }

For the most part this works beautifully, the references are found and selected (I can tell they are selected by the content of the inspector), but they are not hilighted in the Hierarchy. Am I missing something here? Is there some way to force the editor to highlight the current selection(s)?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by DavidDebnar · Jul 10, 2013 at 10:43 AM

Edit: I just reread your question. Make sure to pass GameObject[] into Selection.objects, not Object[] and it'll work.

Also, take a look at EditorGUIUtility.PingObject.

 function Ping() {
     if(!Selection.activeObject) {
         Debug.LogError("Select an object to ping");
         return;
     }
     
     for(var o in Selection.objects) {
         EditorGUIUtility.PingObject(o);
         Debug.Log("Pinged " + o.name);
     }
 }

--David--

Comment
Add comment · Show 4 · 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
avatar image mattbenic · Jul 10, 2013 at 11:22 AM 0
Share

Thanks $$anonymous$$, I'm already pinging the objects (line 30 in the code I posted) and that works to temporarily highlight the objects. However I want to persistently highlight them (as if they had been selected with a click).

avatar image DavidDebnar · Jul 10, 2013 at 11:26 AM 0
Share

I've edited the answer to include that too.

avatar image mattbenic · Jul 10, 2013 at 11:47 AM 1
Share

Thanks, but nope, passing it a GameObject array ins$$anonymous$$d of a Unity.Object array makes no difference (not that I would have expected it to, the property is a Unity.Object[] type so they would be doing casting internally if necessary anyway, and things like textures and materials wouldn't be selectable if a GO[] was required). For reference, here's the change I tried (after trying a linq version first, just to be sure my linq isn't completely fubar):

         List<GameObject> goReferences = new List<GameObject>();
         foreach (UnityObject reference in references)
         {
             if (reference is GameObject) goReferences.Add(reference as GameObject);
         }

         Selection.objects = goReferences.ToArray();
avatar image DavidDebnar · Jul 10, 2013 at 12:25 PM 0
Share

Odd, because it worked for me.

avatar image
0

Answer by Free286 · Mar 22, 2018 at 11:24 AM

Modified script to work by pressing D when selecting an object in the hierarchy. It will find and highlight objects that are referencing the selected objects.

 using System;
 using System.Collections.Generic;
 using System.Reflection;
   using UnityEditor;
   using UnityEngine;
   
 public class BacktraceReference {
         private Component _theObject;
         
         public static List<UnityEngine.Object> find;
 
         [MenuItem("Tools/What Objects Reference this? _d")]
         public static void FindReferences() {
             
             find = new List<UnityEngine.Object>();
 
             foreach (var item in Selection.objects) {
                 var g = item as GameObject;
                 if (g) {
                     foreach (var c in g.GetComponents<Component>()) {
                         FindObjectsReferencing(c);
                     }
                 }
             }
             
             if (find.Count == 0) {
                 // Debug.Log("<b>No objects reference this</b>");
             }
             else {
                 Selection.objects = find.ToArray();
             }
                 foreach (var o in Selection.objects) {
                     EditorGUIUtility.PingObject(o);
                 }
         }
         private static void FindObjectsReferencing<T>(T mb) where T : Component {
                 var objs = Resources.FindObjectsOfTypeAll(typeof (Component)) as Component[];
                 if (objs == null) return;
                 foreach (Component obj in objs) {
                         FieldInfo[] fields =
                                 obj.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance |
                                                                                 BindingFlags.Static);
                         foreach (FieldInfo fieldInfo in fields) {
                                 if (FieldReferencesComponent(obj, fieldInfo, mb)) {
                                     var contains = false;
                                     foreach (var item in Selection.objects) {
                                         if (item == obj.gameObject) {
                                             contains = true;
                                             break;
                                         }
                                     }
                                     contains |= find.Contains(obj.gameObject);
                                     if (!contains) {
                                         find.Add(obj.gameObject);
                                         Debug.Log("<b>" + obj.gameObject +  "</b>\n", obj.gameObject);
                                     }
                                 }
                         }
                 }
         }
 
         private static bool FieldReferencesComponent<T>(Component obj, FieldInfo fieldInfo, T mb) where T : Component {
                 if (fieldInfo.FieldType.IsArray) {
                         var arr = fieldInfo.GetValue(obj) as Array;
                         if (arr != null) {
                             foreach (object elem in arr) {
                                     if (elem != null && mb != null && elem.GetType() == mb.GetType()) {
                                             var o = elem as T;
                                             if (o == mb)
                                                     return true;
                                     }
                             }
                         }
                 }
                 else {
                         if (fieldInfo.FieldType == mb.GetType()) {
                                 var o = fieldInfo.GetValue(obj) as T;
                                 if (o == mb)
                                         return true;
                         }
                 }
                 return false;
         }
 }
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

17 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 avatar image

Related Questions

Copying files from one directory to the other 1 Answer

Is it possible to draw more than one property attribute on a field? 1 Answer

Browse button in unity editor script 0 Answers

Find public assigned values using editor script 1 Answer

How can I preserve static object/data between editor and play? 2 Answers


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