简体   繁体   English

不允许从 Monobehaviour 调用 persistentDatapath

[英]persistentDatapath is not allowed called from Monobehaviour

错误。

I also try putting it on start and awake like what the error said but all i got is more error instead.我也尝试像错误所说的那样将其启动并唤醒,但我得到的只是更多的错误。

public class SaveManager : MonoBehaviour
{
      private string SavePath = $"{Application.persistentDataPath}/save.text";

    public void Save()
    {
        var state = LoadFile();
        CaptureState(state);
        SaveFile(state);
    }

    public void Load()
    {
        var state = LoadFile();
        RestoreState(state);
    }

    private Dictionary<string, object> LoadFile()
    {
        if(!File.Exists(SavePath))
        {
            return new Dictionary<string, object>();
        }
        using(FileStream stream = File.Open(SavePath, FileMode.Open))
        {
            var formatter = new BinaryFormatter();
            return(Dictionary<string, object>)formatter.Deserialize(stream);
        }
    }


    private void SaveFile(object state)
    {
        using(var stream = File.Open(SavePath, FileMode.Create))
        {
            var formatter = new BinaryFormatter();
            formatter.Serialize(stream, state);
        }
    }

    private void CaptureState(Dictionary<string, object> state)
    {
        foreach(var saveable in FindObjectsOfType<Entity>())
        {
            state[saveable.Id] = saveable.CaptureState();
        }
    }

    private void RestoreState(Dictionary<string, object> state)
    {
        foreach(var saveable in FindObjectsOfType<Entity>())
        {
            if(state.TryGetValue(saveable.Id, out object value))
            {
                saveable.RestoreState(value);
            }
        }
    }

   
}

Well exactly as the error suggests rather do好吧,正如错误所暗示的那样

private string SavePath;

private void Awake ()
{
    SavePath = $"{Application.persistentDataPath}/save.text";
}

In general rather use Path.Combine which uses the correct path separator depending on the platform一般来说,宁可使用Path.Combine ,它根据平台使用正确的路径分隔符

SavePath = Path.Combine(Application.persistentDataPath, "save.text");

Or you could also make it a property but this returns a new string everytime或者您也可以将其设为属性,但这每次都会返回一个新字符串

private string SavePath => Path.Combine(Application.persistentDataPath, "save.text");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnityException:不允许从 MonoBehaviour 构造函数调用 GetActiveScene - UnityException: GetActiveScene is not allowed to be called from a MonoBehaviour constructor “不允许从 MonoBehaviour 构造函数调用加载”在不从 MonoBehaviour 继承的脚本上 - "Load is not allowed to be called from MonoBehaviour constructor" On script that does not inherit from MonoBehaviour C# - 不允许从 MonoBehaviour 构造函数调用 RandomRangeInt - C# - RandomRangeInt is not allowed to be called from a MonoBehaviour constructor UnityException:不允许从 MonoBehaviour 构造函数调用 get_time - UnityException: get_time is not allowed to be called from MonoBehaviour constructor unity 从persistentDataPath 添加mp3 文件 - Unity Add mp3 file from persistentDataPath 实例化从MonoBehaviour派生的类 - Instantiate a class that derives from MonoBehaviour 我可以从非单一行为脚本中访问单一行为脚本吗? - Can i access a monobehaviour script from within a non monobehaviour script? 安装后将文件从Resources / StreamingAssets复制到Application.persistentDataPath - Copy files from Resources/StreamingAssets to Application.persistentDataPath upon installation 来自另一个非MonoBehaviour类的访问变量 - Access variable from another non MonoBehaviour class 如何从新的MonoBehaviour隐藏脚本字段 - How hide script field from new MonoBehaviour
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM