Hi I use a c# script to save and load. Now i want to access the c# script variables with a Javascript script. But this won't work like i want to.( I moved the cs script in the Standard Asset folder)
SaveDataCS.cs:
using UnityEngine; // For Debug.Log, etc.
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.Runtime.Serialization;
using System.Reflection;
public class SaveDataCS : MonoBehaviour
{
void Start () {
}
// === This is the info container class ===
[Serializable ()]
public class SaveData : ISerializable {
// === Values ===
// Edit these during gameplay
public bool foundGem1 = false;
public float score= 42 ;//= 42;
public int levelReached = 3;
public int[] HighscoreArraySave = new int[10]; //highscorearrsave
public int[] MedalArraySave = new int[10]; //medalarrsave
public float[] TimeArraySave = new float[10];//timearr
public int LevelSave = 0; // level
public string PlayerNameSave = "Playername";
// === /Values ===
// The default constructor. Included for when we call it during Save() and Load()
public SaveData () {}
// This constructor is called automatically by the parent class, ISerializable
// We get to custom-implement the serialization process here
public SaveData (SerializationInfo info, StreamingContext ctxt)
{
// Get the values from info and assign them to the appropriate properties. Make sure to cast each variable.
// Do this for each var defined in the Values section above
foundGem1 = (bool)info.GetValue("foundGem1", typeof(bool));
score = (float)info.GetValue("score", typeof(float));
levelReached = (int)info.GetValue("levelReached", typeof(int));
HighscoreArraySave = (int[])info.GetValue("HighscoreArraySave", typeof(int[]));
MedalArraySave = (int[])info.GetValue("MedalArraySave", typeof(int[]));
TimeArraySave = (float[])info.GetValue("TimeArraySave", typeof(float[]));
LevelSave = (int)info.GetValue("LevelSave", typeof(int));
PlayerNameSave = (string)info.GetValue("PlayerNameSave", typeof(string));
}
// Required by the ISerializable class to be properly serialized. This is called automatically
public void GetObjectData (SerializationInfo info, StreamingContext ctxt)
{
// Repeat this for each var defined in the Values section
info.AddValue("foundGem1", (foundGem1));
info.AddValue("score", score);
info.AddValue("levelReached", levelReached);
info.AddValue("HighscoreArraySave",(HighscoreArraySave));
info.AddValue("MedalArraySave",(MedalArraySave));
info.AddValue("TimeArraySave",(TimeArraySave));
info.AddValue("LevelSave",(LevelSave));
info.AddValue("PlayerNameSave",(PlayerNameSave));
}
}
// === This is the class that will be accessed from scripts ===
public class SaveLoad {
public static string currentFilePath = "SaveData.cjc"; // Edit this for different save files
// Call this to write data
public static void Save () // Overloaded
{
Save (currentFilePath);
}
public static void Save (string filePath)
{
SaveData data = new SaveData ();
Stream stream = File.Open(filePath, FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Binder = new VersionDeserializationBinder();
bformatter.Serialize(stream, data);
stream.Close();
Debug.Log("Gesaved2"+ data);
}
// Call this to load from a file into "data"
public static void Load () { Load(currentFilePath); } // Overloaded
public static void Load (string filePath)
{
SaveData data = new SaveData ();
Stream stream = File.Open(filePath, FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Binder = new VersionDeserializationBinder();
data = (SaveData)bformatter.Deserialize(stream);
stream.Close();
Debug.Log("Geladen2" + data);
// Now use "data" to access your Values
}
}
// === This is required to guarantee a fixed serialization assembly name, which Unity likes to randomize on each compile
// Do not change this
public sealed class VersionDeserializationBinder : SerializationBinder
{
public override Type BindToType( string assemblyName, string typeName )
{
if ( !string.IsNullOrEmpty( assemblyName ) && !string.IsNullOrEmpty( typeName ) )
{
Type typeToDeserialize = null;
assemblyName = Assembly.GetExecutingAssembly().FullName;
// The following line of code returns the type.
typeToDeserialize = Type.GetType( String.Format( "{0}, {1}", typeName, assemblyName ) );
return typeToDeserialize;
}
return null;
}
}
}
and with this JS script it try to access the variables in the SaveData class:
function Update () {
if (Input.GetKeyUp(KeyCode.S) ){
SaveDataCS.SaveLoad.Save(); // this works fine
}
if (Input.GetKeyUp(KeyCode.L) ){
SaveDataCS.SaveLoad.Load(); // this works fine too
}
if (Input.GetKeyUp(KeyCode.H) ){
var other : SaveDataCS;
other = gameObject.GetComponent("SaveDataCS"); // this doesn't work
print(other.score);
other.score=10; //
}
if (Input.GetKeyUp(KeyCode.K) ){
print(SaveDataCS.score);
SaveDataCS.score = 10; // this doesn't work too
}
}
I think its the fact that there are to many classes in the C# script. But maybe someone knows a solution. Thanks for answers.
edit1: Both Scripts are on the same GameObject.
Nee, leider nicht ;). Unfortunately i wasn't in need of such a saving method yet. In general binary serialization can be used to save any objects data. You used an extra object to store only relevant data, but you have to copy your actual data into this class before you serialize it and after deserialization you have to copy the data back. I can add a small example.
– Bunny83