首页 > 解决方案 > 为什么我的函数没有出现在 Unity 的 On Click 字段中?

问题描述

我为按钮编写了一个简单的脚本,并把它扔到了按钮的“On Click”中,但是我的Button1函数并没有显示在那里。如何解决这个问题呢?

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

public class Nikita : MonoBehaviour
{
public void Button1()
{
    Debug.Log("button clicked");
}

// Start is called before the first frame update
void Start()
{
    Debug.Log("started");
}

// Update is called once per frame
void Update()
{
    
}  
}

标签: unity3d

解决方案


我会尝试实现IPointerClickHandler

按照文档中的建议,“场景中存在事件系统以允许点击检测”。你可以检查这个例子。

对于您的情况,可能类似于(未调试):

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

public class Nikita : MonoBehaviour, IPointerClickHandler
{
    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("button clicked");
    }

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("started");
    }

    // Update is called once per frame
    void Update()
    {
    
    }  
}

推荐阅读