- Home /
1 script, but apply to be applied to many others
I have this basic script that will convert a basic File and turn it into a .txt file and back again. My problem is that in my Save and Load methods, I want to be able to put any file reference into it without having to make several copies of that same script.
using UnityEngine;
using System.IO;
[System.Serializable]
public class SaveLoadSystem
{
public string FileLocation;
public string Filename;
public void GetPath()
{
FileLocation = Path.Combine(Application.dataPath + "/" + FileLocation, Filename + ".txt");
}
public void Save(MonoBehaviour Data)
{
string DataInfo = JsonUtility.ToJson(Data);
File.WriteAllText(FileLocation, DataInfo);
}
public void Load(ScriptableObject Data)
{
string DataInfo = File.ReadAllText(FileLocation);
JsonUtility.FromJsonOverwrite(DataInfo, Data);
}
}
I want to be able to put this into another script and say, save Obj1 file, but not have to put (Obj1 Data) in the save and load methods. I want to make them universal~ So I can say Save Obj2 file from one script and then maybe say save Obj3 file from another one, but not have a separated script for each Obj I want to save.
Answer by ShadyProductions · Jul 28, 2017 at 05:40 PM
You can make an extension of monobehaviour
public static class SaveLoadSystem
{
public static string FileLocation; //you'll have to set this inside the save & load
public static void Save(this MonoBehaviour Data) {
string DataInfo = JsonUtility.ToJson(Data);
File.WriteAllText(FileLocation, DataInfo);
}
public static void Load(this ScriptableObject Data)
{
string DataInfo = File.ReadAllText(FileLocation);
JsonUtility.FromJsonOverwrite(DataInfo, Data);
}
}
then you can just do on any MonoBehaviour or ScriptableObject you can do .Save(); or .Load()
You will have to set FileLocation inside the save & load you could probably do the path combine with the Data.name
I got around to making the changes and I am getting lots of errors~
using UnityEngine; using System.IO;
[System.Serializable] public class SaveLoadSystem { public string FileLocation; public string Filename;
public string FileGetting;
public void GetPath()
{
FileLocation = Path.Combine(Application.dataPath + "/" + FileLocation, Filename + ".txt");
}
//Want to be able to make this get any refernce from any Serializable script
public void Save(LobbyData Data)
{
string DataInfo = JsonUtility.ToJson(Data);
File.WriteAllText(FileLocation, DataInfo);
}
//Want to be able to make this get any refernce from any Serializable script
public void Load(LobbyData Data)
{
string DataInfo = File.ReadAllText(FileLocation);
JsonUtility.FromJsonOverwrite(DataInfo, Data);
}
}
and say I have 2 other scirpts being a serailizable, I would need the Save load script to be able to take any kind of script refrence into it. So like
[Serializable] public class Data1 { public float Name } and then call the save function from the save load system.
private void Start() { saveload.Save(Data1) }
and same goes as the other serializable sricpt (ex. Data2) without having to create serveral scripts saying in the Save(Data1 Data) and Load(Data1 Data) methods
I'm not sure what you are trying to do man..
I know and I figured it out on my own time. I forgot all scripts are just Objects and there is a method that called Object that I can put into the method field. So, thank you for your help and efforts ;)
Your answer

Follow this Question
Related Questions
Adding jump animation in script editor [2D Project] 0 Answers
Can't Create Directory 1 Answer
Weird Mac %FileName 0 Answers