如何保存和加载用户创建的gameObject?

时间:2019-01-13 20:30:14

标签: c# object unity3d save load

我已经实现了用户可以选择以自定义3D对象的所有选项,并添加了一些GUI按钮来改进我的旧代码并提高可用性。但是我在弄清楚如何保存和加载游戏对象时遇到了麻烦。

我已经阅读了有关序列化的信息,并且看到有人提到PlayerPrefs,但是我仍然很难理解如何将其用于对象。

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class cubeControls : MonoBehaviour
 {
     // Constants for object rotation
     public float moveSpeed = 80.0F;
     public float turnSpeed = 100.0F;

     // Initial scale of the original cube
     public static Vector3 initscale = Vector3.one;

     // Start is called before the first frame update
     void Start()
     {
     }

     // Update is called once per frame
     void Update()
     {
         // Changing the position of the object

         // Moving the object right
         if (Input.GetKey(KeyCode.D))
         {
             transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
     }

         // Moving the object left
         if (Input.GetKey(KeyCode.A))
         {
             transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
     }

         // Changing the rotation of the object

         // Rotating the cube to the right
         if (Input.GetKey(KeyCode.RightArrow))
         {
             transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
         }

         // Rotating the cube to the left
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             transform.Rotate(Vector3.down, turnSpeed * Time.deltaTime);
         }

         // Saving the current rendered material
         Renderer rend = GetComponent<Renderer>();

         // Changing the scale of the object

         // Double the size of the cube
         if (Input.GetKeyDown(KeyCode.Alpha2))
         {
             transform.localScale += new Vector3(2F, 2F, 2F);
         }

         // Changing the color via key presses

         if (Input.GetKeyDown(KeyCode.R))
         {
             rend.material.SetColor("_Color", Color.red);
         }
     }

     // To add button elements to the visual interface
     void OnGUI() 
     {
         // Changing to cylinder
         if (GUI.Button(new Rect(50, 90, 100, 40), "Cylinder"))
         {
             GameObject newCylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
             newCylinder.AddComponent<cubeControls>();
             Destroy(gameObject);
         }

         // Saving
         if (GUI.Button(new Rect(700, 330, 50, 30), "Save"))
         {
         }

         // Loading
         if (GUI.Button(new Rect(770, 330, 50, 30), "Load"))
         {
         }
     }
 }

1 个答案:

答案 0 :(得分:1)

您无法保存游戏对象。您保存的唯一内容是游戏对象的详细信息。例如,如果您具有带有某些粒子效果的3D多维数据集,则首先保存所需的多维数据集值,例如“位置”,“旋转”,“缩放”,“颜色”和其他必要的元素(需要更改的粒子发射器值)。即使在序列化中,您也将无法保存Vector3,因为它是一个结构,并且必须为向量编写自己的序列化器。简而言之,基本上是您节省了游戏对象正常工作所需的值以及其他需要用户/其他系统自定义输入的行为特征。通过保存和加载会影响对象的变量状态,将其视为将对象重建到中断状态的方法。

统一保存有两种类型

1)Player prefs:使用播放器偏好设置,您一次只能在字段中保存一个值。通常是以下三种之一:

  • 浮动
  • 整数
  • 字符串

您通常会保存令牌和其他不需要大文件的小值

2)序列化的游戏数据:在这里,您可以在序列化文件中保存大型数据集和类,例如PlayerInfo,对场景的自定义更改。

我相信您正在寻找的是第二个。因此,与其查找所有示例并感到困惑,不然您真正可以开始的是在文件中保存/加载多维数据集值(可能是位置吗?或您在运行时修改的任何内容)。逐渐地,您可以转向序列化器。

您可以检查以下链接以帮助您进一步了解。

Reference

Save/Load Data using simple BinaryFormatter

XML serialiser


此保存代码段来自简单的二进制格式化程序链接,该链接保存了某些类型的整数列表和用户得分。

[System.Serializable]
public class Save
{
  public List<int> livingTargetPositions = new List<int>();
  public List<int> livingTargetsTypes = new List<int>();

  public int hits = 0;
  public int shots = 0;
}

private Save CreateSaveGameObject()
{
  Save save = new Save();
  int i = 0;
  foreach (GameObject targetGameObject in targets)
  {
    Target target = targetGameObject.GetComponent<Target>();
    if (target.activeRobot != null)
    {
      save.livingTargetPositions.Add(target.position);
      save.livingTargetsTypes.Add((int)target.activeRobot.GetComponent<Robot>().type);
      i++;
    }
  }

  save.hits = hits;
  save.shots = shots;

  return save;
}

public void SaveGame()
{
  // 1
  Save save = CreateSaveGameObject();

  // 2
  BinaryFormatter bf = new BinaryFormatter();
  FileStream file = File.Create(Application.persistentDataPath + "/gamesave.save");
  bf.Serialize(file, save);
  file.Close();

  // 3
  hits = 0;
  shots = 0;
  shotsText.text = "Shots: " + shots;
  hitsText.text = "Hits: " + hits;

  ClearRobots();
  ClearBullets();
  Debug.Log("Game Saved");
}