Search Unity

Find a gameobject in project windows (through code)

Discussion in 'Scripting' started by sirio21, Sep 1, 2016.

Thread Status:
Not open for further replies.
  1. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    hi!! i need to locate a gameobject in project windows programatically.
    Exactly like if we use the search window in the project tab.

    It's possible to do this?
    Thanks!!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I'm assuming we're talking some sort of editor script?
     
  3. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Maybe you can use Resources.FindObjectsOfTypeAll() to search for Objects, then iterate through the array to seach by name. This is going to be very slow, though, so hopefully it isn't intended for gameplay. I think it returns scene objects, too.
     
  4. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    i need to do it in editor not at run time; in a lot of gameobject having the name with a button i need to bring it (locate it) quickly; actually i need to take the name, go to project windows and type the name it to locate, i need to locate automatically with code
     
  5. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    plis plis
     
  6. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    Like @Hyblademin said, use Resource.Resources.FindObjectsOfTypeAll() with the desired type. This will give you a list of all objects in the scene and project pane that are of that type. You then will have to take the array you are given and filter out the results based on your search. If you get a list of GameObjects, you can access the .scene property and check if that is null, which means the object isn't in the scene. You can use the Selection class to select the objects you have come up with.
     
  7. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
  8. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    Actually that is completely wrong.

    Execute in edit mode is meant to have a regular script ALSO execute while in edit mode, aside from play mode.

    In order to make editor scripts, you'd like to use editor classes ( Editor, EditorWindow) or use a scriptable object.
     
    Bunny83 likes this.
  9. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    My problem is how i can interact with the project window (in order to show it there) when game object is found it.
     
  10. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Completely wrong seems a little harsh... I'd say mostly wrong, but only due to semantics. Anyway, what I meant to say was, "try ExecuteInEditMode". I'm not sure what exactly what OP's trying to do, so I don't know if that's going to cut it for them.

    Actually, can you give us a better idea of what you're doing @sirio21 ? I don't really know what you mean by "interact with the project window"; are you trying to highlight things?
     
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Bunny83 likes this.
  12. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    Might be a little harsh :) point is, ExecuteInEditMode is not for making editor scripts, but rather have gameplay scripts also run in editor mode outside of play mode. This is an important distinction, especially with this case, where the OP wants to script the project window. For what he is attempting to do, which from my understanding is to basically script the search functionality of Project Window, he'll need an editor script, and the UnityEditor namespace as well, both are unavailable in a built gameplay script. I wanted to make this distinction especially for future readers.

    Back to the problem at hand though, you are going to have to code this up by yourself. You can't really interact with the project window natively (without some magic). Like mentioned already, if you get the proper object, and then call Selection.ActiveObject = theObject you just found, it will select it in the project window.
     
    Bunny83 likes this.
  13. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    Sorry for my english,,
    Having the (blue) Name of gameobject (a material in this case), i need to locate and show it (green) like if i type the name (red) in the project window search texboxt. Idea is create a button (blue) with the name of gameobject to locate it quickly.
     
  14. StephenWebb

    StephenWebb

    Joined:
    Feb 3, 2013
    Posts:
    11
    Nope it is completely wrong, and not because of semantics either. What the OP is asking is how to automatically select a prefab in the project window from code. He is looking to Ping the object.
     
  15. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    He WAS looking to ping the object, eight years ago. Don't necro-post unless you have very useful information to share like a working solution.
     
    Bunny83 likes this.
  16. StephenWebb

    StephenWebb

    Joined:
    Feb 3, 2013
    Posts:
    11
    Right, I did, you see, he was looking to use PingObject. That is the method, it's part of EditorGUIUtility.PingObject(object) - but he is completely wrong about [ExecuteInEditMode] which is something he should know if he is writing Editor or EditorWindow code. I program tools, it's what I do. Anyhow, eight years, a thousand years, time is irrelavant in this case. Someone else can still visit and see what was posted, so it is what it is until it ain't
     
  17. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,992
    And that was already mentioned by @Baste in post #11 in 2016. So you just repeated what was already said.

    Uhm, no, that's not true. What makes you think that the anti-necro posting rule (point 1i) doesn't apply to this thread? The older the thread is, the more important the information has to be in order to be relevant and to justify waking a dead thread. In your case you said two things that has been said several times already. Also Baste actually posted the link to the actual method documentation while you just mentioned the abstract concept in the end. So since it appears that you didn't read through the thread, do you think your post will improve the thread by making it even longer and reduce the chance of other people reading through it? :)

    Anyways, I think this thread should be locked as the question has been answered a bit over 7 years ago.
     
    Ryiah, sngdan and Anthiese like this.
Thread Status:
Not open for further replies.