简体   繁体   English

画布按钮统一问题

[英]Problems with canvas buttons in unity

So, i have buttons for my game that include : Main Menu button , Restart button and Play button. 因此,我的游戏按钮包括:主菜单按钮,重新启动按钮和播放按钮。 Everything works fine, but occasionally one of the button's text doesn't load , one of them doesn't work. 一切正常,但有时按钮的文本之一不加载,其中之一不起作用。 I'm not sure how to fix this, because it happens randomly and not every time. 我不确定如何解决此问题,因为它是随机发生的,而不是每次都发生。

The code for the buttons : 按钮的代码:

GameObject[] pauseObjects;
void Start()
{
    Time.timeScale = 1;
    pauseObjects = GameObject.FindGameObjectsWithTag("ShowOnPause");
    hidePaused();
}


public void showPaused()
{
    foreach (GameObject g in pauseObjects)
    {
        g.SetActive(true);
    }
}


public void hidePaused()
{
    foreach (GameObject g in pauseObjects)
    {
        g.SetActive(false);
    }
}

public void LoadLevel(string level)
{
    Application.LoadLevel(level);
}

public void pauseControl()
{
    if (Time.timeScale == 1)
    {
        Time.timeScale = 0;
        showPaused();
    }
    else if (Time.timeScale == 0)
    {
        Time.timeScale = 1;
        hidePaused();
    }
}

The way your UI is setup is not right. UI的设置方式不正确。 If you have to loop through everything during pause, you are doing it wrong. 如果必须在暂停过程中遍历所有内容,那么您做错了。 I have answered a similar question before and will just copy and paste this here. 我之前已经回答过类似的问题,将其复制并粘贴到此处。

Use different Canvas to separate your UI and group them depending on when they are displayed. 使用不同的画布来分隔您的UI并根据它们的显示时间对其进行分组。 Then you can toggle the whole Canvas on / off . 然后,您可以打开 / 关闭整个“画布 Your [post][1] before this post shows that you are using image as button and you have a different script attached to two images you are using as buttons. 此帖子之前的[post] [1]显示您正在使用图像作为按钮,并且在要用作按钮的两个图像上附加了一个不同的脚本。 Use Buttons for buttons and image for non-clickable objects. 按钮用于按钮 ,将图像用于不可单击的对象。 Simple as that. 就那么简单。 So change those images to buttons. 因此,将这些图像更改为按钮。

You don't need the GameOver scene. 您不需要GameOver场景。 A GameOverCanvas is fine. 一个GameOverCanvas很好。 The GameObjects in bold are the parent Objects(Canvas). 粗体显示的GameObjects是父级 Objects(Canvas)。 The ones under that starts with ' - ' are the child GameObjects. 以' - '开头的是 GameObjects。

Step 1: 第1步:

Create Canvas and name it MainMenuCanvas (First UI to display when game Loads).Create each child button and rename them as below( GameObject->UI->Button ): 创建Canvas并将其命名为MainMenuCanvas (游戏加载时显示的第一个UI)。创建每个子按钮并将其重命名如下( GameObject-> UI-> Button ):

-playButton;
-settingsButton;
-exitGameButton;

Attach the MainMenuCanvas script to the MainMenuCanvas Object. MainMenuCanvas脚本附加到MainMenuCanvas对象。

Step 2: 第2步:

Create a Canvas and name it GameCanvas (Displayed during game).Create each child button and rename them as below( GameObject->UI->Button ): 创建一个Canvas并将其命名为GameCanvas (在游戏期间显示)。创建每个子按钮并将其重命名为以下名称( GameObject-> UI-> Button ):

-pauseButton
-jumpButton

Attach the GameCanvas script to the GameCanvas Object. GameCanvas脚本附加到GameCanvas对象。

Step 3: 第三步:

Create a Canvas and name it PauseCanvas (Displayed when pause button is clicked).Create each child button and rename them as below( GameObject->UI->Button ): 创建一个Canvas并将其命名为PauseCanvas (单击暂停按钮时显示)。创建每个子按钮并将其重命名如下( GameObject-> UI-> Button ):

-resumeButton;
-backToMainMenuButton;
-settingsButton;
-exitGameButton;

Attach the PauseCanvas script to the PauseCanvas Object. PauseCanvas脚本附加到PauseCanvas对象。

Step 4: 第四步:

Create a Canvas and name it SettingsCanvas (Displayed when settings button is clicked).Create each child button and rename them as below( GameObject->UI->Button ): 创建一个Canvas并将其命名为SettingsCanvas (单击设置按钮时显示)。创建每个子按钮并将其重命名如下( GameObject-> UI-> Button ):

-backButton;

Attach the SettingsCanvas script to the SettingsCanvas Object. SettingsCanvas脚本附加到SettingsCanvas对象。

Step 5: 步骤5:

Create a Canvas and name it GameOverCanvas (Displayed when Game is Over or player is killed).Create each child button and rename them as below( GameObject->UI->Button ): 创建一个Canvas并将其命名为GameOverCanvas (在游戏结束或玩家被杀死时显示)。创建每个子按钮并将其重命名如下( GameObject-> UI-> Button ):

-playAgainButton;
-backToMainMenuButton;
-exitGameButton;

Attach the GameOverCanvas script to the GameOverCanvas Object. GameOverCanvas脚本附加到GameOverCanvas对象。

Step 6: 步骤6:

In the Game scene, make sure that only GameCanvas is enabled. 游戏场景中,确保仅启用GameCanvas The rest of the canvas should be manually disabled . 画布的其余部分应手动禁用

Step 7: 步骤7:

In the Menu scene, make sure that only MainMenuCanvas is enabled. 在“ 菜单”场景中,确保仅启用MainMenuCanvas The rest of the canvas should be manually disabled . 画布的其余部分应手动禁用

Once you get this setup correctly, the UI code templates I provided should work. 正确设置后,我提供的UI代码模板应该可以使用。 No more UI overlapping or text disappearing . 不再有UI 重叠或文本消失 You can easily add or remove features. 您可以轻松添加或删除功能。

Your UI setup should look like the image below. 您的UI设置应如下图所示。

在此处输入图片说明

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

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