Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# Create new editor menu from array

Discussion in 'Scripting' started by carking1996, Apr 9, 2014.

  1. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,605
    Hello,

    Is it possible to create an editor menu from an array? I'm getting all the prefabs in a folder from an editor script and was wondering how to dynamically create editor menus from the array of prefabs to load them from.
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    I don't understand what you're trying to do. Perhaps you can explain a bit more.
     
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Do I understand you want to give a restricted choice to the user of your editor?
     
  4. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    It is not exactly clear what you are asking for, but one option might be to just provide a single menu with all the commands and then add validators to disable the ones which are not appropriate for the current context:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. static class YourMenuItems {
    6.     [MenuItem("Your Menu/Foo")]
    7.     private static void Foo() {
    8.         Debug.Log(Selection.activeTransform.name);
    9.     }
    10.     [MenuItem("Your Menu/Foo", true)]
    11.     private static bool ValidateFoo() {
    12.         // Menu item is only useful when there is a user selection.
    13.         return Selection.activeTransform != null;
    14.     }
    15. }
    16.  
    Or, perhaps you are wanting to add a custom dynamic menu within your own editor window/inspector?
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public class YourEditorWindow : EditorWindow {
    6.     private void OnGUI() {
    7.         if (GUILayout.Button("My Menu"))
    8.             ShowMyMenu(GUILayoutUtility.GetLastRect());
    9.     }
    10.  
    11.     private void ShowMyMenu(Rect position) {
    12.         var menu = new GenericMenu();
    13.  
    14.         menu.AddItem(new GUIContent("Always Available A", false, () => {
    15.             Debug.Log("Clicked 'Always Available A' menu item!");
    16.         }));
    17.  
    18.         menu.AddSeparator("");
    19.  
    20.         menu.AddItem(new GUIContent("Always Available B", false, () => {
    21.             Debug.Log("Clicked 'Always Available B' menu item!");
    22.         }));
    23.  
    24.         if (Selection.activeTransform != null) {
    25.             // Only show this menu when an object is selected!
    26.             menu.AddItem(new GUIContent("Name of Selected Transform", false, () => {
    27.                 Debug.Log(Selection.activeTransform.name);
    28.             }));
    29.         }
    30.  
    31.         menu.DropDown(position);
    32.     }
    33. }
    34.  
    I hope that this helps :)
     
    FM-Productions likes this.
  5. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,605
    I apologize, I forgot about this thread. Here's what I wanted to do.

    I have traffic car Prefabs in a folder in the Assets folder of the project. I want the user to be able to add vehicle prefabs, and then have them come up in the menu option. So, adding a menu option per prefab in the folder, dynamically.
     
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    What numberkruncher posted should give you just that.

    Code (csharp):
    1.  
    2. GenericMenu menu = new GenericMenu();
    3. foreach(GameObject prefab in AssetDatabase.LoadAllAssetsAtPath(PATH))
    4. {
    5.     menu.AddItem(new GUIContent(prefab.name), false, SelectPrefab, prefab);
    6. }
    7.  
    Or something similar...

    If you have a lot of object, you can maybe look into the link in my signature. It's a modal window for listing choices to the user in editor mode.