Created
January 8, 2018 12:31
-
-
Save northy179/bb0a1e360f03fc81db43154a13fa4df6 to your computer and use it in GitHub Desktop.
Saving Scriptable Objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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(); | |
} | |
} | |
} |
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.
Thank you very much. This helps so much.
Thank you Bro ;)
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
.pso ??