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

Need help understanding how to use ScriptableObject.

Discussion in 'Scripting' started by Pysassin, May 8, 2014.

  1. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    I was told to use ScriptableObject for item statistics/class information and I THOUGHT I was doing so when I copied some ones snippit but then I found out I was using a Class that wasn't extending anything. I made one that now extends ScriptableObject but have NO clue how I can get into the ScriptableObject to set the variables in the editor like I need to do. I can use the CreateInstance function OnAwake and then set stuff that way but I need a way to set the variables before runtime, and a way I can see them in the editor without running the project.Is there a way to do this? I need something like looks just like setting class variables in the editor of a class that extends monobehaviour, but have no idea how to do this. I tried looking through the script ref but it is completely useless as far as ScriptableObjects go....

    UnityScript is what I use so a solution in that language would be best but I could get by with something in C#
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Yeah I don't blame you for having trouble with this; there're a couple steps to do what you want.

    You need to create an editor class (script inside Editor folder) and then have a static method with the MenuItem attribute applied to do the work. You also need to use the AssetDatabase class to create a .asset for you (this will be the custom ScriptableObject). I'm sure there are samples of it floating around the web.
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
  4. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    @TonyLi thanks for the link!

    And to avoid confusion, when the title of the article says :
    "Unity Pro Tip: Use Custom Made Assets As Configuration Files"

    it means "Pro Tip for Unity" not "Tip for Unity Pro" right?
     
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Useful code;

    Code (csharp):
    1.  
    2.     private static void Create<T>() where T : ScriptableObject
    3.     {
    4.         MethodInfo method = EditorWindow.focusedWindow.GetType().GetMethod("GetActiveFolderPath", BindingFlags.Instance | BindingFlags.NonPublic);
    5.         if (method == null)
    6.             return;
    7.  
    8.         T item = ScriptableObject.CreateInstance<T>();
    9.         string path = (string)method.Invoke(EditorWindow.focusedWindow, new object[0]);
    10.  
    11.         path += "/New " + typeof(T).Name + ".asset";
    12.         path = AssetDatabase.GenerateUniqueAssetPath(path);
    13.  
    14.         AssetDatabase.CreateAsset(item, path);
    15.     }
    16.  
    17.     [MenuItem("Assets/Create/Item")]
    18.     public static void CreateItem()
    19.     {
    20.         Create<ItemData>();
    21.     }
    22.  
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    That's correct. It works fine in Unity Free. I guess the tutorial was written back in the day when "pro tip" was a buzzword. :)