按钮`OnSelect`在Unity.UI画布情况下不起作用 [英] Button `OnSelect` not functioning in Unity.UI Canvas situation

查看:601
本文介绍了按钮`OnSelect`在Unity.UI画布情况下不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个按钮,当您点击一个按钮时,会出现一个带有文本的面板.

I have three buttons, when you tap one button, a panel with a text appear.

问题是当我附加脚本时,什么也没发生. "click"已注册,但面板从未出现.

The problem is when I attached the script, nothing happens. The "click" is registered but the the panel never appears.

我的脚本附在每个按钮上,如下所示:

My script is attached to each of the button and is something like this:

public GameObject panel; //i use to put the panel in unity
bool selected = false;

void Start () {    
    panel.SetActive(false); 
}

void OnSelect() {    
    selected = !selected;
    panel.SetActive(true);
}

我可能需要对面板进行其他操作,但我无法弄清楚.

I probably have to do something else with the panel but I can't figure it out.

推荐答案

这样做:

(1)将画布添加到您的项目中

(1) Add the canvas to your project

(2)大提示-确保选择根据屏幕尺寸缩放.

(2) BIG TIP - be sure to select Scale with screen size.

那是您使用过的唯一一个. Unity意外地在此处设置了错误的默认值,他们尚未对其进行修复.

That's the only one you ever use. Unity accidentally set the wrong default there, they have not fixed it yet.

(3)在您的画布中,添加一个按钮,使其说出测试"

(3) In your Canvas, add a BUTTON Make it say perhaps "Test"

(3)在您的画布中,添加另一个按钮,使其说也许是另一个测试"

(3) In your Canvas, add another BUTTON Make it say perhaps "Another Test"

(4)编写类似这样的脚本...

(4) Make a script something like this...

public class MainScreen:MonoBehaviour
    {
    public void UserClickedTest()
        {
        Debug.Log("test..");
        }
    public void UserClickedAnotherTest()
        {
        Debug.Log("another test..");
        }
    }

(5)将该脚本的一个副本放在您喜欢的任何对象上.您可以将其放在相机,画布上或其他任何有意义的地方

(5) put ONE copy of that script on ANY object you like. You can put it on your camera, on the canvas, or anywhere else that makes sense

例如,现在假设您将其放在CAMERA对象上.

For now let's say you put it on your CAMERA object, for example.

(6)单击按钮测试". .....

(6) Click on the button "Test" .....

  1. 单击OnClick下的加号按钮

  1. click the PLUS button under OnClick

您会看到标有"_main"的广告位在这个例子中.将您的CAMERA物品从HEIRARCHY拖到该位置

you see the slot that says "_main" in this example. DRAG your CAMERA item from HEIRARCHY, to that slot

使用下拉菜单:

选择"UserClickedTest()";功能...

好吗?

  1. 现在,对于其他按钮,执行相同的操作,但是选择"UserClickedAnotherTest()";功能.

  1. Now for the other button, do the same but select the "UserClickedAnotherTest()" function.

您完成了!运行并测试!

You're done! Run and test!

除非使用ISelectHandler和更多的东西,否则您不能使用OnSelect系统:对于初学者来说很难.我强烈建议OP管理员掌握我在这里介绍的更简单的技术.享受吧!

You can't use the OnSelect system unless you use ISelectHandler and more stuff: it is difficult for beginners. I strongly recommend the OP masters the simpler technique I explain here. Enjoy!

这篇关于按钮`OnSelect`在Unity.UI画布情况下不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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