Skip to content

Instantly share code, notes, and snippets.

@northy179
Created January 8, 2018 12:31
Show Gist options
  • Save northy179/bb0a1e360f03fc81db43154a13fa4df6 to your computer and use it in GitHub Desktop.
Save northy179/bb0a1e360f03fc81db43154a13fa4df6 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();
}
}
}
@itsSwArchitect
Copy link

what is mean by """" "/{0}_{1}.pso" """"

@itsSwArchitect
Copy link

.pso ??

@Vendrad
Copy link

Vendrad commented Sep 11, 2018

pso = persistable scriptable object

He's used this as the filetype but it really does not matter what you type in there.

{0} and {1} refer to the 2nd and 3rd parameters passed in.

@fidelsoto
Copy link

Thank you very much. This helps so much.

@furkanselcuk11
Copy link

Thank you Bro ;)

@BugsBunny421
Copy link

But we can't save references to other objects. This can work for simple types like ints, Lists etc. But It will break when you assign references to other Scriptable Objects or Prefabs. As Unity will only serialize their Instance IDs which is inconsistent between different sessions.

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