简体   繁体   English

尝试从保存文件加载游戏数据时出现 FileNotFoundException

[英]FileNotFoundException when trying to Load Game Data from save file

I am unsure why I am getting this error;我不确定为什么会收到此错误;

FileNotFoundException: Could not find file "........\\New Unity Project\\Assets\\Saves\\NewGame" FileNotFoundException:找不到文件“........\\New Unity Project\\Assets\\Saves\\NewGame”

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

[System.Serializable]

public class PlayerStatistics  : MonoBehaviour
{

private string _loadGameName;
//all save variables, only leaving one example
private int _decision1;

    public void ChangeGameLoadName(string LoadGameName)
    {
            _loadGameName = LoadGameName;
            Debug.Log("Load Game Name Received by Player Stats as : " + LoadGameName) ;
    }
    public void PlayerLoadGameData()
    {
            string LoadName = _loadGameName;
            Debug.Log("Attempting to load game with the filename: " + LoadName);
            BinaryFormatter loadGameBF = new BinaryFormatter();
            string ApplicationFolder = Application.dataPath;
            string saveGameDirectory = ApplicationFolder + "/Saves/";
            FileStream loadFile = File.Open(saveGameDirectory + LoadName, FileMode.Open);
            Save load = (Save)loadGameBF.Deserialize(loadFile);
            loadFile.Close();

            _decision1 = load._decision1;
            //other save vars here

            Debug.Log("GameInfoLoaded in player stats");
            TakeLoadGameAndPlay();
    }
}

The Debug.Log in ChangeGameLoadName is printing the correct name eg Load Game Name Received by Player Stats as : Google.save ChangeGameLoadName 中的 Debug.Log 正在打印正确的名称,例如 Load Game Name Received by Player Stats as : Google.save

The "Attempting to load game with the filename:" prints as NewGame. “正在尝试使用文件名加载游戏:”打印为 NewGame。 For reference, the user presses a button which invokes ChangeGameLoadName(), then another button which invokes PlayerLoadGameData().作为参考,用户按下一个调用 ChangeGameLoadName() 的按钮,然后按下另一个调用 PlayerLoadGameData() 的按钮。 I don't know where it is getting this "NewGame" from at all.我根本不知道它是从哪里得到这个“NewGame”的。 Any help would be very appreciated.任何帮助将不胜感激。

EDIT:编辑:

ChangeGameLoadName() is called from my GameManager script through this function: ChangeGameLoadName() 是通过这个函数从我的 GameManager 脚本调用的:

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

public class GameManager : MonoBehaviour
{
    // Script References
    GameView GameViewScript;
    PlayerStatistics PlayerStatisticsScript;
    GameObject GlobalObject;
    PlayerOptions PlayerOptionsScript;
    PlayerInput PlayerInputScript;
    StoryHandler StoryHandlerScript;
    GameHUD GameHUDScript;

    [SerializeField]
    private GameObject _storyScripts;

    private float _shortFadeSpeed = 0.75f;
    private float _longFadeSpeed = 1.5f;
    private float _longLongFadeSpeed = 3f;

    private void Start()
    {
        GameViewScript = GetComponent<GameView>();
        PlayerInputScript = GetComponent<PlayerInput>();
        StoryHandlerScript = _storyScripts.GetComponent<StoryHandler>();
        GlobalObject = GameObject.FindGameObjectWithTag("GlobalScripts");
        PlayerStatisticsScript = GlobalObject.GetComponent<PlayerStatistics>();
        PlayerOptionsScript = GlobalObject.GetComponent<PlayerOptions>();
        GameHUDScript = GetComponent<GameHUD>();

    }

    public void LoadGameNameHUDtoPlayerStatistics(string LoadGameName)
    {
        PlayerStatisticsScript.ChangeGameLoadName(LoadGameName);
        Debug.Log("Game Manager Received Name and Sent it to Player Stats. Name = "+ LoadGameName);
    }

}

The above debug log prints correctly.上面的调试日志打印正确。 (as google.save if my savefilename was google.save) (如果我的保存文件名是 google.save,则作为 google.save)

Which in turn was called from:反过来又是从以下位置调用的:

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

public class SpecialPrefabLoad : MonoBehaviour
{
    private GameManager GameManagerScript;

    public void GetLoadGameNameAndFeedToLoad()
    {
        GameObject Canvas = GameObject.Find("Canvas");
        GameManagerScript = Canvas.GetComponent<GameManager>();

        string LoadGameName = this.gameObject.name;
        GameManagerScript.LoadGameNameHUDtoPlayerStatistics(LoadGameName);
        Debug.Log("Found Save Game and Sent Info to Game Manager");
    }
}

This was a special script because its attached to a prefab button which instantiates the load game buttons.这是一个特殊的脚本,因为它附加到一个预制按钮上,该按钮实例化了加载游戏按钮。 I couldn't find an alternate way to do this.我找不到替代方法来做到这一点。 This was called by the OnClick() Unity Function.这是由 OnClick() Unity 函数调用的。

The gameObject.name was set here: gameObject.name 在此处设置:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.Audio;
using System.IO;

public class GameHUD : MonoBehaviour
{
    [SerializeField]
    private AudioMixer _globalAudio;

    [SerializeField]
    private GameObject _faderBackgroundObject;
    [SerializeField]
    private Image _faderBackground;

    [SerializeField]
    private GameObject _menuMainGame;
    [SerializeField]
    private GameObject _menuButtons;
    [SerializeField]
    private GameObject _saveGameMenu;
    [SerializeField]
    private GameObject _loadGameMenu;
    [SerializeField]
    private GameObject _optionsMenu;
    [SerializeField]
    private GameObject _audioOptions;
    [SerializeField]
    private GameObject _gameplayOptions;

    [SerializeField]
    private Slider _globalVolumeSlider;
    [SerializeField]
    private Slider _bgmVolumeSlider;
    [SerializeField]
    private Slider _fxVolumeSlider;

    private float _longFadeSpeed = 1.5f;
    private float _shortFadeSpeed = 0.5f;

    [SerializeField]
    private GameObject _canvas;
    private GameManager GameManagerScript;

    public Text inputText;

    [SerializeField]
    private GameObject _gameSavedNotification;

    [SerializeField]
    private GameObject _loadGameScroller;
    [SerializeField]
    private GameObject _prefabLoadGameButton;

    private void Start()
    {
        GameManagerScript = _canvas.GetComponent<GameManager>();
        SetGameSettingsFromPlayerPrefs();
    }

    private void GetPlayerLoadGameList()
    {
        string ApplicationFolder = Application.dataPath;
        string[] saveFolder = Directory.GetFiles(@ApplicationFolder + "/Saves/", "*.save");
        int i = 0;
        foreach (string saveGameFile in saveFolder)
        {
            i = i + 1;

            GameObject myNewObj = Instantiate(_prefabLoadGameButton, _loadGameScroller.transform);
            myNewObj.transform.position = new Vector3(_loadGameScroller.transform.position.x + 250, _loadGameScroller.transform.position.y - (i * 20), 0);
            myNewObj.name = saveGameFile;
            Text saveGameName = myNewObj.GetComponent<Text>();
            saveGameName.text = Path.GetFileNameWithoutExtension(saveGameFile);
            myNewObj.name = saveGameName.text + ".save";
        }

    }
}

Bit old, but I thought I'd say.有点旧,但我想我会说。 It was an instance issue.这是一个实例问题。

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

相关问题 尝试将Autofac加载为嵌入式程序集时出现FileNotFoundException - FileNotFoundException when trying to load Autofac as an embedded assembly 尝试保存Outlook附件时出现“ System.IO.FileNotFoundException” - 'System.IO.FileNotFoundException' when trying to save an outlook Attachment 为什么 Gembox 电子表格在调用 Excelfile.save 时崩溃:FileNotFoundException 无法加载文件或程序集 System.Security.Permissions - Why does Gembox spreadsheet crash when calling Excelfile.save: FileNotFoundException Could not load file or assembly System.Security.Permissions 如何为webGL保存/加载游戏数据 - How to save/ load game data for webGL FileNotFoundException尝试将文件从一个目录复制到另一个目录时 - FileNotFoundException While trying to copy the file from one directory to another FileNotFoundException:无法加载文件或程序集 - FileNotFoundException: Could not load file or assembly 在文件中保存和加载 MemoryStream - Save and load MemoryStream to/from a file 从PicturesLibrary获取文件时出现System.IO.FileNotFoundException - System.IO.FileNotFoundException when getting file from PicturesLibrary 尝试保存变量值数据并加载它们时为什么会出现异常? - Why getting exception when trying to save variables values data and load them? 在Window Game中保存和加载游戏状态 - Save and load game state in Window Game
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM