C# 对象';政府立场及;未保存/加载旋转(Unity引擎)

C# 对象';政府立场及;未保存/加载旋转(Unity引擎),c#,unity3d,save,position,load,C#,Unity3d,Save,Position,Load,我需要保存球员在比赛中的位置和轮换。为此,我使用二进制格式化程序和两个按钮:“保存”和“加载”。如果我手动写入、保存并再次加载场景,脚本将保存公共矢量3数据。但是,玩家(只是一个立方体)不会改变其位置和旋转。为了解决这个问题,我补充道: void FixedUpdate() { position = player.transform.position; rotation = player.transform.rotation; } 但这没有帮助

我需要保存球员在比赛中的位置和轮换。为此,我使用二进制格式化程序和两个按钮:“保存”和“加载”。如果我手动写入、保存并再次加载场景,脚本将保存公共矢量3数据。但是,玩家(只是一个立方体)不会改变其位置和旋转。为了解决这个问题,我补充道:

void FixedUpdate()
    {
        position = player.transform.position;
        rotation = player.transform.rotation;
    } 
但这没有帮助。我使用2个脚本来实现这一点:

Player.cs

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

public class Player : MonoBehaviour
{
    public GameObject player;
    public Vector3 position;
    public Quaternion rotation;

    void FixedUpdate()
    {
        position = player.transform.position;
        rotation = player.transform.rotation;
    }

    public void Save()
    {
        SaveLoadManager.SavePlayer(this);
    }

    public void Load()
    {
        float[] loadedStats = SaveLoadManager.LoadPlayer();

        Vector3 loadedPos = new Vector3(loadedStats[0], loadedStats[1], loadedStats[2]);
        Vector3 loadedRot = new Vector3(loadedStats[3], loadedStats[4], loadedStats[5]);

        player.transform.position = loadedPos;
        player.transform.rotation = Quaternion.Euler(loadedRot);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public static class SaveLoadManager
{
    public static void SavePlayer(Player player)
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream stream = new FileStream(Application.persistentDataPath + "/player.save", FileMode.Create);

        PlayerData data = new PlayerData(player);

        bf.Serialize(stream, data);
        stream.Close();
    }

    public static float[] LoadPlayer()
    {
        if (File.Exists(Application.persistentDataPath + "/player.save"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream stream = new FileStream(Application.persistentDataPath + "/player.save", FileMode.Open);

            PlayerData data = bf.Deserialize(stream) as PlayerData;

            stream.Close();
            return data.stats;
        }
        else
        {
            Debug.LogError("File does not exist.");
            return new float[6];
        }
    }
}

[Serializable]
public class PlayerData
{
    public float[] stats;

    public PlayerData(Player player)
    {
        stats = new float[6];

        stats[0] = player.position.x;
        stats[1] = player.position.y;
        stats[2] = player.position.z;

        stats[3] = player.rotation.x;
        stats[4] = player.rotation.y;
        stats[5] = player.rotation.z;
    }
}
SaveLoadManager.cs

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

public class Player : MonoBehaviour
{
    public GameObject player;
    public Vector3 position;
    public Quaternion rotation;

    void FixedUpdate()
    {
        position = player.transform.position;
        rotation = player.transform.rotation;
    }

    public void Save()
    {
        SaveLoadManager.SavePlayer(this);
    }

    public void Load()
    {
        float[] loadedStats = SaveLoadManager.LoadPlayer();

        Vector3 loadedPos = new Vector3(loadedStats[0], loadedStats[1], loadedStats[2]);
        Vector3 loadedRot = new Vector3(loadedStats[3], loadedStats[4], loadedStats[5]);

        player.transform.position = loadedPos;
        player.transform.rotation = Quaternion.Euler(loadedRot);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public static class SaveLoadManager
{
    public static void SavePlayer(Player player)
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream stream = new FileStream(Application.persistentDataPath + "/player.save", FileMode.Create);

        PlayerData data = new PlayerData(player);

        bf.Serialize(stream, data);
        stream.Close();
    }

    public static float[] LoadPlayer()
    {
        if (File.Exists(Application.persistentDataPath + "/player.save"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream stream = new FileStream(Application.persistentDataPath + "/player.save", FileMode.Open);

            PlayerData data = bf.Deserialize(stream) as PlayerData;

            stream.Close();
            return data.stats;
        }
        else
        {
            Debug.LogError("File does not exist.");
            return new float[6];
        }
    }
}

[Serializable]
public class PlayerData
{
    public float[] stats;

    public PlayerData(Player player)
    {
        stats = new float[6];

        stats[0] = player.position.x;
        stats[1] = player.position.y;
        stats[2] = player.position.z;

        stats[3] = player.rotation.x;
        stats[4] = player.rotation.y;
        stats[5] = player.rotation.z;
    }
}
我对保存加载系统和二进制格式很陌生,所以我希望你能帮我一把

提前谢谢


如果其他人遇到类似问题:除了在下面的答案中添加代码外,我还更改了player.transform.rotation.(x/y/z);到player.transform.eulerAngles(x/y/z);在SaveLoadManager.cs中进行旋转。

我不确定是否遗漏了什么,但我看不到Load()方法中有任何地方实际将玩家的位置设置为加载的值。相反,它看起来像是将加载的值传递到不必要的变量中,然后在FixedUpdate()中不断覆盖这些变量

也许可以尝试以下方式:

   public void Load()
    {
        float[] loadedStats = SaveLoadManager.LoadPlayer();
        
        Vector3 loadedPos = new Vector3(loadedStats[0], loadedStats[1], loadedStats[2]);
        Vector3 loadedRot = new Vector3(loadedStats[3], loadedStats[4], loadedStats[5]);
        player.transform.position = loadedPos ;
        player.transform.rotation = Quaternion.Euler(loadedRot);

    }

您只需去掉玩家脚本中的位置和旋转变量。

您的代码完全适合玩家的位置。但是,玩家的旋转被设置为0,0,0,每次我加载。嗯…我没有立即看到任何会导致这种情况的东西。您确定正确保存了旋转吗?如果你在保存时打印出这些值,你能找到你想要的值吗?我真的想知道你的玩家脚本所连接的游戏对象是否在旋转?还有其他一些方法可以在不直接更改player.transform.rotation值的情况下进行旋转。是的,当我删除FixeUpdate()并手动设置旋转值时,效果很好。您不需要在FixeUpdate()中执行任何操作,因为这只会覆盖不必要的值。您是否可以编辑您的帖子以显示您用于播放器脚本的代码的当前版本?我认为你的储蓄很管用。