Need help understanding how to use ScriptableObject.

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#

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.

Jacob Pennock wrote a great tutorial on this: http://www.jacobpennock.com/Blog/?p=670

@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?

Useful code;

    private static void Create<T>() where T : ScriptableObject
    {
        MethodInfo method = EditorWindow.focusedWindow.GetType().GetMethod("GetActiveFolderPath", BindingFlags.Instance | BindingFlags.NonPublic);
        if (method == null)
            return;

        T item = ScriptableObject.CreateInstance<T>();
        string path = (string)method.Invoke(EditorWindow.focusedWindow, new object[0]);

        path += "/New " + typeof(T).Name + ".asset";
        path = AssetDatabase.GenerateUniqueAssetPath(path);

        AssetDatabase.CreateAsset(item, path);
    }

    [MenuItem("Assets/Create/Item")]
    public static void CreateItem()
    {
        Create<ItemData>();
    }

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. :slight_smile: