Search Unity

Is it possible to add a Prefab as a Sub-Asset to a Scriptable Object?

Discussion in 'Prefabs' started by AndrewKaninchen, Apr 22, 2019.

  1. AndrewKaninchen

    AndrewKaninchen

    Joined:
    Oct 30, 2016
    Posts:
    149
    I think it's impossible, but I'd really like to do something like that.
     
  2. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    This is not possible.
    It is possible to add a ScriptableObject as a sub-asset to a prefab
     
  3. AndrewKaninchen

    AndrewKaninchen

    Joined:
    Oct 30, 2016
    Posts:
    149
    Well, I'm sad, but thanks. Good to know that though. I don't think I've ever tried it, will keep it in mind.
     
  4. PlayCreatively

    PlayCreatively

    Joined:
    Jan 25, 2016
    Posts:
    94
    I don't seem to be able to do that. Here's the code
    Code (CSharp):
    1. var area = CreateInstance<AreaDataSO>();
    2. area.name = "SubAsset";
    3. AssetDatabase.AddObjectToAsset(area, Selection.activeObject);
    4. AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(area));
    It just adds a scriptable object to what I select and it works on other scriptable objects but not on prefabs.
     
  5. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    @PlayCreatively

    This should work

    Code (CSharp):
    1.     var area = CreateInstance<AreaDataSO>();
    2.     area.name = "SubAsset";
    3.     AssetDatabase.AddObjectToAsset(area, Selection.activeObject);
    4.    
    5.     if (prefab)
    6.         PrefabUtility.SavePrefabAsset(Selection.activeObject)
    7.  
    After adding an object to the prefab you need to save prefab again in order for that file in the assets folder to be updated.
     
    PlayCreatively likes this.
  6. caxapexac

    caxapexac

    Joined:
    Feb 27, 2018
    Posts:
    6
    This is crahing editor
    Can you post full working example please?