在团结帆布按钮的问题 [英] Problems with canvas buttons in unity

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

问题描述

所以,我认为包括我的游戏按键:主菜单按钮,重启按钮和播放按钮。一切工作正常,但偶尔按钮的文本不加载之一,其中一个不能正常工作。 。我不知道如何解决这个问题,因为它随机不发生每次



有关按钮的代码:

 游戏对象[] pauseObjects; 
无效的start()
{
Time.timeScale = 1;
pauseObjects = GameObject.FindGameObjectsWithTag(ShowOnPause);
hidePaused();
}


公共无效showPaused()
{
的foreach(游戏对象在pauseObjects G)
{
g.SetActive (真正);
}
}


公共无效hidePaused()
{
的foreach(在pauseObjects游戏物体G)
{
g.SetActive(假);
}
}

公共无效LoadLevel(串级)
{
Application.LoadLevel(水平);
}

公共无效pauseControl()
{
如果(Time.timeScale == 1)
{
Time.timeScale = 0 ;
showPaused();
}
,否则如果(Time.timeScale == 0)
{
Time.timeScale = 1;
hidePaused();
}
}


解决方案

的这样你的用户界面是设置是不正确的。如果通过暂停期间的一切必须循环,你做错了。我之前已经回答过类似的问题,将刚才复制并粘贴在这里这一点。



使用不同的画布为单独的你的用户界面并根据组显示当他们。然后你就可以在切换整个画布 / 关闭即可。你的[文章] [1]在此之前职位表明你正在使用图像按钮,你有一个不同的脚本连接到你正在使用的按钮两个图像。使用按钮作为按钮图片对于不可点击的对象。就那么简单。因此,这些图像改为按钮。



您不需要GAMEOVER现场。一个GameOverCanvas是罚款。
粗体GameObjects是在对象(画布)。根据该那些以' - 儿童 GameObjects。



第1步:



创建画布并将其命名为 MainMenuCanvas (第一个UI显示当游戏加载).Create每个孩子按钮,如下它们重命名( GameObject->用户界面 - >按钮 ):

  -playButton; 
-settingsButton;
-exitGameButton;



附上的 MainMenuCanvas脚本,以在 MainMenuCanvas 对象



第二步:



创建一个画布并将其命名为的GameCanvas (比赛过程中显示).Create每个子键,将其重命名如下( GameObject->用户界面 - >按钮):

  -pauseButton 
- jumpButton

附上的的GameCanvas脚本,以在的GameCanvas 对象



第三步:



创建一个画布并将其命名为 PauseCanvas (点击暂停按钮时显示).Create每个孩子下面按钮并重新命名( GameObject->用户界面 - >按钮):

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



附上的 PauseCanvas脚本,以在 PauseCanvas 对象



第四步:



创建一个画布并将其命名为 SettingsCanvas (点击设置按钮时显示).Create每个子按钮和下面将其重命名( GameObject->用户界面 - >按钮):

  -backButton; 



附上的 SettingsCanvas脚本,以在 SettingsCanvas 对象



第五步:



创建一个画布并将其命名为 GameOverCanvas (当游戏结束或显示的玩家被杀死).Create下面每个子按钮并重新命名( GameObject->用户界面 - >按钮):

   - playAgainButton; 
-backToMainMenuButton;
-exitGameButton;



附上的 GameOverCanvas脚本,以在 GameOverCanvas 对象



第六步:



游戏现场,确保只有的GameCanvas 已启用。画布的其余部分应该是手动的禁用



第七步:



菜单现场,确保只有 MainMenuCanvas 已启用。画布的其余部分应该是手动的停用即可。



一旦你正确地得到这个设置,我提供的UI代码模板应该工作。没有更多的UI的重叠或文字的消失即可。您可以轻松地添加或删除功能。



您的UI设置看起来应该像下面的图片。



<一个HREF =htt​​p://i.stack.imgur.com/13mHW.png相对=nofollow>


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天全站免登陆