如何在两个场景中使用Player游戏对象?

时间:2020-08-30 23:21:57

标签: c# unity3d

我有一个主菜单场景和游戏场景。 “加载”按钮在主菜单中,带有加载/保存功能的脚本在游戏场景中的“播放器”上。

我什至不能将播放器拖到主菜单中的onclick按钮事件上,因为它不在同一场景中。

Editor

在游戏场景中的播放器上,我附加了以下脚本:

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

public class Player : MonoBehaviour
{
    public void SavePlayer()
    {
        SaveSystem.SavePlayer(this);
    }

    public void LoadPlayer()
    {
        PlayerData data = SaveSystem.LoadPlayer();

        Vector3 position;
        position.x = data.position[0];
        position.y = data.position[1];
        position.z = data.position[2];
        transform.position = position;

        Quaternion rotation;
        rotation.x = data.rotation[0];
        rotation.y = data.rotation[1];
        rotation.z = data.rotation[2];
        rotation.w = data.rotation[3];
        transform.rotation = rotation;
    }
}

在主菜单加载按钮中,我有一个onclick事件用于按钮加载对话框:

public void MouseClick(string buttonType)
        {
            if (buttonType == "Controls")
            {
                controlsMenu.SetActive(true);
                menuNumber = 6;
            }

            if (buttonType == "Graphics")
            {
                GeneralSettingsCanvas.SetActive(false);
                graphicsMenu.SetActive(true);
                menuNumber = 3;
            }

            if (buttonType == "Sound")
            {
                GeneralSettingsCanvas.SetActive(false);
                soundMenu.SetActive(true);
                menuNumber = 4;
            }

            if (buttonType == "Exit")
            {
                Debug.Log("YES QUIT!");
                Application.Quit();
            }

            if (buttonType == "Options")
            {
                menuDefaultCanvas.SetActive(false);
                GeneralSettingsCanvas.SetActive(true);
                menuNumber = 2;
            }

            if (buttonType == "LoadGame")
            {
                menuDefaultCanvas.SetActive(false);
                loadGameDialog.SetActive(true);
                menuNumber = 8;
            }

            if (buttonType == "NewGame")
            {
                menuDefaultCanvas.SetActive(false);
                newGameDialog.SetActive(true);
                menuNumber = 7;
            }
        }

这是来自主菜单中控制按钮的脚本。 如果要加载是/否返回选项,我需要此事件进行对话。

因此,我想在同一加载按钮上为LoadPlayer函数添加另一个onclick事件。 但是LoadPlayer脚本已附加到游戏场景中的Player上,因此我无权使用LoadPlayer函数。

我可以将Player对象设置为DontDestroyOnLoad,但仍然如何获得对Load按钮onclick事件的引用?

好吧,在单击“加载游戏”按钮时在主菜单中加载“游戏”场景:

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

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

但是我还需要调用LoadPlayer函数以将更改应用于游戏场景。然后,也许我不需要另一个onclick事件,但是如何获取对Player对象的引用?

1 个答案:

答案 0 :(得分:0)

下一步是简单的方法。 您的SaveSystem.LoadPlayer是静态函数,您需要在SaveSystem中将事件静态化 使用系统; 公共静态事件Action LoadPlayerDataEvent; 在播放器脚本中,将加载播放器更改为

void OnEnable(){
     SaveSystem.LoadPlayerDataEvent += LoadPlayer;
}
void OnDisable(){
     SaveSystem.LoadPlayerDataEvent -= LoadPlayer;
}

然后在您的ClickLoadGameDialog中,使用此SaveSystem.LoadPlayerDataEvent?.Invoke();启动事件。

相关问题