Home>
I make a save system in the game:
using System.Collections.Generic;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine;
public class SaveDataItems: SaveDataRaw
{
public int [] ints= new int [0];
public void PrepareForSave ()
{
string data= JsonUtility.ToJson (ints);
}
public void Load ()
{
ints= JsonUtility.FromJson <
int [] >
(data);
}
}
[Serializable]
public class SaveDataRaw
{
public string data;
public void PrepareForSave () {}
public void Load () {}
}
public class SaveManager: MonoBehaviour
{
public static void Save (string fileName, SaveDataRaw data)
{
BinaryFormatter bf= new BinaryFormatter ();
FileStream file= File.Create (Application.persistentDataPath
+ "/"+fileName+".dat");
data.PrepareForSave ();
bf.Serialize (file, data);
file.Close ();
}
public static SaveDataRaw Load (string fileName)
{
if (File.Exists (Application.persistentDataPath + "/" + fileName + ".dat"))
{
BinaryFormatter bf= new BinaryFormatter ();
FileStream file=
File.Open (Application.persistentDataPath + "/" + fileName + ".dat", FileMode.Open);
SaveDataRaw data= (SaveDataRaw) bf.Deserialize (file);
file.Close ();
return data;
}
else return new SaveDataRaw ();
}
}
But if I try to load a list of items
SaveDataItems save= (SaveDataItems) SaveManager.Load ("Items");
save.Load ();
Then an error appears at startup, tell me what you can do?
-
Answer # 1
-
Answer # 2
public static SaveDataRaw Load (string fileName)
The method returns
SaveDataRaw
, and you are trying to cast him inSaveDataItems
...(SaveDataItems) SaveManager.Load ("Items");
That is, you are trying to cast the parent's type
SaveDataRaw data
to the heir.Such a caste is impossible. You can only the heir to the parent, or the heir, previously raised to the parent, lower back to himself.
This is how you can
SaveDataItems item= new SaveDataItems (); SaveDataRaw cast= (SaveDataRaw) item;
And this is how you can
SaveDataItems item= new SaveDataItems (); SaveDataRaw upcast= (SaveDataRaw) item; SaveDataItems downcast= (SaveDataItems) upcast;
But this is impossible
SaveDataRaw item= new SaveDataRaw (); SaveDataItems cast= (SaveDataItems) item; //InvalidCastException
That is, it is not the type of the object link that is important here, but the type of the object itself under the link, which is created using
new
...I searched and found that you can make your own caste
YuraSuper20482021-12-27 05:09:11
Related questions
- c# : Unity | Can't add getting coins for watching rewarded ads
- c# : Inheriting a component from a superclass
- c# : KeyNotFoundException: The given key was not present in the dictionary
- c# : The Strategy pattern in Unity. How to fit the logic of click-to-move motion into this pattern?
- c# : Overlay background on top of each other
- c# : Incorrect Unity3D motion calculation
- c# : How do I get a component in Unity through a string variable?
- c# : Is it correct to write logic in scriptableObject?
- c# : How to walk both up and down?
The method returns
SaveDataRaw
, and you are trying to cast him inSaveDataItems
...That is, you are trying to cast the parent's type
SaveDataRaw data
to the heir.Such a caste is impossible. You can only the heir to the parent, or the heir, previously raised to the parent, lower back to himself.
This is how you can
And this is how you can
But this is impossible
That is, it is not the type of the object link that is important here, but the type of the object itself under the link, which is created using
new
...I searched and found that you can make your own caste
YuraSuper20482021-12-27 05:09:11