Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:如何使用数组而不是Unity中的列表重写用于管理游戏数据序列化的代码?_C#_Unity3d - Fatal编程技术网

C#:如何使用数组而不是Unity中的列表重写用于管理游戏数据序列化的代码?

C#:如何使用数组而不是Unity中的列表重写用于管理游戏数据序列化的代码?,c#,unity3d,C#,Unity3d,我对编码相当陌生,目前正致力于创建脚本以将数据序列化到硬盘驱动器。我已经设法让它与类型为“游戏”的列表一起工作。Game类保存可以序列化的数据。我一直试图编写这段代码来处理一个大小为3的数组。不幸的是,当我运行程序并尝试使用GUI时,我遇到了麻烦。我只需要在正确的方向上推动如何重写我的代码。我知道从以下几点开始: public static Game[] savedGames = new Game[3]; 但是,如何使用数组而不是列表来获得定义呢 //<SaveLoad.cs>

我对编码相当陌生,目前正致力于创建脚本以将数据序列化到硬盘驱动器。我已经设法让它与类型为“游戏”的列表一起工作。Game类保存可以序列化的数据。我一直试图编写这段代码来处理一个大小为3的数组。不幸的是,当我运行程序并尝试使用GUI时,我遇到了麻烦。我只需要在正确的方向上推动如何重写我的代码。我知道从以下几点开始:

public static Game[] savedGames = new Game[3];
但是,如何使用数组而不是列表来获得定义呢

//<SaveLoad.cs>

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;

public static class SaveLoad {
    // Note: [ 07 / 20 / 2018]
    // 1. Make  an array of game saves index of 3. (To replace list) 
    // 2. Create definition for overwritesave()??
    // 3. Rewrite definitions Save/Load/Delete to work with an array
    // 4. Start working on custom GUI

public static List<Game> savedGames = new List<Game>(3);

public static void Save()
{
    savedGames.Add(Game.current);
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Create(Application.persistentDataPath + 
    "/savedGames.gd");
    Debug.Log("The file is saved at " + Application.persistentDataPath);
    bf.Serialize(file, savedGames);
    file.Close();
}

public static void Load()
{
    if (File.Exists(Application.persistentDataPath + "/savedGames.gd"))
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Open(Application.persistentDataPath + 
        "/savedGames.gd", FileMode.Open);
        Debug.Log("The File is loaded from " + 
        Application.persistentDataPath);
        savedGames = (List<Game>)bf.Deserialize(file);
        file.Close();
    }
}

public static void DeleteSave(int index) {

    if (File.Exists(Application.persistentDataPath + "/savedGames.gd"))
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Open(Application.persistentDataPath + 
        "/savedGames.gd", FileMode.Open);
        savedGames = (List<Game>)bf.Deserialize(file);
        file.Close();
    }

    savedGames.RemoveAt(index); 
    // Removes element in List from the passed Index

    BinaryFormatter bf2 = new BinaryFormatter();
    FileStream file2 = File.Create(Application.persistentDataPath + 
    "/savedGames.gd"); //you can call it anything you want
    Debug.Log("Save file has been Updated at " + 
    Application.persistentDataPath);
    bf2.Serialize(file2, savedGames); // Saves Changes to Data
    file2.Close();
    }
 }
//
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
使用System.Runtime.Serialization.Formatters.Binary;
使用System.IO;
使用制度;
公共静态类存储负载{
//注:[07/20/2018]
//1.制作一个索引为3的游戏保存数组。(替换列表)
//2.为overwritesave()创建定义??
//3.重写定义保存/加载/删除以使用数组
//4.开始使用自定义GUI
public static List savedGames=新列表(3);
公共静态void Save()
{
savedGames.Add(Game.current);
BinaryFormatter bf=新的BinaryFormatter();
FileStream file=file.Create(Application.persistentDataPath+
“/savedGames.gd”);
Log(“文件保存在”+Application.persistentDataPath);
序列化(文件,保存的名称);
file.Close();
}
公共静空荷载()
{
if(File.Exists(Application.persistentDataPath+“/savedGames.gd”))
{
BinaryFormatter bf=新的BinaryFormatter();
FileStream file=file.Open(Application.persistentDataPath+
“/savedGames.gd”,FileMode.Open);
Log(“该文件是从”+
persistentDataPath);
savedGames=(列表)bf.反序列化(文件);
file.Close();
}
}
公共静态void DeleteSave(int索引){
if(File.Exists(Application.persistentDataPath+“/savedGames.gd”))
{
BinaryFormatter bf=新的BinaryFormatter();
FileStream file=file.Open(Application.persistentDataPath+
“/savedGames.gd”,FileMode.Open);
savedGames=(列表)bf.反序列化(文件);
file.Close();
}
savedGames.RemoveAt(索引);
//从传递的索引中删除列表中的元素
BinaryFormatter bf2=新的BinaryFormatter();
FileStream file2=File.Create(Application.persistentDataPath+
“/savedGames.gd”);//您可以随意调用它
Debug.Log(“保存文件已在”+
persistentDataPath);
序列化(file2,savedGames);//保存对数据的更改
file2.Close();
}
}
这是我的GUI脚本。我目前有一个列表,但我有麻烦使用它与数组以及工作。我需要帮助找到一种方法,从菜单中传递一个索引值来修改数组

//<MainMenu.cs>

 using UnityEngine;
 using System;
 using UnityEngine.SceneManagement;

 public class MainMenu : MonoBehaviour {

 int index = 0;

 public enum Menu {
    MainMenu,
    NewGame,
    Continue,
    delete,
    caution
}

 public Menu currentMenu;

     void OnGUI () {

         GUILayout.BeginArea(new Rect(0,0,Screen.width, Screen.height));
         GUILayout.BeginHorizontal();
         GUILayout.FlexibleSpace();
         GUILayout.BeginVertical();
         GUILayout.FlexibleSpace();

        if(currentMenu == Menu.MainMenu) {

            GUILayout.Box("THE EIGHT");
            GUILayout.Space(10);

        if(GUILayout.Button("New Game")) {
            Game.current = new Game();
            currentMenu = Menu.NewGame;
        }

        if(GUILayout.Button("Continue")) {
            SaveLoad.Load();
            currentMenu = Menu.Continue;
        }

        if(GUILayout.Button("Delete Save")) {
            SaveLoad.Load();
            currentMenu = Menu.delete;
        }

        if(GUILayout.Button("Quit")) {
            Application.Quit();
        }
    }

    else if (currentMenu == Menu.NewGame) {

        GUILayout.Box("Name Your Characters");
        GUILayout.Space(10);

        GUILayout.Label("Some Strange Boy");
        Game.current.Nestor.name = 
        GUILayout.TextField(Game.current.Nestor.name, 20);
        GUILayout.Label("Some Strange Girl");
        Game.current.SomeGirl.name = 
        GUILayout.TextField(Game.current.SomeGirl.name, 20);
        GUILayout.Label("Some Strange Friend");
        Game.current.SomeBro.name = 
        GUILayout.TextField(Game.current.SomeBro.name, 20);

        if(GUILayout.Button("Save")) {
            //Save the current Game as a new saved Game
            SaveLoad.Save();
            //Move on to game...
            SceneManager.LoadScene(2);
        }

        GUILayout.Space(10);
        if(GUILayout.Button("Cancel")) {
            currentMenu = Menu.MainMenu;
        }

    }

    else if (currentMenu == Menu.Continue) {

        GUILayout.Box("Select Save");
        GUILayout.Space(10);

        foreach(Game g in SaveLoad.savedGames) {
            if(GUILayout.Button(g.Nestor.name + " - " + g.SomeGirl.name + " - " + g.SomeBro.name)) {
                Game.current = g;
                //Move on to game...
                SceneManager.LoadScene(2);
            }

        }

        GUILayout.Space(10);
        if(GUILayout.Button("Cancel")) {
            currentMenu = Menu.MainMenu;
        }

    }

    else if (currentMenu == Menu.delete)
    {

        GUILayout.Box("Select Save to Delete");
        GUILayout.Space(10);

        foreach (Game g in SaveLoad.savedGames)
        {
            if (GUILayout.Button(g.Nestor.name + " - " + g.SomeGirl.name + " 
            - " + g.SomeBro.name))
            {
                index = SaveLoad.savedGames.IndexOf(g);
                Debug.Log(index);
                currentMenu = Menu.caution;
            }

        }

        GUILayout.Space(10);
        if (GUILayout.Button("Cancel"))
        {
            currentMenu = Menu.MainMenu;
        }

    }
else if(currentMenu == Menu.caution) {

        GUILayout.Box("Are you sure?");
        GUILayout.Space(10);
        if (GUILayout.Button("Yes")) {
            Debug.Log(index);
            SaveLoad.DeleteSave(index);
            currentMenu = Menu.MainMenu;
        }

           GUILayout.Space(10);
            if (GUILayout.Button("No")) {
                currentMenu = Menu.delete;
            }

        }

        GUILayout.FlexibleSpace();
        GUILayout.EndVertical();
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();
        GUILayout.EndArea();

    }
}
//
使用UnityEngine;
使用制度;
使用UnityEngine.SceneManagement;
公共类主菜单:MonoBehavior{
int指数=0;
公共枚举菜单{
主菜单,
新游戏,
继续,,
删除,
小心
}
公共菜单;
void OnGUI(){
BeginArea(新的Rect(0,0,Screen.width,Screen.height));
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.beginstical();
GUILayout.FlexibleSpace();
如果(currentMenu==Menu.MainMenu){
Guillayout.Box(“八”);
空间(10);
if(GUILayout.Button(“新游戏”)){
Game.current=新游戏();
currentMenu=Menu.NewGame;
}
if(GUILayout.按钮(“继续”)){
SaveLoad.Load();
当前菜单=菜单。继续;
}
if(GUILayout.按钮(“删除保存”)){
SaveLoad.Load();
currentMenu=Menu.delete;
}
if(GUILayout.按钮(“退出”)){
Application.Quit();
}
}
else if(currentMenu==Menu.NewGame){
GUILayout.Box(“说出你的角色”);
空间(10);
标签(“某个奇怪的男孩”);
Game.current.Nestor.name=
text字段(Game.current.Nestor.name,20);
标签(“某个陌生女孩”);
Game.current.SomeGirl.name=
GUILayout.TextField(Game.current.SomeGirl.name,20);
标签(“某个奇怪的朋友”);
Game.current.SomeBro.name=
text字段(Game.current.SomeBro.name,20);
if(GUILayout.按钮(“保存”)){
//将当前游戏另存为新保存的游戏
SaveLoad.Save();
//继续游戏。。。
场景管理器。加载场景(2);
}
空间(10);
if(GUILayout.按钮(“取消”)){
currentMenu=Menu.MainMenu;
}
}
else if(当前菜单==菜单。继续){
GUILayout.Box(“选择保存”);
空间(10);
foreach(SaveLoad.savedGames中的游戏g){
if(GUILayout.Button(g.Nestor.name+“-”+g.SomeGirl.name+“-”+g.SomeBro.name)){
Game.current=g;
//继续游戏。。。
场景管理器。加载场景(2);
}
}
空间(10);
if(GUILayout.按钮(“取消”)){
currentMenu=Menu.MainMenu;
}
}
else if(currentMenu==Menu.delete)
{
GUILayout.Box(“选择保存以删除”);
空间(10);
foreach(SaveLoad.savedGames中的游戏g)
{
if(GUILayout.Button)(g.Nestor.name+“-”+g.SomeGirl.name+“)
-“+g.SomeBro.name”)
{
index=SaveLoad.savedGames.IndexOf(g);
Debug.Log(索引);
currentMenu=Menu.caution;
}
}
空间(10);
如果
void Awake() {
    //this is an object initializer, pass your Game[] in place of this
    Save(new[] {
        new Game { propertyStub = 0 },
        new Game { propertyStub = 1 },
        new Game { propertyStub = 2 }
    });

    var gA = Load();
    if(gA == null || gA.Length > 3) throw new SerializationException("Serialize error");

    foreach (var g in gA) Debug.Log(g);
}

public static void Save(Game[] gA) {
    using (var f = File.Create(Directory.GetCurrentDirectory() + "\\sample.bin")) new BinaryFormatter().Serialize(f, gA);
}

public static Game[] Load() {
    using (var f = File.Open(Directory.GetCurrentDirectory() + "\\sample.bin", FileMode.Open)) return (new BinaryFormatter().Deserialize(f)) as Game[];
}

//Stub class used for testing purposes
[Serializable]
class Game {
    public int propertyStub { get; set; }

    public override string ToString() {
        return "propertyStub value: " + propertyStub;
    }
}