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

What are ScriptableObjects used for?

Discussion in 'Scripting' started by liortal, Jun 20, 2014.

  1. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,557
    Hi,

    I read about the ScriptableObject class but i still can't figure out what it may be good for.

    From reading the documentation/some more information on the answers site, i gathered that:

    • It is good for storing data.
    • It can be copied by reference, hence saving up on memory (instead of creating multiple copies of the data object).
    • It is not associated to a GameObject (like Components are).

    Can you please provide some example usages of how you are using ScriptableObject and the problem that it solves? It may be easier for me to understand it that way.
     
    Last edited: Jun 20, 2014
  2. ElvisAlistar

    ElvisAlistar

    Unity Technologies

    Joined:
    Oct 2, 2013
    Posts:
    226
  3. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    What I can think of is maybe a localization texts. If many objects need to access storage with lots of strings, ScriptableObject can be useful. Really it's one of those features that most of us have never asked for :p
    Maybe can store terrain data or something like that...
     
  4. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    ScriptableObjects are types that I infact use on a daily basis. Especially when diving deeper you will love them. On of their huge benefits is that the get serialized properly as reference and even when they are stored as their parent class. So yes they aswell work under polymorphism. Other than that, you can store them directly to the asset database.

    Regarding the serialization aspect:
    http://forum.unity3d.com/threads/serialization-best-practices-megapost.155352/
     
    zaxvax likes this.
  5. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Zomg wall of text! But looks like a good article. Bookmarked for dinner time.
    Can you share with us more details about your usage of SO? What kind of data you store in them?
     
  6. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    Well, the most recent example would be my batching tool. It effectivly needs to split up submeshes of meshes into own meshes. This takes alot of procssing time when doing it at runtime. So I grant developers the possibility to set up those BatchParts at edit time. BatchParts are nothing more then a storage class to hold the newly generated meshes and materials, and yes they are scriptable objects in order to save them to disk. ;)
     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    What I used them for so far;

    - Item definition
    - Localization
    - Audio item
    - Quest
    - Character customization
    - ... and I probably forget some

    In other words, any time I have to save data that is not related to a spatial position, and is independent of all scenes.
     
  8. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    Unity Live Training just made an excellent tutorial on scriptable objects.

    The archive link isn't up yet, but here is the Twitch link http://www.twitch.tv/unitytech/b/539093937 the actual tutorial starts at around the 10 minute mark.
     
  9. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    Is there a good reason to use a Scriptable object in place of a static class?
     
  10. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,557
    What are the benefits of using ScriptableObjects over a plain old class that stores data ?
     
  11. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    It's all explained in the video I linked :)
     
  12. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Unity handles all the serialization/deserialization.

    Otherwise, you have to create your own file format.

    The Inspector also display values in ScriptableObject, which it wouldn't with custom file format.
     
  13. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,557
    Awesome. Looks like I have some links to check out. But I think I am getting the point.