当前位置: 首页>>代码示例>>C#>>正文


C# BinaryFormatter.Serialize方法代码示例

本文整理汇总了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");
       
    }
开发者ID:Ellesent,项目名称:EcoGalaxy,代码行数:31,代码来源:GameControl.cs

示例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 ();
        }
    }
开发者ID:galalmounir,项目名称:Flying-Dino,代码行数:30,代码来源:GameVariables.cs

示例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();
    }
开发者ID:jakrhar,项目名称:pcogd2016_carchase,代码行数:36,代码来源:Leaderboard.cs

示例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();
    }
开发者ID:Aileenche,项目名称:Space,代码行数:10,代码来源:MessageData.cs

示例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();
    }
开发者ID:MARWEN-KHIARI,项目名称:LocalNetworkUnityProject,代码行数:15,代码来源:MessageData.cs

示例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();			
		}
	}
开发者ID:Rikarie,项目名称:Kingdom,代码行数:29,代码来源:SaveLoadPbObjects.cs

示例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();
 }
开发者ID:AdamBoyce,项目名称:UnitySaveLoadExample,代码行数:8,代码来源:SaveLoadBehaviour.cs

示例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
开发者ID:ArildF,项目名称:masters,代码行数:29,代码来源:simpleserialize.cs

示例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 ();
    }
开发者ID:Nic-vanDessel,项目名称:2DGameProject_island-survival,代码行数:26,代码来源:LoadScript.cs

示例10: Save

 public void Save()
 {
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create (Application.persistentDataPath + "/savedPlayers.gd");
     bf.Serialize(file, savedPlayers);
     file.Close();
 }
开发者ID:claytantor,项目名称:shadow-haven-unity,代码行数:7,代码来源:PlayerManger.cs

示例11: GetBytes

 public static byte[] GetBytes(Level data)
 {
     IFormatter formatter = new BinaryFormatter();
     MemoryStream stream = new MemoryStream();
     formatter.Serialize(stream, data);
     return stream.ToArray();
 }
开发者ID:alexkirwan29,项目名称:Cubes-Of-Wrath,代码行数:7,代码来源:LevelIO.cs

示例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 ();
 }
开发者ID:LightLex,项目名称:RoboRacers,代码行数:8,代码来源:Controller.cs

示例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;
	}
开发者ID:mono,项目名称:gert,代码行数:34,代码来源:test.cs

示例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();
 }
开发者ID:achievegame,项目名称:MineDrill,代码行数:7,代码来源:PlayerSave.cs

示例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 ();
	}
开发者ID:imann24,项目名称:cs327-bestmobilegameever,代码行数:7,代码来源:SaveLoad.cs


注:本文中的BinaryFormatter.Serialize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。