如何使不可见的透明按钮起作用? [英] How to make an invisible transparent button work?

查看:274
本文介绍了如何使不可见的透明按钮起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看Unity论坛和Q& A网站中的一些答案,有关如何制作不可见按钮的答案无效,因为删除了按钮附带的图像会使该按钮无效.

Looking at some of the answers in the Unity forums and Q&A site, the answers for how to make an invisible button do not work because taking away the image affiliated with the button makes it not work.

如何解决此问题并在使按钮实际起作用的同时保留invisible属性?

How do you get around this and keep the invisible property while allowing the button to actually work?

推荐答案

这是关于Unity的怪异事物之一.

现实世界中有100%的项目需要此功能,但Unity却忘记了.

简短版本:

This is one of those weird things about Unity...

100% of real-world projects need this, but Unity forgot to do it.

Short version:

在每个Unity项目中都需要 Touchable.cs :

You need Touchable.cs in every Unity project:

// file Touchable.cs
// Correctly backfills the missing Touchable concept in Unity.UI's OO chain.

using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(Touchable))]
public class Touchable_Editor : Editor
     { public override void OnInspectorGUI(){} }
#endif
public class Touchable:Text
     { protected override void Awake() { base.Awake();} }

  1. 使用Unity的常规创建按钮"编辑器功能

您知道,编辑器功能会自动为您添加两个组件.一个是Text,另一个是Image ...

As you know, the editor function adds two components for you automatically. One is a Text and one is an Image...

只需将它们都删除

将上面的脚本Touchable.cs 放到按钮上

您完成了.这就是全部.

You are done. That's all there is to it.

它不能随着Unity升级而衰减".

It cannot "decay" with Unity upgrades.

实际上,您可以通过将Touchable放到.UI中的任何位置上来纽扣化" 任何 .

You can actually "buttonize" anything in .UI by dropping Touchable on top of it.

再也不用添加透明图像"来创建按钮了.

Never again "add a transparent Image" to make a button.

团结忘了在OO链中抽象一个可触摸"的概念.

Unity forgot to abstract a "touchable" concept in the OO chain.

因此,我们开发人员必须从Unity的类中创建自己的Touchable类.

So, us developers have to make our own Touchable class "from" Unity's classes.

这是OO中的经典回填"问题 .

This is a classic "backfilling" problem in OO.

当回填"时,唯一的问题是:它必须是完美的自动维护.每个人都只有一个很好的解决方案Touchable.cs.

When "backfilling" the only issue is that: it must be perfectly auto-maintaining. There is only one good solution, Touchable.cs, which everyone uses.

因此,在所有实际的Unity项目中,按钮都如下所示:

So in all real-world Unity projects a button looks like this:

一个,您拥有Unity的Button.cs

ONE You have Unity's Button.cs

两个,您必须添加Touchable.cs

一些团队创建了一个编辑器功能创建更好的按钮",该功能只是使用Button.cs + Touchable.cs来制作游戏对象.

Some teams make an editor function "Create Better Button" which simply makes a game object, with, Button.cs + Touchable.cs.

假设您的UI 面板可能非常复杂.因此,它会调整大小甚至具有动画.

Say you may have a very complex UI panel. So it resizes or even has an animation.

实际上,您可以将"Button + Touchable" 放到上,这样就可以了.

In fact, you can just drop "Button+Touchable" on to anything like that, and it will work.

只需将Button + Touchable设置为展开即可填充父级.这就是全部.

Just set the Button+Touchable so as to expand to fill the parent. That's all there is to it.

在此示例图像中,恢复"和退出"可以是任何内容. (动画,具有许多部分的复杂面板,文本,子画面,不可见的东西,堆栈-任何东西.)

In this example image, "resume" and "quit" could be anything. (An animation, a complicated panel with many parts, text, sprites, something invisible, a stack - anything.)

在所有情况下,只需将Button + Touchable 放在其下方,即可拥有完美无瑕的按钮.

In all cases, just drop a Button+Touchable underneath and you have a flawless button.

实际上:这种方法是如此简单,即使在简单的情况下,您也可能会使用它.

In fact: this approach is so simple, you'll probably use it for even simple cases.

说您的按钮很简单.仅具有图像,然后在其上放一个Button + Touchable容易得多. (而不是在编辑器中使用令人困惑且有问题的按钮"功能.)

Say your button is a trivial image. It's much easier to just have an image, and then drop a Button+Touchable on it. (Rather than use the confusing and problematic "Button" function in the editor.)

1)Unity的Button.cs类很棒.

1) Unity's Button.cs class is fantastic.

2)但是编辑器功能制作按钮"是垃圾...

2) But the editor function "make a Button" is garbage...

3)它制作了一个颠倒"按钮,

3) It makes an "upside down" button,

4),即,将文本/图像放在 Button.cs

4) i.e., it puts a text/image under Button.cs

5)按钮状态"是您应该能够 添加到 的所有内容.这正是Button + Touchable的工作方式.

5) "Button-ness" is something you should be able to add to anything at all. This is precisely how it works with Button+Touchable.

6)所以-很简单-

这就是每个人在Unity中完成所有按钮的方式!

That's how everyone does all buttons in Unity!

历史悠久的信誉:我相信Unity论坛用户"signalZak" 是很多年前第一个想到这一点的人!

Historic credit: I believe Unity forum user "signalZak" was the first to think this out many, many years ago!

这篇关于如何使不可见的透明按钮起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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