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

Cloning ScriptableObect asset with sub-assets.

Discussion in 'Scripting' started by Elfstone, Aug 10, 2020.

  1. Elfstone

    Elfstone

    Joined:
    Apr 13, 2015
    Posts:
    26
    So, I was trying to make a ScriptableObject asset with sub assets for the sake of polymorphism.
    Turned out when I tried to copy the main asset, sub assets were not cloned. The copy references sub assets of the original.
    I didn't like the idea of writing custom code for cloning, so I did some research into official packages (Timeline), and found out if the sub-assets reference the main asset, cloning is handled as exactly what I would like. except for the names of the sub assets.

    I may be lame for discovering this the hard way. But can someone explain this? Is there any documentation on this behavior?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    C# language documentation covers the basics of how value vs reference types copy, and that informs how most 'clone-ish' operations will behave. Shallow seems implicit.

    There are exceptions to this such as when you get certain reference types out of Unity, the Unity engine actually makes a copy of the thing for you, such as
    Renderer.material
    , just to name one example.

    As far as your specific question, I'm not even sure what response you are looking for. If you have a specific behavior you are struggling to implement, it could be any number of things:

    - yes that's how you have to do it, there's no better way
    - oh you shouldn't do things that way, here's a better way
    - you don't even have to do that because you could do this instead
     
  3. Elfstone

    Elfstone

    Joined:
    Apr 13, 2015
    Posts:
    26
    I'm not sure if we are talking about the same thing. What I'm asking is about Unity's asset management, not quite relevant to how an object is copied at the language level, of which I think I understand the basics.

    Let me reexplain myself. So my asset looks like this.

    Code (CSharp):
    1. class StateBase: ScriptableObject
    2. {
    3. }
    4.  
    5. [CreateAssetMenu]
    6. class StateMachine: ScriptableObject
    7. {
    8.     [SerializeField]
    9.     StateBase[] _states;
    10. }
    11.  
    I create a StateMachine.asset and add state objects to it via AssetDatabase.AddObjectToAsset.
    When I copy the StateMachine.asset in the project window, I get a 'StateMachine 1.asset' of course. But the copy's _states' items still reference the sub assets of the original. If I edit the copies' states I end up modifying the original's. I understand this.

    But if my states have a serialized member, say _parent, that references the StateMachine, when I copy StateMachine.asset, the state objects also get copied into 'StateMachine 1.asset' as sub assets. References are remapped in the copy. I can edit the copy's states independent of the original, which is cool, but I need to see some documentation.

    --- that's exactly how copying Unity's Timline.asset works, its tracks are also sub assets and track.m_Parent is serialized and references either another track or the Timeline. It doesn't seem to have been explained anywhere.

    --- to put it even shorter, copying Scriptable Object assets sometimes work like copying prefabs, where all the game objects and components inside the prefab get copied and references remapped. Sometimes it does not, only the main asset gets a copy.
     
    Last edited: Aug 11, 2020
  4. Elfstone

    Elfstone

    Joined:
    Apr 13, 2015
    Posts:
    26
    I edited my last reply for a couple of times and got rejected. Just posting this to see if I can still reply.