本文整理汇总了C#中BinaryFormatter.Serialize方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryFormatter.Serialize方法的具体用法?C# BinaryFormatter.Serialize怎么用?C# BinaryFormatter.Serialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinaryFormatter
的用法示例。
在下文中一共展示了BinaryFormatter.Serialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Save
public void Save()
{
// Achievements.first.Value = true;
BinaryFormatter bf = new BinaryFormatter();
//create needed files
FileStream file = File.Create(Application.persistentDataPath + "/PlayerInfo.dat");
FileStream res = File.Create(Application.persistentDataPath + "/ResourceInfo.dat");
// Get current resource numbers
ResourceControl data = new ResourceControl();
data.money = MoneyManager.money;
data.water = MoneyManager.water;
data.food = MoneyManager.food;
data.power = MoneyManager.power;
data.buildMat = MoneyManager.buildMat;
data.rating = MoneyManager.rating;
data.pop = MoneyManager.pop;
data.conquered = MoneyManager.conquered;
data.oxygen = MoneyManager.Oxygen;
//serialize the information to the specified file
bf.Serialize(file, ConvertObjects());
bf.Serialize(res, data);
//close the files and confirm save
file.Close();
res.Close();
Debug.Log("Saved");
}
示例2: Save
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
if(File.Exists (Application.persistentDataPath + "/PlayerInfo.dat"))
{
Debug.Log("YES");
FileStream file = File.Open(Application.persistentDataPath + "/PlayerInfo.dat",FileMode.Open);
Debug.Log("Opened");
playerData data = new playerData();
Debug.Log("Created new playerData");
data.Score = Score;
Debug.Log("Before Serialize");
bf.Serialize(file,Score);
Debug.Log("After Serialize");
file.Close ();
Debug.Log("Closed");
}
else
{
Debug.Log("NO");
FileStream file = File.Create(Application.persistentDataPath + "/PlayerInfo.dat");
playerData data = new playerData();
data.Score = Score;
bf.Serialize(file,Score);
file.Close ();
}
}
示例3: SaveDefault
// Save leaderboard content locally to "persistentDataPath/Leaderboard/"
public void SaveDefault() {
string[] names = new string[10];
int[] scores = new int[10];
names[0] = "DonutDestroyer99";
scores[0] = 15;
names[1] = "Trevor Philips";
scores[1] = 10;
names[2] = "Master Chief";
scores[2] = 8;
names[3] = "Solid Snake";
scores[3] = 7;
names[4] = "Lara Croft";
scores[4] = 6;
names[5] = "Nathan Drake";
scores[5] = 5;
names[6] = "Niko Bellic";
scores[6] = 4;
names[7] = "Diabeetus";
scores[7] = 3;
names[8] = "Mickey Mouse";
scores[8] = 2;
names[9] = "Illusive Man";
scores[9] = 1;
BinaryFormatter bf = new BinaryFormatter();
var folder = Directory.CreateDirectory(Application.persistentDataPath + "/Leaderboard");
FileStream file = File.Create(Application.persistentDataPath + "/Leaderboard/names.dat");
FileStream file2 = File.Create(Application.persistentDataPath + "/Leaderboard/scores.dat");
bf.Serialize(file, names);
file.Close();
bf.Serialize(file2, scores);
file2.Close();
}
示例4: ToByteArray
public static byte[] ToByteArray(MessageData msg)
{
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, msg.type);
formatter.Serialize(stream, msg.stringData);
return stream.ToArray();
}
示例5: ToByteArray
public static byte[] ToByteArray(MessageData msg)
{
// Create a memory stream, and serialize.
MemoryStream stream = new MemoryStream();
// Create a binary formatter.
BinaryFormatter formatter = new BinaryFormatter();
// Serialize.
formatter.Serialize(stream, msg.Chaine);
formatter.Serialize(stream, msg.reel);
formatter.Serialize(stream, msg.entier);
// Now return the array.
return stream.ToArray();
}
示例6: MenuSavePbObject
public static void MenuSavePbObject()
{
pb_Object[] selection = pbUtil.GetComponents<pb_Object>(Selection.transforms);
int len = selection.Length;
if(len < 1) return;
string path = "";
if(len == 1)
path = EditorUtility.SaveFilePanel("Save ProBuilder Object", "", selection[0].name, "pbo");// "Save ProBuilder Object to File.");
else
path = EditorUtility.SaveFolderPanel("Save ProBuilder Objects to Folder", "", "");
foreach(pb_Object pb in selection)
{
//Creates a new pb_SerializableObject object.
pb_SerializableObject obj = new pb_SerializableObject(pb);
//Opens a file and serializes the object into it in binary format.
Stream stream = File.Open( len == 1 ? path : path + pb.name + ".pbo", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
stream.Close();
}
}
示例7: SaveGameData
//overwrites the file with a fresh save
public void SaveGameData()
{
bf = new BinaryFormatter();
fs = File.Create(Application.persistentDataPath + GAME_DATA_FILE);
bf.Serialize(fs, gd);
fs.Close();
}
示例8: Main
public static void Main(String[] args)
{
Console.WriteLine ("Fill MyList object with content\n");
MyList l = new MyList();
for (int x=0; x< 10; x++)
{
Console.WriteLine (x);
l.Add (x);
} // end for
Console.WriteLine("\nSerializing object graph to file {0}", FILENAME);
Stream outFile = File.Open(FILENAME, FileMode.Create, FileAccess.ReadWrite);
BinaryFormatter outFormat = new BinaryFormatter();
outFormat.Serialize(outFile, l);
outFile.Close();
Console.WriteLine("Finished\n");
Console.WriteLine("Deserializing object graph from file {0}", FILENAME);
Stream inFile = File.Open(FILENAME, FileMode.Open, FileAccess.Read);
BinaryFormatter inFormat = new BinaryFormatter();
MyList templist = (MyList)inFormat.Deserialize(inFile);
Console.WriteLine ("Finished\n");
foreach (MyListItem mli in templist)
{
Console.WriteLine ("List item number {0}, square root {1}", mli.Number, mli.Sqrt);
} //foreach
inFile.Close();
} // end main
示例9: SaveGame
public void SaveGame(SaveGame save, string filename)
{
BinaryFormatter bf = new BinaryFormatter ();
// then create a file stream that can be opened or created, with write access to it
FileStream fs = File.OpenWrite (Application.persistentDataPath + "/" + filename + ".dat");
// set up our data before writing, and we assume we have access to the game model
GameObject Player = GameObject.FindGameObjectWithTag ("Player");
GameObject Boat = GameObject.FindGameObjectWithTag ("Boat");
sg.PlayerPos = Player.GetComponent<Transform> ().localPosition;
sg.Health = Player.GetComponent<ResourceHandler> ().health;
sg.Water = Player.GetComponent<ResourceHandler> ().hydration;
sg.Lumber = Player.GetComponent<ResourceHandler> ().lumber;
sg.Palm = Player.GetComponent<ResourceHandler> ().Palm;
sg.Stone = Player.GetComponent<ResourceHandler> ().stone;
sg.String = Player.GetComponent<ResourceHandler> ().String;
sg.Axe = Player.GetComponent<ResourceHandler> ().axeON;
sg.Coconut = Player.GetComponent<ResourceHandler> ().Coconut;
sg.BoatPOS = Boat.GetComponent<Transform> ().localPosition;
// now write our stuff
bf.Serialize (fs, save);
fs.Close ();
}
示例10: Save
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create (Application.persistentDataPath + "/savedPlayers.gd");
bf.Serialize(file, savedPlayers);
file.Close();
}
示例11: GetBytes
public static byte[] GetBytes(Level data)
{
IFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, data);
return stream.ToArray();
}
示例12: setUserGameStatus
//Funcion para guardar los datos durante el juego
public static void setUserGameStatus(UserGameStatus gameStatus)
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/UserGameStatus.dat");
bf.Serialize (file, gameStatus);
file.Close ();
}
示例13: Main
static int Main ()
{
string filename = Path.Combine (AppDomain.CurrentDomain.BaseDirectory,
"encrypt.tmp");
string data = "this is sensitive data";
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
des.GenerateIV ();
des.GenerateKey ();
// ----------- WRITING ENCRYPTED SERIALIZED DATA ------------------
Stream stream = new FileStream (filename, FileMode.Create, FileAccess.Write);
stream = new CryptoStream (stream, des.CreateEncryptor (), CryptoStreamMode.Write);
BinaryFormatter bformatter = new BinaryFormatter ();
bformatter.Serialize (stream, data);
stream.Close ();
stream = null;
bformatter = null;
data = string.Empty;
// ----------- READING ENCRYPTED SERIALIZED DATA ------------------
stream = new FileStream (filename, FileMode.Open, FileAccess.Read);
stream = new CryptoStream (stream, des.CreateDecryptor (), CryptoStreamMode.Read);
bformatter = new BinaryFormatter ();
data = (string) bformatter.Deserialize (stream);
stream.Close ();
//----------- CHECK RESULTS ----------------
if (data != "this is sensitive data")
return 1;
return 0;
}
示例14: commit
public void commit()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/" + Constants.USER_DATA_FILE);
bf.Serialize(file, this);
file.Close();
}
示例15: Save
public void Save(PlayerData data){
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/Save.sav");
bf.Serialize (file, data);
file.Close ();
}