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

Custom Assets vs. Single Component Prefabs

Discussion in 'Scripting' started by Samo-Jordan, Jul 7, 2014.

  1. Samo-Jordan

    Samo-Jordan

    Joined:
    Jan 15, 2014
    Posts:
    11
    Hello everyone

    I have recently been studying custom assets based on classes derived from ScriptableObject, saved using AssetDatabase.CreateAsset()/AssetDatabase.SaveAssets(). My motivation to use them was to have the possibility of referencing the same class instance from many components, avoiding data duplication and reducing serialization/deserialization overhead.

    Code (csharp):
    1. // saved to disk as asset file
    2. public class MyClass: ScriptableObject {
    3. }
    4.  
    5. // two components located in some other gameobjects
    6. public class MyComponent1 : MonoBehaviour {
    7.     public MyClass myObject;
    8. }
    9.  
    10. public class MyComponent2: MonoBehaviour {
    11.     public MyClass myObject; // may point to the same instance as MyComponent1.myObject
    12. }
    But then I realized that in a project one can have both situations: many components sharing the same data, and many other components which have individualized data. In the latter case the workflow of creating/saving a new asset for each component becomes tedious. In that situation I would like to embed the class instance into the component, which normally would be done as follows:

    Code (csharp):
    1. [System.Serializable]
    2. public class MyClass {
    3. }
    4.  
    5. public class MyComponent : MonoBehaviour {
    6.     public MyClass myObject;
    7. }
    These two approaches are mutually exclusive. Then I came up with a new idea, namely deriving MyClass from MonoBehaviour. If I want the 'shared data' behaviour I add a component of MyClass to a prefab and add a reference to the prefab to MyComponent.

    Code (csharp):
    1. // added as component to a prefab
    2. public class MyClass : MonoBehaviour {
    3. }
    4.  
    5. public class MyComponent : MonoBehaviour {
    6.     public MyClass myObject;
    7. }
    If I want the 'individualized data' behaviour I simply use MyClass as standard component in any gameobject which needs this kind of behaviour.

    At this point I began to wonder if there was *any* difference between the custom asset and the single component prefab except that the latter allows me to implement the 'individualized data' behaviour while the former does not. So I would like to ask the question here: is there any reason to use custom assets at all when the same functionality can apparently be replicated using single-component prefabs?
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Yes. A ScriptableObject define a set of serialized data, nothing more, nothing less. They can be created directly from the Project without having to "add a component" on a GameObject. They do not have any behaviour by themselves, no update method. They are also lighter because they don't carry around a transform component.

    Component usually define a behaviour, something that happens. They can be added, remove or move around. They have a reason to exist on a GameObject, because they are assumed to exist in a world and perform a task.

    Can you use component to do the job of a ScriptableObject? Yes. Should you? Nah.
     
    IvanAuda likes this.