Search Unity

Spawning Menu

Discussion in 'Immediate Mode GUI (IMGUI)' started by Glemau, Aug 22, 2015.

  1. Glemau

    Glemau

    Joined:
    Jul 24, 2015
    Posts:
    37
    Hi there,

    I want to make a little menu that you can open by pressing a key, and then you can search for a certain thing
    that you can then spawn.
    Spawning is not the problem but how do i make this search window?
    I totally don't understand the UI system.
    Hope you help,

    ~Glemau
     
  2. seldom

    seldom

    Joined:
    Dec 4, 2013
    Posts:
    118
    Your window has to derive from EditorWindow, your GUI code goes into OnGUI, you bind the window to a hotkey through the MenuItem attribute.
    Also make sure to place the class somewhere inside the Assets/Editor folder, or at least have a parent folder named Editor.
    The following example will show up when pressing CTRL+T (on windows, CMD+T on Mac).

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. class TestWindow : EditorWindow
    6. {
    7.   string searchText;
    8.  
    9.   void OnGUI()
    10.   {
    11.     searchText = EditorGUILayout.TextField( "Search text", searchText );
    12.     if(GUILayout.Button("Search"))
    13.     {
    14.       // your code here
    15.     }
    16.   }
    17.  
    18.   [MenuItem("Window/test window %t")]
    19.   static void SpawnWindow()
    20.   {
    21.     GetWindow<TestWindow>();
    22.   }
    23. }
    24.  
     
  3. Glemau

    Glemau

    Joined:
    Jul 24, 2015
    Posts:
    37
    I think this is for the Editor right? I meant Ingame.
    but its a good thing its just not what i wanted :)
    but thanks for the answer.

    ~Glemau
     
  4. seldom

    seldom

    Joined:
    Dec 4, 2013
    Posts:
    118
    Right, this is editor code. To use it ingame, turn the class into a monobehaviour, put it into an empty scene object, and activate the object only when you want to display the menu. Better yet, use the new UGUI system.