Here I will publish code to illustrate the various usages of ScriptableObject Pro.
This code is copyright SteveSmith.Software 2022. But may be used as is or in a modified form under the MIT License.
No warranty of useability or support will be applicable for the use of code derived from these examples.
More example code can be found at ScriptableObject Pro on GitHub
Mutable Class
Apply the [Mutable] attribute to a class.
All of the serializable fields in the class will become mutable
using System;
using UnityEngine;
using SSSoftware.Attributes;
[CreateAssetMenu(fileName = "New MySO", menuName = "MySO")]
[Mutable]
public class MySO : SSSoftware.SOPro.ScriptableObject
{
public Vector3[] waypoints;
public Vector3 currPosition;
}
<- Previous
Next ->
Mutable Fields
Apply the [Mutable] attribute to a field.
Only those fields with the attribute will be mutable
using System;
using UnityEngine;
using SSSoftware.Attributes;
[CreateAssetMenu(fileName = "New MySO", menuName = "MySO")]
public class MySO : SSSoftware.SOPro.ScriptableObject
{
public Vector3[] waypoints;
[Mutable]
public Vector3 currPosition;
}
In this example changes to currPosition will be persisted. Changes to waypoints will NOT persist.
<- Previous
Next ->
Immutable Fields
Apply the [Mutable] attribute to a class.or to fields
Apply the [Immutable] attribute to fields to be ignored
using System;
using UnityEngine;
using SSSoftware.Attributes;
[CreateAssetMenu(fileName = "New MySO", menuName = "MySO")]
[Mutable]
public class MySO : SSSoftware.SOPro.ScriptableObject
{
[Immutable]
public Vector3[] waypoints;
public Vector3 currPosition;
}
using System;
using UnityEngine;
using SSSoftware.Attributes;
[CreateAssetMenu(fileName = "New MySO", menuName = "MySO")]
public class MySO : SSSoftware.SOPro.ScriptableObject
{
[Immutable]
public Vector3[] waypoints;
[Mutable]
public Vector3 currPosition;
}
In these examples changes to currPosition will be persisted. Changes to waypoints will NOT persist.
<- Previous
Next ->
User defined Save
using System;
using UnityEngine;
using SSSoftware.Attributes;
[CreateAssetMenu(fileName = "New MySO", menuName = "MySO")]
[Mutable]
public class MySO : SSSoftware.SOPro.ScriptableObject
{
public Vector3 currPosition;
}
using UnityEngine;
public class MyScript : MonoBehaviour
{
public MySO so;
void Start()
{
transform.localPosition = so.currPosition;
so.AutoSaveOff(); // Do not autosave
}
void Update()
{
so.currPosition = transform.position;
so.Save(); // Save every Update
}
void OnApplicationQuit()
{
so.currPosition = transform.position;
so.Save(); // Save On Exit
}
}
<- Previous
Next ->
AutoSave
using System;
using UnityEngine;
using SSSoftware.Attributes;
[CreateAssetMenu(fileName = "New MySO", menuName = "MySO")]
[Mutable]
public class MySO : SSSoftware.SOPro.ScriptableObject
{
public Vector3 currPosition;
}
using UnityEngine;
public class MyScript : MonoBehaviour
{
public MySO so;
void Start()
{
transform.localPosition = so.currPosition;
so.AutoSaveOn(0.5f); // Autosave every half second and On Exit
}
void Update()
{
so.currPosition = transform.position;
}
}
<- Previous
Next ->
Persist Unity Objects
Persisting UnityObjects across builds could cause instability in the ScriptableObject save file.
Using an index into an array of objects stored on a MonoBehaviour will mitigate this potential problem.
Before (May NOT Persist across builds)
using System;
using UnityEngine;
using SSSoftware.Attributes;
[CreateAssetMenu(fileName = "New MySO", menuName = "MySO")]
[Mutable]
[PersistAcrossBuilds]
public class MySO : SSSoftware.SOPro.ScriptableObject
{
public Material currMat;
}
using UnityEngine;
public class MyScript : MonoBehaviour
{
public MySO so;
void Start()
{
if (so.currMat != null)
{
MeshRenderer mr = GetComponent<MeshRenderer>();
mr.material = so.currMat;
}
}
}
After (Will persist across builds)
using System;
using UnityEngine;
using SSSoftware.Attributes;
[CreateAssetMenu(fileName = "New MySO", menuName = "MySO")]
[Mutable]
[PersistAcrossBuilds]
public class MySO : SSSoftware.SOPro.ScriptableObject
{
public int currMat;
}
using UnityEngine;
public class MyScript : MonoBehaviour
{
public MySO so;
public Material[] materials;
void Start()
{
MeshRenderer mr = GetComponent<MeshRenderer>();
mr.material = materials[so.currMat];
}
}
<- Previous
Next ->