首先是按钮的代码,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class enter1 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void test()
{
SceneManager.LoadScene(2);
}
}
test函数中就是想要实现的功能
写好后将代码挂到按钮对象上,
再将按钮对象移到按钮的inspector页面的onclick项中的runtime项下了
(注意,是按钮对象拖过来,我之前一直是将代码拖进来,导致按钮一直没反应)
接下来就可以选择右侧的执行函数了
以上完成后开始测试就可以实现按钮功能了
另外,又一个困扰我半天的错误,是当我们使用starter assets时,放置UI时自动生成的Event System会与之起冲突导致出错,按钮无法使用
会产生如图所示错误
寻找半天终于在CSDN上得到了大佬的帮助——以下转自小小强2021
解决方案如下:
在2020版的Unity中的玩家设置中,将active lnput handling选项设置为Both。
如下图所示:
即可解决问题