Data System
System Type | Core |
---|---|
Revision | 1 |
Last Updated |
+
The data setup is designed to aid in storing data related to the project in scriptable objects for ease of access at runtime. Without adding the overhead of needed to assign references for the assets.
+
+
-Usage
The data system has two main elements to it. The data assets and the data asset index. The assets are what hold the data you define. With you making classes that implement the class to store data. While the index keeps a reference of all the data assets in the project for use at runtime.
+
Creating data assets
To create a data asset, either inherit from the DataAsset
class or use the provided tool that will create the necessary classes for you. You can find the creation tool under:
Tools/Carter Games/The Cart/Core/Data/Asset Creator
This will open an editor window that’ll let you make a new data asset class. To use the tool, you simple enter the name you want the class to have a press create. A popup will then appear to let you choose where it should be saved. After which you’ll get the chance to open the newly created class for editing in your chosen IDE.
+
+
You can also make the class manually by making a new class that inherit from DataAsset
An example below:
[CreateAssetMenu] // So you can make an instance in the project.
+public class DataAssetLevels : DataAsset
+{
+ // Your data here.
+}
+
+
Referencing data assets
You can reference data assets in two main ways. Either a direct reference in the inspector like you normally would with any field. Or by getting the asset from the DataAccess
class. The data access class
private void OnEnable()
+{
+ // Gets the first asset of the type found.
+ var asset = DataAccess.GetAsset<DataAssetLevels>();
+
+ // Gets the asset of the matching variant id.
+ asset = DataAccess.GetAsset<DataAssetLevels>("MyAssetVariantId");
+
+ // Gets all of the assets of the type found.
+ var assets = DataAccess.GetAssets<DataAssetLevels>();
+}
+
+
Data asset index
The data asset index is a scriptable object that stores a reference to all the data assets. This is used to performantly allow referencing to the assets through the DataAccess
class. Avoiding expensive operations like Resources.Load()
. The system will automatically make the data asset index if it doesn’t exist and update it with any new assets when entering play mode or before a build is made to ensure it is up to date. Should you need to update it manually, you can do so from the following menu item:
Tools/Carter Games/The Cart/Core/Data/Update Asset Index
+
+
- Scripting
Assembly
CarterGames.TheCart.Core
+
Namespace
CarterGames.Cart.Core.Data
- - API
+
- Data Access
+
GetAsset
Gets the data asset of the defined type. If there is more than one and you don’t define the id it will pick the first one it finds. Will return null if none are found.
DataAssetMyData dataAsset;
+
+private void OnEnable()
+{
+ // Gets the first one found.
+ dataAsset = DataAccess.GetAsset<DataAssetMyData>();
+
+ // Gets the data asset with the variant id of "MyVariantId".
+ dataAsset = DataAccess.GetAsset<DataAssetMyData>("MyVariantId");
+}
+
+
GetAssets
Gets all the data assets of the defined type that are found in the project. Will return null if none are found.
List<DataAssetMyData> dataAssets;
+
+private void OnEnable()
+{
+ // Gets all the found assets of the entered type.
+ dataAssets = DataAccess.GetAssets<DataAssetMyData>();
+}
+
+
+
+
- Data Asset
+
VariantId
A unique Id that can be used to identify the data asset for use with the data access class. By default a random Guid will be used to populate the field.
+
+
+