Unity 按钮控制函数脚本事件-简单学

简单创建个按钮,将本脚本放在任意场景中物体上执行即可

本脚本实现了 利用按钮控制关闭某UI面板的功能。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UI_Hide : MonoBehaviour
{
    //通过按钮关闭debug 开关

    Button Hidedebuguibt;
  [SerializeField] private GameObject OneHidedUI;
    private void Awake()
    {
       
        Hidedebuguibt = GameObject.FindWithTag("HideUIBT").GetComponent<Button>();//从场景中拿到按钮
        Debug.Log("按钮拿到"+ Hidedebuguibt.name);
    }
    private void Update()
    {
        do {
            if (Hidedebuguibt == null||OneHidedUI == null)
            {
                Debug.Log("没有按钮或者物体!");
                break;
            }
            else
            {
                Hidedebuguibt.onClick.AddListener(HideUI);//为按钮添加点击事件(点击后执行的函数)
            }
        } while (false);
    }
    void HideUI()
    {
        Debug.Log("点击了关闭UI的按钮,安排!");
        OneHidedUI.SetActive(false);
    }
}

猜你喜欢

转载自blog.csdn.net/leoysq/article/details/124620428
今日推荐