统一画布按钮的问题 [英] Problems with canvas buttons in unity

查看:23
本文介绍了统一画布按钮的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的游戏按钮包括:主菜单按钮、重新启动按钮和播放按钮.一切正常,但有时按钮的文本之一无法加载,其中之一不起作用.我不知道如何解决这个问题,因为它是随机发生的,而不是每次都发生.

按钮代码:

GameObject[] pauseObjects;无效开始(){时间.timeScale = 1;pauseObjects = GameObject.FindGameObjectsWithTag("ShowOnPause");隐藏暂停();}public void showPaused(){foreach(暂停对象中的游戏对象 g){g.SetActive(true);}}公共无效隐藏暂停(){foreach(暂停对象中的游戏对象 g){g.SetActive(false);}}public void LoadLevel(字符串级别){Application.LoadLevel(level);}公共无效暂停控制(){如果(时间.timeScale == 1){时间.timeScale = 0;显示暂停();}否则如果(时间.timeScale == 0){时间.timeScale = 1;隐藏暂停();}}

解决方案

您的 UI 设置方式不正确.如果您必须在暂停期间遍历所有内容,那么您就做错了.我之前回答过一个类似的问题,这里只是复制粘贴.

使用不同的 Canvas 来分离您的 UI,并根据它们的显示时间将它们分组.然后您可以切换整个 Canvas 打开/关闭.您在此帖子之前的 [post][1] 显示您将图像用作按钮,并且您将不同的脚本附加到用作按钮的两个图像上.按钮用于按钮图像用于不可点击对象.就那么简单.因此,将这些图像更改为按钮.

您不需要 GameOver 场景.GameOverCanvas 很好.粗体的游戏对象是 对象(画布).下面以-"开头的是游戏对象.

第 1 步:

创建Canvas并将其命名为MainMenuCanvas(游戏加载时显示的第一个UI).创建每个子按钮并将它们重命名如下(GameObject->UI->按钮):

-playButton;-设置按钮;-exitGameButton;

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. 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. 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. 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. A GameOverCanvas is fine. The GameObjects in bold are the parent Objects(Canvas). The ones under that starts with '-' are the child GameObjects.

Step 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):

-playButton;
-settingsButton;
-exitGameButton;

Attach the MainMenuCanvas script to the MainMenuCanvas Object.

Step 2:

Create a Canvas and name it GameCanvas (Displayed during game).Create each child button and rename them as below(GameObject->UI->Button):

-pauseButton
-jumpButton

Attach the GameCanvas script to the GameCanvas Object.

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):

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

Attach the PauseCanvas script to the PauseCanvas Object.

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):

-backButton;

Attach the SettingsCanvas script to the SettingsCanvas Object.

Step 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):

-playAgainButton;
-backToMainMenuButton;
-exitGameButton;

Attach the GameOverCanvas script to the GameOverCanvas Object.

Step 6:

In the Game scene, make sure that only GameCanvas is enabled. The rest of the canvas should be manually disabled.

Step 7:

In the Menu scene, make sure that only MainMenuCanvas is enabled. The rest of the canvas should be manually disabled.

Once you get this setup correctly, the UI code templates I provided should work. No more UI overlapping or text disappearing. You can easily add or remove features.

Your UI setup should look like the image below.

这篇关于统一画布按钮的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆