Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

How to use ScriptableObject

Discussion in 'Scripting' started by jashan, Mar 15, 2008.

  1. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,053
    Hi there,

    I need a little more info than I find in:

    http://unity3d.com/support/documentation/ScriptReference/ScriptableObject.html

    I noticed that GUISkin is a scriptable object, so I thought maybe I could use ScriptableObject to create assets that exist in the project view, of which the properties can changed there and taht can then be dragged on the relevant properties in scripts that use this (just like it's done with the GUISkin).

    Now the magical question is: If that's possible, how do I get instances of my ScriptableObjects into the project? ;-)

    Or is this not what ScriptableObjects are meant to be used for? Then, what is it?

    Any info will be greatly appreciated...

    Sunny regards,
    Jashan
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Yep, that's how the GUISkin asset works basically.

    If you want to create an asset _file_ that is saved to the project directory like GUISkin is, and you don't just want to just use it at runtime (CreateInstance(...), etc), you create an Editor script to create your asset, and hook it into the menu as you do with any Editor script (MenuItem attribute).

    The rest is using some undocumented stuff. It's a *use at your own risk* thing, because this stuff isn't officially supported yet, but it's in there and usable. Just don't yell at UT if it does something goofy or blows up your machine. ;-)

    Here is what the editor script would look like basically:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System;
    5.  
    6. public class MyAssetEditor
    7. {  
    8.     [MenuItem("Assets/Create/MyAsset")]
    9.     public static void CreateMyAsset()
    10.     {
    11.         MyAsset asset = new MyAsset();  //scriptable object
    12.         AssetUtility.CreateAsset(asset, "New MyAsset");
    13.         AssetUtility.SaveAsset(asset);
    14.         EditorUtility.FocusProjectView();
    15.         Selection.activeObject = asset;        
    16.     }
    17. }
    18.  
    Took a while to figure out. As I said, use at your own risk. ;-)

    All this being said, unless you really really need to do it this way, it would probably best to just use Prefabs for your "asset" objects.

    -Jeremy
     
  3. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,053
    Hi Jeremy,

    thanks - that does it for me :)

    I'm using this to store metadata for my levels that is really "global in nature". So, game objects and prefabs would really be kind of overkill for this, it's "above scene level in project level", so to say. What I have now is ten assets based on my ScriptableObject "LevelData" that contain the relevant meta-information (stuff like filename, human readable name, description and stuff like that). Which is very nice because I can change this in the editor while play-testing. Also, I get very nice "source-control" through the Asset Server ;-)

    The only thing I can see I need to be careful with: There's a few properties I change during runtime, so these get permanently changed in the LevelData objects. Not a big deal in my case, but something one sure needs to be aware of...

    Very nice! And of course: I hope this will soon be "officially supported" (actually, that little Editor script would really be nice in the ScriptableObject doc file).

    Sunny regards,
    Jashan
     
  4. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    UT does some sort of voodoo to give GUISkin an icon, and extention other than asset, but I couldn't figure out how. This is close enough without the extension as long as you use good names for the different asset files.

    -Jeremy
     
  5. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    409
    Aside from moving the braces up to the beginning of the line where they belong :wink: - this is a one stop fix. Thanks!
     
  6. pookool03

    pookool03

    Joined:
    Nov 29, 2012
    Posts:
    1
    i am new to unity
    i have to implement scriptable object in my project
    the above is the editor code
    how to write the class that inherits from scriptable object
    Any help will be appreciated

    Thanks
     
  7. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,166
    I know this thread is very old, however it popped up when i searched for ScriptableObjects and so others may find this useful: I've put together a helper editor extension that allows you to create a ScriptableObject and save it in the project.

    Unfortunately, Unity doesn't provide the option to create these types of objects from the editor, and so you have to code that solution yourself...

    Here's a link to the github repository where i posted my helper code: https://github.com/liortal53/ScriptableObjectFactory
    I also posted about it in my blog: http://www.tallior.com/2014/08/06/unity-scriptableobject-factory/

    Hope this helps others!
     
    Nition, Mock and Ash-Blue like this.