Every supported data type in Databox is derived from DataboxType. Follow these instructions to create a custom databox type class.
Add the namespace Databox to the class.
using Databox;
Derive from DataboxType.
public class CustomDataClass : DataboxType {}
While deriving from DataboxType you will get access to several override methods.
public override void DrawEditor(){}
public override void DrawInitValueEditor(){}
public override void Reset(){}
public override string Equal(DataboxType _changedValue){}
Let's add a simple int value called health to our class.
[SerializeField]
private int _health;
[SerializeField]
public int InitHealth; // The initial health value
// Public property of health. By using get and set we can add an OnValueChanged callback
public int Health
{
get {return _health;}
set
{
if (value == _health){return;}
_health = value;
if (OnValueChanged != null){OnValueChanged(this);}
}
}
Several things we need to consider.
Next we will create a custom editor for our custom class. This will be drawn in the Databox editor.
public override void DrawEditor()
{
var _healthString = Health.ToString();
_healthString = GUILayout.TextField(_healthString);
int.TryParse(_healthString, out _health);
}
Let's add the additional GUI code to draw the initial value.
public override void DrawInitValueEditor()
{
GUI.color = Color.yellow;
GUILayout.Label ("Init Health:");
GUI.color = Color.white;
var _healthString = InitHealth.ToString();
_healthString = GUILayout.TextField(_healthString);
int.TryParse(_healthString, out InitHealth);
}
To make sure our reset to initial value functionality works we need to add the Reset() method.
// Reset value back to initial value
public override void Reset()
{
Health = InitHealth;
}
To make sure the cloud sync comparison works we need to add following method. We basically compare each value and if they're different we return a string with the changes.
// Important for the cloud sync comparison
public override string Equal(DataboxType _changedValue)
{
var _v = _changedValue as CustomDataClass;
if (Health != _v.Health)
{
// return original value and changed value
return Health.ToString() + " : " + _v.Health.ToString();
}
else
{
return "";
}
}
Finally we have to add the convert method to make sure the CSV import works. The convert method simply converts the string which comes from the CSV file to the appropriate data type. In this example we simply parse the string to a integer value.
// Convert the CSV string to an integer value
public override void Convert(string _value)
{
Health = int.Parse(_value);
InitHealth = Health;
}
Here's the complete custom class. After saving the file you can add data entries of type "YourCustomDataClass"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Databox;
[System.Serializable]
public class CustomDataClass : DataboxType {
[SerializeField]
private int _health;
[SerializeField]
public int InitHealth;
public int Health
{
get {return _health;}
set
{
if (value == _health){return;}
_health = value;
if (OnValueChanged != null){OnValueChanged(this);}
}
}
public override void DrawEditor()
{
var _healthString = Health.ToString();
_healthString = GUILayout.TextField(_healthString);
int.TryParse(_healthString, out _health);
}
public override void DrawInitValueEditor()
{
GUI.color = Color.yellow;
GUILayout.Label ("Init Health:");
GUI.color = Color.white;
var _healthString = InitHealth.ToString();
_healthString = GUILayout.TextField(_healthString);
int.TryParse(_healthString, out InitHealth);
}
// Reset value back to initial value
public override void Reset()
{
Health = InitHealth;
}
// Important for the cloud sync comparison
public override string Equal(DataboxType _changedValue)
{
var _v = _changedValue as CustomDataClass;
if (Health != _v.Health)
{
// return original value and changed value
return Health.ToString() + " : " + _v.Health.ToString();
}
else
{
return "";
}
}
}