简体   繁体   English

unity3d和许多按钮-寻找一种平滑的方式来处理它

[英]unity3d and lot of buttons - looking for a smooth way to handle it

Im doing gui in Unity (ugui) right now and i want to make lot of buttons and transitions. 我现在正在Unity(ugui)中做gui,我想进行很多按钮和过渡。 Is there a smoother way to do it than using transition method with screen ID/few onclick methods with int parameter? 是否有比使用带有屏幕ID的过渡方法/带有int参数的少量onclick方法更流畅的方法? I tried to do onClick method base on enums but its not possible to add enum method to ugui button (tried integer but it can be confusing with lot of buttons). 我试图基于枚举进行onClick方法,但是无法将枚举方法添加​​到ugui按钮(尝试使用整数,但是它可能与许多按钮混淆)。

Nika Kasradze: putting method based on string in inspector in not a good idea. Nika Kasradze:在检查器中基于字符串的放置方法不是一个好主意。 You need write same string in onClick method later, and writing two same strings in not a good idea. 您稍后需要在onClick方法中写入相同的字符串,并且在同一情况下编写两个相同的字符串也不是一个好主意。

I used custom button function. 我使用了自定义按钮功能。 You just add it to button and it calls a function in UI manager called "onButtonClick" and pass button name as parameter. 您只需将其添加到按钮,它就会在UI管理器中调用一个名为“ onButtonClick”的函数,并将按钮名称作为参数传递。 Then you have onButtonClick method where you set up all buttons using button names. 然后,您具有onButtonClick方法,可以在其中使用按钮名称设置所有按钮。 You can use multiple buttons with same name to call one function or use unique name to call unique function. 您可以使用多个具有相同名称的按钮来调用一个功能,也可以使用唯一名称来调用唯一功能。 onButtonClick method is really easy to read and you dont have to set up every button in inspector!!! onButtonClick方法非常易于阅读,您不必在检查器中设置每个按钮!!!

using UnityEngine;
using UnityEngine.UI;

{
    [RequireComponent (typeof(Button))]
    public class CustomButton : MonoBehaviour
    {

        void Awake ()
        {
            Button btn = GetComponent<Button> ();
            btn.onClick.AddListener (() => ManagerUI.Instance.onButtonClick (gameObject.name));
        }

    }
}

and you put something like this in ManagerUI 然后在ManagerUI中放入这样的内容

void onButtonClick(string buttonName){
switch(buttonName){
case "back_btn": GoBack();
case "openshop_btn": OpenShop();
case "closepopup_btn": ClosePopup();
}
}

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

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