C# 在Unity中使用JSON保存游戏

C# 在Unity中使用JSON保存游戏,c#,json,unity3d,C#,Json,Unity3d,我使用Unity的JSON序列化在Unity中创建了一个简单的保存游戏系统。我的保存游戏非常简单,我只是保存角色的位置并加载它 “保存”游戏在游戏中运行良好。当我启动游戏并单击“保存”时,游戏将被保存,JSON文件将根据我的玩家位置进行更新。当我点击“加载”时,游戏加载良好,我的玩家被移动到先前保存的位置 然而,我的问题是,每当我关闭游戏并重新启动它时,“加载”功能只是将我的角色重置到我生成它的位置。它不会在我上次游戏中保存的位置加载角色 现在是我的代码。我有一个Actor类,它保存我的Acto

我使用Unity的JSON序列化在Unity中创建了一个简单的保存游戏系统。我的保存游戏非常简单,我只是保存角色的位置并加载它

“保存”游戏在游戏中运行良好。当我启动游戏并单击“保存”时,游戏将被保存,JSON文件将根据我的玩家位置进行更新。当我点击“加载”时,游戏加载良好,我的玩家被移动到先前保存的位置

然而,我的问题是,每当我关闭游戏并重新启动它时,“加载”功能只是将我的角色重置到我生成它的位置。它不会在我上次游戏中保存的位置加载角色

现在是我的代码。我有一个Actor类,它保存我的Actor数据:

using UnityEngine;
using System.Collections;
using System;

public class Actor : MonoBehaviour
{

    public ActorData data = new ActorData(); // to store our data

    public void StoreData() // for storing our data
    {
        data.pos = transform.position;
    }

    public void LoadData() // for loadinjg our data
    {
        transform.position = data.pos;
    }

    public void ApplyData()
    {
        SaveData.AddActorData(data);
    }

    void OnEnable() // to enable the save/load to avoid hanging
    {
        SaveData.OnLoaded += LoadData;
        SaveData.OnBeforeSave += StoreData;
        SaveData.OnBeforeSave += ApplyData;
    }

    void OnDisable() // to disable the save/load to avoid hanging
    {
        SaveData.OnLoaded -= LoadData;
        SaveData.OnBeforeSave -= StoreData;
        SaveData.OnBeforeSave -= ApplyData;
    }
}

[Serializable] // for storing in JSON
public class ActorData // our players attributes in JSON
{
    public Vector3 pos;
}
我还有一个ActorContainer来保存不同玩家的数据:

using System.Collections.Generic;
using System;

[Serializable]
public class ActorContainer
{

    // To store our different actors data
    public List<ActorData> actors = new List<ActorData>();
}
我的JSON文件actors.JSON包含以下内容:

using System.Linq;

// ..

public void LoadData() 
{
    // guessing you will implementing an identifier for the separate actors later..
    var currentActor = SaveData.actorContainer.First();
    transform.position = currentActor.pos;

    // also set the loaded data to the field
    data = currentActor;
}

// ..
{“演员”:[{“位置”:{“x”:419.6240539550781,“y”:5.0189208984375,“z”:0.0}]}


欢迎任何建议。

因此问题很简单,您永远不会将JSON中加载的值分配给
actior.cs中的
data.pos
。而是从公共字段
ActorData
加载,该字段未更改

如果我能看到这一点,您必须在
Actor.cs
中更改以下内容:

using System.Linq;

// ..

public void LoadData() 
{
    // guessing you will implementing an identifier for the separate actors later..
    var currentActor = SaveData.actorContainer.First();
    transform.position = currentActor.pos;

    // also set the loaded data to the field
    data = currentActor;
}

// ..

您从未将加载的值分配给
Actor.cs
中的
data.pos
。您完全正确!谢谢:)@arjwolf很高兴我能帮上忙!:D
using System.Linq;

// ..

public void LoadData() 
{
    // guessing you will implementing an identifier for the separate actors later..
    var currentActor = SaveData.actorContainer.First();
    transform.position = currentActor.pos;

    // also set the loaded data to the field
    data = currentActor;
}

// ..