简体   繁体   English

在场景Unity 3D之外禁用脚本

[英]Disable script outside of the scene Unity 3D

In my chess game, I have a scene of pieces choice - black or white. 在我的国际象棋游戏中,我有一个选择的场景-黑色或白色。 After the user clicks on one of the pawns he/she gets a popup message/ It looks like this: 用户单击其中一个棋子后,他/她会收到一条弹出消息/如下所示:

在此处输入图片说明

On Ok button click, the scene changes to the one with the board: 单击确定按钮后,场景将变为带有木板的场景:

在此处输入图片说明

When the user chose black pieces he/she should see them closer to him/her, while if the user chose white pieces, they should be at the front. 当用户选择黑色块时,他/她应该看到它们离他/她更近,而如果用户选择白色块,则它们应该在前面。 By default, in my scene pieces which are closer are black. 默认情况下,在我的场景中,更接近的片段是黑色的。 I tried to achieve this by adding a texture change script on each figure (they will differ for white and black pieces): 我试图通过在每个图形上添加一个纹理更改脚本来实现此目的(白色和黑色的图形会有所不同):

void Start () {
        GetComponent<Renderer>().material = Resources.Load<Material>)"Materials/Pieces/Marble/White Pawn");
    }

However, how can I disable this script when I redirect to the scene if the user chose black pieces and the default view is needed. 但是,如果用户选择黑色片断并且需要默认视图,那么在重定向到场景时如何禁用此脚本。 Here is my code for popup window: 这是我的弹出窗口代码:

void OnGUI()
    {
        if (showPopUp)
        {
            GUI.Window(0, new Rect((Screen.width / 2) - 200, (Screen.height / 2) - 115
                   , 420, 180), ShowGUI, "Figures choice");

        }
    }

    void ShowGUI(int windowID)
    {
        RedirectToMenu redirect = new RedirectToMenu();
        guiStyle.fontSize = 22;
        guiStyle.normal.textColor = Color.white;
        GUI.Label(new Rect(80, 40, 200, 30), "You have chosen black pieces", guiStyle);

        if (GUI.Button(new Rect(180, 110, 75, 35), "OK")){
            showPopUp = false;
            redirect.LoadAsset("UpgradedRoom");
            SceneManager.LoadScene("UpgradedRoom");
        }
    }

I suppose I should access this script before loading the scene and disable if needed. 我想我应该在加载场景之前访问此脚本,并在需要时禁用它。 But how can I access it outside of the scene with table, chessboard, and pieces? 但是,如何通过桌子,棋盘和棋子在场景之外访问它呢? Or can I change the textures of game objects on another scene? 还是可以在另一个场景上更改游戏对象的纹理?

What I would do is use a static variable to remember whether the pieces are black or white. 我要做的是使用一个static变量来记住这些块是黑色还是白色。 So, you'd set the variable before the scene loads and then check it after it is loaded. 因此,您需要在场景加载前设置变量,然后在加载后进行检查。 Therefore, if your class is called chess manager, your code for setting the variable might look like this: 因此,如果您的班级称为国际象棋管理器,则用于设置变量的代码可能如下所示:

public class ChessManager : Monobehavior {
    public enum ChessColor { Black, White }
    public static ChessColor playerColor

    public void OnGUI() {
        if(user chooses black) {
            playerColor = ChessColor.Black;
            //Load scene
        }
        else if(user chooses white) {
            playerColor = ChessColor.White;
            //Load scene
        }
    }
}

...And your code for enabling/disabling the script might be something like this, where ColorChanger is the script that sets the color: ...并且您用于启用/禁用脚本的代码可能类似于以下内容,其中ColorChanger是用于设置颜色的脚本:

public class ColorChanger : Monobehavior {
    public void Start() {
        if(ChessManager.playerColor == ChessManager.ChessColor.White) {
            //Set texture
        }
    }
}

This would set the texture to something else when the user chooses white once the new scene is loaded. 一旦加载新场景,当用户选择白色时,这会将纹理设置为其他内容。 Don't forget to replace the if statements in ChessManager with the code that executes when a chess piece is selected (I'm assuming you're using legacy buttons for that, so you should replace if(user chooses color) with if(GUI.Button) ). 不要忘了用选择棋子时执行的代码替换ChessManager的if语句(我假设您使用的是旧按钮,因此应将if(user chooses color)替换为if(GUI.Button) )。 Hope I could help! 希望我能帮上忙!

Also, as previously noted by someone else in the comments, it would probably be best if you used UnityEngine.UI instead of Unity's outdated legacy GUI system, but depending upon the project it might not be worth the effort. 另外,正如之前其他人在评论中指出的那样,最好是使用UnityEngine.UI而不是Unity的过时的旧版GUI系统,但是根据项目的不同,这样做可能不值得。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM