层级窗口中右键添加按钮
会同时生成一个Canvas容器,设置按钮和UI和摄像机视野比例相同:UI随屏幕比例缩放
选中按钮,在属性窗口中新建一个脚本
生成的脚本默认在Assets根目录下
双击打开脚本并编辑
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class test : MonoBehaviour
{
public NewBehaviourScript1 other;//NewBehaviourScript1是控制角色动画的脚本里的类,设为public公有可以直接把挂载了含有要调用变量的类的脚本的动画对象拖进来
// 执行一次
void Start()
{
this.GetComponent<Button>().onClick.AddListener(delegate ()//按钮点击事件
{
print("test");
other.Test = true;//控制NewBehaviourScript1脚本里的变量(变量也要声明为public)
});
}
// Update is called once per frame
void Update()
{
}
}
接着把挂载了要调用变量的对象拖进去
运行测试,Over