This document will explain about how to export and import Unity's Scriptable Object. For general information, see the Repository's README.
ScriptableObject exporter and importer uses Unity's ScriptableObject asset as data container. So you can inspect the data, reference specific row from MonoBehaviour, or even make it Addressable.
You can create ScriptableObject assets using ScriptableObjectSheetExporter
.
The path is the base path of ScriptableObject will be saved, relative to project path.
var exporter = new ScriptableObjectSheetExporter("Assets/ScriptableObject");
await sheetContainer.Store(exporter);
You can inspect each row from Unity's inspector.
You can import from ScriptableObject assets using ScriptableObjectSheetImporter
. A reference to SheetContainerScriptableObject
is required.
public class SheetManager : MonoBehaviour
{
private SheetContainer container;
public SheetContainerScriptableObject containerSO;
public async void Start()
{
var importer = new ScriptableObjectSheetImporter(containerSO);
container = new SheetContainer(UnityLogger.Default);
await container.Bake(importer);
}
}
You can drag-drop _Container
ScriptableObject to your SheetManager
component.
You can use Sheet.Reference
(Same as cross-sheet reference) to reference specific Row from inspector.
public class SampleAccessToScriptableRow : MonoBehaviour
{
public HeroSheet.Reference hero;
public ConstantSheet.Reference constant;
public List<DungeonSheet.Reference> dungeons;
public void Awake()
{
Debug.Log(hero.Ref.Name);
Debug.Log(constant.Ref.ValueInt);
foreach (var dungeon in dungeons)
{
foreach (var item in dungeon.Ref.Items)
Debug.Log(item.Ref.Name);
}
}
}
Note that while you can reference value of each row with this way, PostLoad
will not be called until you import container with ScriptableObjectImporter
.
DirectAssetPath
will be automatically converted to asset reference when you export to ScriptableObject.
Depends on your usage. If you'd like to integrate data with Unity editor or use Addressable assets to update datasheet, you can go for ScriptableObject converter.
If you rather manage your data as raw JSON, or wants to send it directly over wire, then you can use JSON converter.
You can open the ScriptableObject file directly from your IDE, find & replace name of sub-assets to match with new Id.
This way you can keep all the references to your ScriptableObject when importing again.