Search Unity

Converting ScriptableObjects to MonoBehaviours.

Discussion in 'Editor & General Support' started by TheHeftyCoder, Jul 6, 2020.

  1. TheHeftyCoder

    TheHeftyCoder

    Joined:
    Oct 29, 2016
    Posts:
    91
    I've found myself in a situation where I went with ScriptableObject instead of MonoBehaviour. At the time it made sense, but come along a few Unity updates and it now doesn't.. I now have a bunch of SO assets filled with data. I want to keep the data intact and convert those assets to prefabs.

    Any ideas on a smooth transition? I noticed that SOs and MBs in disk are almost identical, save from a GameObject and a Transform tag. Thought I'd ask here if anyone has any tips.

    Cheers!
     
  2. TheHeftyCoder

    TheHeftyCoder

    Joined:
    Oct 29, 2016
    Posts:
    91
    Well, the easiest solution seems to be this:
    1) Find all different types of SOs you want. Serialize the assets with JsonUtility and save the json along with the qualified assembly name.
    2) Change the scripts you want from ScriptableObject to MonoBehaviour.
    3) Create a prefab for each serialized asset, get the type via the qualified name, add it to the prefab and then de-serialize with JsonUtility.