• 主页
  • 我应该如何重置所有的对象,事实上,整个世界在重新开始一个新的游戏时,它的原始状态?

我应该如何重置所有的对象,事实上,整个世界在重新开始一个新的游戏时,它的原始状态?

问题是,当我保存一次游戏,然后在没有先退出游戏的情况下开始新游戏时,它就是开始新游戏,但例如,相机和玩家位置与第一次开始新游戏时不同。也许相机是一样的,但是玩家的位置不一样。

这看起来像是在保存游戏一次后不退出游戏就创建一个新游戏,而不是重置所有内容。

我在层级主菜单和游戏中有两个场景。在游戏下,我拥有所有的游戏对象。

在主菜单中,我有这个脚本和两个方法,我从一个按钮onclick事件调用,一个用于新游戏,另一个用于加载:

public void ClickNewGameDialog(string ButtonType)
        {
            if (ButtonType == "Yes")
            {
                loading = false;
                newGameDialog.SetActive(false);
                StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, _newGameButtonLevel));
            }

            if (ButtonType == "No")
            {
                GoBackToMainMenu();
            }
        }

        public void ClickLoadGameDialog(string ButtonType)
        {
            if (ButtonType == "Yes")
            {
                loading = true;
                newGameDialog.SetActive(false);
                StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, _newGameButtonLevel));               
                
            }

            if (ButtonType == "No")
            {
                GoBackToMainMenu();
            }
        }

和FadeAndLoadScene方法:

public IEnumerator FadeAndLoadScene(FadeDirection fadeDirection, string sceneToLoad)
    {
        yield return Fade(fadeDirection);
        SceneManager.LoadScene(sceneToLoad);
    }

在一个新的游戏中,加载" Game“时字符串是相同的,因为我有一个游戏场景。

这是我创建的一个类,只是为了能够方便地调用我的Save和Load方法:

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

public class SaveLoad : MonoBehaviour
{
    private static SaveGame saveGame;

    private void Awake()
    {
        // Create your temporary save game
        saveGame = new SaveGame();
        SaveMaster.SetSlot(0, true, saveGame);
    }

    public static void Save()
    {
        // Is it using a temporary slot?
        if (SaveMaster.GetActiveSlot() == 0)
        {
            if (SaveFileUtility.IsSlotUsed(1))
                SaveMaster.DeleteSave(1); // Removes the save game that was available.
            SaveMaster.SetSlot(1, false, saveGame); // Sets the slot to the first one, while keeping your save game.
        }

        SaveMaster.SyncSave();
        SaveMaster.WriteActiveSaveToDisk();
    }

    public static void Load()
    {
        SaveMaster.SetSlot(1, true);
    }
}

这是开始新游戏时的一小段场景剪辑,我在这里进行保存和加载:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using SpeedTutorMainMenuSystem;

public class PlayingInGameScenesController : MonoBehaviour
{
    public CinemachineFreeLook[] freeLookCams;
    public CinemachineVirtualCamera[] virtualCams;
    public LockController lockController;
    public GameObject uiSceneText;
    public GameObject player;
    public float transitionSpeed = 5f;
    public float thresHold = 0.1f;
    public bool talkingProcess = true;
    public FadeInOutSaveGameText fadeInOutSaveGame;

    private bool newGame = true;

    private void Start()
    {
        
    }

    public void PlayingSceneInGame()
    {
        PlayingSceneStatesControls(true);
        StartCoroutine(ScenePlayingTime());
    }

    private void Update()
    {
        if (SceneManager.GetActiveScene().name != "Main Menu" && newGame == true && MenuController.loading == false)
        {
            PlayingSceneInGame();
            newGame = false;
        }

        if (SceneManager.GetActiveScene().name != "Main Menu" && MenuController.loading == true)
        {
            SaveLoad.Load();
        }
    }

    private void LateUpdate()
    {
       
    }

    private void PlayingSceneStatesControls(bool LockState)
    {
        lockController.LockControl(LockState);

        if (LockState == true)
        {
            uiSceneText.SetActive(true);
        }
        else
        {
            talkingProcess = false;
            uiSceneText.SetActive(false);
        }
    }

    IEnumerator ScenePlayingTime()
    {
        yield return new WaitForSeconds(10);

        PlayingSceneStatesControls(false);

        freeLookCams[0].enabled = false;
        freeLookCams[1].enabled = true;

        var brain = Camera.main.GetComponent<CinemachineBrain>().m_DefaultBlend.m_Time;

        StartCoroutine(SaveAfterSomeTime());
        
    }

    IEnumerator SaveAfterSomeTime()
    {
        yield return new WaitForSeconds(15f);

        SaveLoad.Save();

        StartCoroutine(fadeInOutSaveGame.OverAllTime(5f));
    }
}

游戏开始,25秒后自动保存游戏。然后,当我加载游戏时,它加载场景,然后加载保存的游戏,并将玩家放置在保存它的位置。

问题是,当我制作加载游戏,然后逃脱到主菜单,然后制作新游戏,然后新游戏从保存的游戏位置开始,而不是从原始位置开始。当开始一款新游戏时,我不会进行任何加载:

这是一个新的游戏:

if (SceneManager.GetActiveScene().name != "Main Menu" && newGame == true && MenuController.loading == false)
        {
            PlayingSceneInGame();
            newGame = false;
        }

这是用于加载的:

if (SceneManager.GetActiveScene().name != "Main Menu" && MenuController.loading == true)
        {
            SaveLoad.Load();
        }

如果我要保存并加载游戏,然后退出游戏,再运行一次,将首先创建一个新游戏,它将作为新的罚款开始游戏。问题是,当在游戏中进行保存,然后加载新游戏时,新游戏永远不会将玩家位置重新设置为原始位置。

我应该做一些全局的世界状态保存,并在每次创建新游戏时加载状态吗?

现在我只保存播放器状态:

?

?

我想当你开始一个新的游戏时,加载游戏场景而不加载保存的游戏,它应该从零开始游戏,一切都应该休息到原始的。但它不会重置球员的位置和旋转。

如果我先退出游戏,但不是在游戏仍在运行时退出,就会发生这种情况。

我试过了,但它还不起作用:

public CinemachineFreeLook[] freeLookCams;
    public CinemachineVirtualCamera[] virtualCams;
    public LockController lockController;
    public GameObject uiSceneText;
    public GameObject playerStart;
    public float transitionSpeed = 5f;
    public float thresHold = 0.1f;
    public bool talkingProcess = true;
    public FadeInOutSaveGameText fadeInOutSaveGame;

    private bool newGame = true;
    private Vector3 playerOriginalPosition;
    private Vector3 playerOriginalScale;
    private Quaternion playerOriginalRotation;

    private void Start()
    {
        playerOriginalPosition = playerStart.transform.position;
        playerOriginalScale = playerStart.transform.localScale;
        playerOriginalRotation = playerStart.transform.rotation;
    }

    public void PlayingSceneInGame()
    {
        PlayingSceneStatesControls(true);
        StartCoroutine(ScenePlayingTime());
    }

    private void Update()
    {
        if (SceneManager.GetActiveScene().name != "Main Menu" && newGame == true && MenuController.loading == false)
        {
            playerStart.transform.position = playerOriginalPosition;
            playerStart.transform.localScale = playerOriginalScale;
            playerStart.transform.rotation = playerOriginalRotation;

            PlayingSceneInGame();
            newGame = false;
        }

转载请注明出处:http://www.jxbyjx.net/article/20230510/1513568.html