Skip to content

Instantly share code, notes, and snippets.

@dainiax
Forked from northy179/examples.cs
Created August 20, 2019 20:06
Show Gist options
  • Save dainiax/059e5e4b3667d16b94e867ec9c5d303b to your computer and use it in GitHub Desktop.
Save dainiax/059e5e4b3667d16b94e867ec9c5d303b to your computer and use it in GitHub Desktop.
Saving Scriptable Objects
/*Copyright 2018 Neil North
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
//ScriptableInt.cs
using UnityEngine;
[CreateAssetMenu]
public class ScriptableInt: ScriptableObject {
public int Value;
}
//TextUpdater.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextUpdater : MonoBehaviour {
public ScriptableInt myInt;
public Text myText;
protected void Start()
{
refresh();
}
public void add(int amount) {
myInt.Value = myInt.Value + amount;
refresh();
}
void refresh() {
myText.text = myInt.Value.ToString();
}
}
//PersistableSO.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class PersistableSO : MonoBehaviour {
[Header("Meta")]
public string persisterName;
[Header("Scriptable Objects")]
public List<ScriptableObject> objectsToPersist;
protected void OnEnable()
{
for (int i = 0; i < objectsToPersist.Count; i++)
{
if (File.Exists(Application.persistentDataPath + string.Format("/{0}_{1}.pso", persisterName, i )))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + string.Format("/{0}_{1}.pso", persisterName, i ), FileMode.Open);
JsonUtility.FromJsonOverwrite((string)bf.Deserialize(file),objectsToPersist[i]);
file.Close();
} else
{
//Do Nothing
}
}
}
protected void OnDisable()
{
for (int i = 0; i < objectsToPersist.Count; i++)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + string.Format("/{0}_{1}.pso", persisterName, i ));
var json = JsonUtility.ToJson(objectsToPersist[i]);
bf.Serialize(file, json);
file.Close();
}
}
}
@snack-gamer
Copy link

change the .pso to .txt if anyone is bothered by the strange extension name :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment