-
-
Notifications
You must be signed in to change notification settings - Fork 1
How to create custom data type?
Hasan Bayat edited this page May 16, 2017
·
1 revision
In this tutorial we want to learn how to create a custom data type.
Right-Click on Project window and click on Save Game Pro -> Create Save Game Type menu.
Name the file the data type that you want (in this example use Toggle) then hit Enter. (this will automatically converts your name to SaveGameType{Your entered name})
Then open the file by double clicking on it.
Your file should be something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SaveGamePro.Types
{
public class SaveGameTypeToggle : SaveGameType
{
public SaveGameTypeToggle () : base ( typeof ( Toggle ) )
{
}
public override object Read ( SaveGameReader saveGameReader )
{
// Do reading here
return null;
}
public override void Write ( object data, SaveGameWriter saveGameWriter )
{
// Do writing here
}
}
}
When you open the file you see the Toggle is red, this is because of the UnityEngine.UI namespace include it on top of your file to fix it.
Then go forward and do reading and writing.
Here is the result:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace SaveGamePro.Types
{
public class SaveGameTypeToggle : SaveGameType
{
public SaveGameTypeToggle () : base ( typeof ( Toggle ) )
{
}
public override object Read ( SaveGameReader saveGameReader )
{
Toggle toggle = SaveGameType.GetOrCreate<Toggle> ();
Read ( saveGameReader, toggle );
return toggle;
}
// We should use output object for data types like this.
public override void Read ( SaveGameReader saveGameReader, object outputObj )
{
Toggle toggle = (Toggle)outputObj;
toggle.isOn = saveGameReader.Read<bool> (); // Load the data in the order that you have saved data.
}
public override void Write ( object data, SaveGameWriter saveGameWriter )
{
Toggle toggle = (Toggle)data;
saveGameWriter.Write<bool> ( toggle.isOn );
}
}
}
Made with ❤️ by Bayat