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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

is there a script for creating ScriptableObjects by context menu?

Discussion in 'Scripting' started by craig4android, Nov 6, 2019.

  1. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    is there a script for creating ScriptableObjects by context menu?

    I know there is [CreateAssetMenu(menuName = "")] but it's awkward to use it on every ScriptableObject.

    Instead I want to create an contextmenu Item that can generate all ScriptableObjects.
     
  2. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
  3. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    k ty, I'll post my solution when I finished it.
     
  4. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    here we go:

    Code (CSharp):
    1.  
    2.     [MenuItem("myFunctions/CreateScriptableObject")]
    3.     public static void createScriptableObject()
    4.     {
    5.         if (Selection.activeObject is MonoScript)
    6.         {
    7.             MonoScript ms = (MonoScript)Selection.activeObject;
    8.             ScriptableObject so = ScriptableObject.CreateInstance(ms.name);
    9.  
    10.             string path = System.IO.Directory.GetParent(AssetDatabase.GetAssetPath(ms.GetInstanceID())) + "/" + ms.name + ".asset";
    11.             int cntr = 0;
    12.             while (!createIfDoesntExists(path, so))
    13.             {
    14.                 path = System.IO.Directory.GetParent(AssetDatabase.GetAssetPath(ms.GetInstanceID())) + "/" + ms.name + cntr.ToString() + ".asset";
    15.  
    16.                 cntr++;
    17.                 if (cntr > 10)
    18.                 {
    19.                     break;
    20.                 }
    21.             }
    22.             AssetDatabase.Refresh();
    23.         }
    24.     }
    25.     public static bool createIfDoesntExists(string path, Object o)
    26.     {
    27.         var ap = AssetDatabase.LoadAssetAtPath(path, o.GetType());
    28.         if (ap == null)
    29.         {
    30.             AssetDatabase.CreateAsset(o, path);
    31.             return true;
    32.         }
    33.         else
    34.         {
    35.             return false;
    36.         }
    37.     }
    any improvements?

    You have to select the ScriptableObject source file and then you can add it by the MenuItem
     
    unity_IpxdANggCs1roQ likes this.
  5. tyoden

    tyoden

    Joined:
    Sep 6, 2015
    Posts:
    2
    If google brought you here, like me, then here is an improved script for the context menu


    Code (CSharp):
    1. using UnityEditor;
    2.  
    3. public static class ScriptableObjectCreator
    4. {
    5.     [MenuItem("Assets/Create/Scriptable Object", false, 0)]
    6.     public static void CreateScriptableObject()
    7.     {
    8.         var selection = Selection.activeObject;
    9.         var assetPath = AssetDatabase.GetAssetPath(selection.GetInstanceID());
    10.         var path = $"{System.IO.Directory.GetParent(assetPath)}/{selection.name}.asset";
    11.         ProjectWindowUtil.CreateAsset(ScriptableObject.CreateInstance(selection.name), path);
    12.     }
    13.  
    14.     [MenuItem("Assets/Create/Scriptable Object", true)]
    15.     public static bool CreateScriptableObjectValidate()
    16.     {
    17.         return Selection.activeObject is MonoScript monoScript && IsScriptableObject(monoScript.GetClass());
    18.     }
    19.  
    20.     private static bool IsScriptableObject(System.Type type) => typeof(ScriptableObject).IsAssignableFrom(type);
    21. }
    22. }
     
    Last edited: Feb 8, 2022
  6. n12howlingshame

    n12howlingshame

    Joined:
    Jun 7, 2018
    Posts:
    7
    Last edited: Sep 22, 2023