unity 0基础自学2.1:unity 中button的各类状态

news2025/2/23 1:55:35

文章目录

  • 1、Button的状态
  • 2、脚本中获取button的状态
    • 2.1 分析状态获取
    • 2.2 通过实现接口获取button的状态
      • 2.2.1 鼠标点击与释放
      • 2.2.2 高亮模式
      • 2.2.3 退出选中模式(高亮状态)
      • 2.2.4 选择模式selected
      • 2.2.5 退出选择模式
  • 3、射线与UI交互设置
    • 3.1 Canvas中组件设置
    • 3.2 EventSystem中组件设置

1、Button的状态

在Inspector中可见,对于button来说,不同的按钮状态呈现不同的颜色,如下图所示,共5种颜色。
在这里插入图片描述

①Normal
UI控件正常情况的状态
②Highlighted
高亮模式:
当射线与UI交互时,呈现为高亮模式,射线变成白色。
鼠标置于UI上方时所处的状态

③Pressed
鼠标左键长按状态
④Selected
选中状态,比如鼠标点击后,
⑤Disabled
button设置为不可点击,不可选择的状态

2、脚本中获取button的状态

2.1 分析状态获取

由于button为Selectable的子类,所以button中状态都通过Selectable中的实现进行处理。
接下来,让我们开始分析Selectable源码,并通过源码获取如何处理button的状态。

首先在Selectoable中知道状态类别,如下所示:

 protected SelectionState currentSelectionState
 {
     get
     {
         if (!IsInteractable())
             return SelectionState.Disabled;
         if (isPointerDown)
             return SelectionState.Pressed;
         if (hasSelection)
             return SelectionState.Selected;
         if (isPointerInside)
             return SelectionState.Highlighted;
         return SelectionState.Normal;
     }
 }

在Selectoable及Selectoable的子类中,可以通过currentSelectionState获取状态,但是在其他类中,无法直接调用其变量。

 // Change the button to the correct state
 private void EvaluateAndTransitionToSelectionState()
 {
     if (!IsActive() || !IsInteractable())
         return;

     DoStateTransition(currentSelectionState, false);
 }

在EvaluateAndTransitionToSelectionState()方法中,根据不同动作修改button的状态,继续向上寻找。

 public virtual void OnPointerEnter(PointerEventData eventData)
 {
     isPointerInside = true;
     EvaluateAndTransitionToSelectionState();
 }

看到上述代码是不是比较熟悉了,变量isPointerInside为ture时, currentSelectionState的值为SelectionState.Highlighted,所以通过这里可以发现,获取button的状态,直接复写OnPointerEnter方法就可以了。
在Selectoable类中,也有比较明显的注释了。

2.2 通过实现接口获取button的状态

2.2.1 鼠标点击与释放

button的状态为 Pressed
OnPointerDown :
Do this when the mouse is clicked over the selectable object this script is attached to.
鼠标点击到这个脚本所附加的可选择对象上时,执行此操作。

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;// Required when using Event data.

 public class ExampleClass : MonoBehaviour, IPointerDownHandler// required interface when using the OnPointerDown method.
  {
    //Do this when the mouse is clicked over the selectable object this script is attached to.
      public void OnPointerDown(PointerEventData eventData)
     {
         Debug.Log(this.gameObject.name + " Was Clicked.");
     }
 }

OnPointerUp :
Do this when the mouse click on this selectable UI object is released.
当用户在点击这个可选择的用户界面(UI)对象后释放鼠标按钮时,执行这个动作。
OnPointerDown is also required to receive OnPointerUp callbacks
为了接收 OnPointerUp 回调,也需要实现 OnPointerDown。

      using UnityEngine;
      using System.Collections;
      using UnityEngine.UI;
      using UnityEngine.EventSystems;// Required when using Event data.
     
      public class ExampleClass : MonoBehaviour, IPointerUpHandler, IPointerDownHandler// These are the interfaces the OnPointerUp method requires.
     {
          //OnPointerDown is also required to receive OnPointerUp callbacks
          public void OnPointerDown(PointerEventData eventData)
          {
          }
     
       //Do this when the mouse click on this selectable UI object is released.
         public void OnPointerUp(PointerEventData eventData)
         {
              Debug.Log("The mouse click was released");
         }
     }

2.2.2 高亮模式

button的状态:Highlighted
Do this when the cursor enters the rect area of this selectable UI object.
当光标进入这个可选择的用户界面(UI)对象的矩形区域时,执行这个动作。
当用户的鼠标光标,射线(或触摸点,在触摸设备上)移动到某个可交互的UI元素中

   using UnityEngine;
   using System.Collections;
   using UnityEngine.UI;
    using UnityEngine.EventSystems;// Required when using Event data.
   
   public class ExampleClass : MonoBehaviour, IPointerEnterHandler// required interface when using the OnPointerEnter method.
    {
      //Do this when the cursor enters the rect area of this selectable UI object.
      public void OnPointerEnter(PointerEventData eventData)
       {
          Debug.Log("The cursor entered the selectable UI element.");
       }
   }

2.2.3 退出选中模式(高亮状态)

button的状态:Normal
Do this when the cursor exits the rect area of this selectable UI object.
当光标离开这个可选择的用户界面(UI)对象的矩形区域时,执行这个动作。

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;// Required when using Event data.

 public class ExampleClass : MonoBehaviour, IPointerExitHandler// required interface when using the OnPointerExit method.
 {
    //Do this when the cursor exits the rect area of this selectable UI object.
     public void OnPointerExit(PointerEventData eventData)
    {
          Debug.Log("The cursor exited the selectable UI element.");
     }
 }

2.2.4 选择模式selected

button的状态:Selected
Do this when the selectable UI object is selected.
当这个可选择的用户界面(UI)对象被选中时,执行这个动作
通过鼠标点击、键盘导航等方式

  using UnityEngine;
  using System.Collections;
  using UnityEngine.UI;
  using UnityEngine.EventSystems;// Required when using Event data.
  
   public class ExampleClass : MonoBehaviour, ISelectHandler// required interface when using the OnSelect method.
   {
       //Do this when the selectable UI object is selected.
       public void OnSelect(BaseEventData eventData)
       {
          Debug.Log(this.gameObject.name + " was selected");
       }
  }

2.2.5 退出选择模式

button状态:Normal

   using UnityEngine;
   using System.Collections;
   using UnityEngine.UI;
   using UnityEngine.EventSystems;// Required when using Event data.
  
   public class ExampleClass : MonoBehaviour, IDeselectHandler This Interface is required to receive OnDeselect callbacks.
  {
      public void OnDeselect(BaseEventData data)
      {
          Debug.Log("Deselected");
      }
  }

3、射线与UI交互设置

3.1 Canvas中组件设置

在这里插入图片描述
Graphic Raycaster组件为鼠标等物理输入进行处理,
Tracked Device Graphic Raycaster组件为射线交互的组件,如果Canvas中没有该组件,射线无法与该UI进行交互,当射线与UI进行交互时,射线会由红色变为白色。

3.2 EventSystem中组件设置

对于EventSystem中的设置,想要射线能够与UI之间进行交互,在Inspector中需要将组件Standalone Input Module进行移除,并添加组件XR UI Input Module,两者同时进行处理,才能实现射线与UI之间进行交互。

移除EventSystem中的Standalone Input Module中的组件
如果不移除上述组件的话,对于射线的交互效果可能出现问题
在这里插入图片描述添加组件XR UI Input Module
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2298256.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

《C++ Primer》学习笔记(一)

第一部分:C基础 在C和C编程语言中,main函数必须返回int类型的值。这一要求自C标准的第一次规范(C89,也叫ANSI C)开始就已经明确规定了。std::endl和\n都用于插入换行符。std::endl除了换行,还会强制刷新输…

DedeBIZ系统审计小结

之前简单审计过DedeBIZ系统,网上还没有对这个系统的漏洞有过详尽的分析,于是重新审计并总结文章,记录下自己审计的过程。 https://github.com/DedeBIZ/DedeV6/archive/refs/tags/6.2.10.zip 📌DedeBIZ 系统并非基于 MVC 框架&…

C#快速排序QuickSort将递归算法修改为堆栈Stack非递归方式

我们知道,方法的调用是采用Stack的方式[后进先出:LIFO], 在DeepSeek中快速搜索C#快速排序, 搜索结果如图: 我们会发现是采用递归的方式 . 递归的优点: 简单粗暴,类似于直接写数学公式,因代码量较少,易于理解.递归与循环迭代的运行次数都是一致的 递归的缺点: 占用大量的内…

【DeepSeek × Postman】请求回复

新建一个集合 在 Postman 中创建一个测试集合 DeepSeek API Test,并创建一个关联的测试环境 DeepSeek API Env,同时定义两个变量 base_url 和 api_key 的步骤如下: 1. 创建测试集合 DeepSeek API Test 打开 Postman。点击左侧导航栏中的 Co…

[免费]Springboot+Vue医疗(医院)挂号管理系统【论文+源码+SQL脚本】

大家好,我是java1234_小锋老师,看到一个不错的SpringbootVue医疗(医院)挂号管理系统,分享下哈。 项目视频演示 【免费】SpringBootVue医疗(医院)挂号管理系统 Java毕业设计_哔哩哔哩_bilibili 项目介绍 在如今社会上,关于信息上…

计算机毕业设计PySpark+hive招聘推荐系统 职位用户画像推荐系统 招聘数据分析 招聘爬虫 数据仓库 Django Vue.js Hadoop

温馨提示:文末有 CSDN 平台官方提供的学长联系方式的名片! 温馨提示:文末有 CSDN 平台官方提供的学长联系方式的名片! 温馨提示:文末有 CSDN 平台官方提供的学长联系方式的名片! 作者简介:Java领…

Jmeter+Influxdb+Grafana平台监控性能测试过程

一、Jmeter自带插件监控 下载地址:https://jmeter-plugins.org/install/Install/ 安装:下载后文件为jmeter-plugins-manager-1.3.jar,将其放入jmeter安装目录下的lib/ext目录,然后重启jmeter,即可。 启动Jmeter&…

ROACH

End-to-End Urban Driving by Imitating a Reinforcement Learning Coach CARLA-Roach ICCV‘21论文:模仿一个强化学习教练的端到端城市驾驶 文章目录 Roach输入BEV语义分割图像测量向量 Roach输出训练策略网络价值网络 具体实现由 Roach 监督的模仿学习&#xff08…

机械臂运动学笔记(一):正向运动学

正向运动学指的是通过相邻关节间的转动和移动坐标,将末端的坐标计算出来。 反向运动学指的是已知机械臂末端的坐标,反算每个关节可能的转动和移动参数。 参考资料:4.机械臂几何法与DH表示法_哔哩哔哩_bilibili 一.任意连杆连接的变量定义&a…

【DuodooBMS】给PDF附件加“受控”水印的完整Python实现

给PDF附件加“受控”水印的完整Python实现 功能需求 在实际工作中,许多文件需要添加水印以标识其状态,例如“受控”“机密”等。对于PDF文件,添加水印不仅可以增强文件的可识别性,还可以防止未经授权的使用。本代码的功能需求是…

GitCode 助力 Dora SSR:开启游戏开发新征程

项目仓库(点击阅读原文链接可直达) https://gitcode.com/ippclub/Dora-SSR 跨越技术藩篱,构建游戏开发乐园 Dora SSR 是一款致力于打破游戏开发技术壁垒的开源游戏引擎。其诞生源于开发者对简化跨平台游戏开发环境搭建的强烈渴望&#xff0…

Mediamtx+Python读取webrtc流

一、功能思路: 1、我采用ffmpeg -re -stream_loop -1 -i xcc.mp4 -c:v libx264 -profile:v baseline -x264opts "bframes0:repeat_headers1" -b:v 1500k -preset fast -f flv rtmp://127.0.0.1:1835/stream/111推流到mediamtx的rtmp上 2、通过mediamtx自…

每日一题——矩阵最长递增路径

矩阵最长递增路径问题 题目描述数据范围:进阶要求:示例示例 1示例 2 题解思路算法步骤:代码实现代码解释复杂度分析总结 题目描述 给定一个 n 行 m 列的矩阵 matrix,矩阵内所有数均为非负整数。你需要在矩阵中找到一条最长路径&a…

【CLIP系列】4:目标检测(ViLD、GLIP)

目录 1 ViLD2 GLIP2.1 前言2.2 损失计算2.3 模型框架 1 ViLD OPEN-VOCABULARY OBJECT DETECTION VIA VISION AND LANGUAGE KNOWLEDGE DISTILLATION 从标题就能看出来,作者是把CLIP模型当成一个Teacher,去蒸馏他自己的网络,从而能Zero Shot去…

Cesium for Unity Linux版本

Cesium for Unity 直装不支持Linux 参照官方开发流程一些操作命令issues 宝藏最后运行图 参照官方开发流程 https://github.com/CesiumGS/cesium-unity/blob/main/Documentation~/developer-setup.md 系统已经安装过dotnet和cmake xuefeixuefei:~$ dotnet --version 9.0.102 …

关于 IoT DC3 中驱动(Driver)的理解

在开源IoT DC3物联网系统中,驱动(Driver)扮演着至关重要的角色,它充当了软件系统与物理设备之间的桥梁。驱动的主要功能是依据特定的通信协议连接到设备,并根据设备模板中配置的位号信息进行数据采集和指令控制。不同的…

EasyRTC嵌入式WebRTC视频通话SDK支持Web浏览器、Linux、ARM、Android、iOS

随着互联网技术的飞速发展,实时通信(RTC)已经成为现代应用中不可或缺的一部分。无论是视频会议、在线教育、远程医疗,还是社交娱乐,实时通信技术都在其中扮演着重要角色。 然而,WebRTC技术在PC和移动端的支…

数据库脚本MySQL8转MySQL5

由于生产服务器版本上部署的是MySQL5,而开发手里的脚本代码是MySQL8。所以只能降版本了… 升级版本与降级版本脚本转换逻辑一样 MySQL5与MySQL8版本SQL脚本区别 大多数无需调整、主要是字符集与排序规则 MySQL5与MySQL8版本SQL字符集与排序规则 主要操作&…

【PGCCC】commit_delay 对性能的提升:PostgreSQL 基准测试

通过禁用参数可以来调整事务工作负载synchronous_commit。该措施有惊人效果。但在操作系统崩溃期间丢失已提交事务的可能性使其成为许多应用程序无法启动的因素。因此我决定写下来。 WAL 刷新是事务数据库工作负载的瓶颈 为了确保已提交的事务不会丢失,PostgreSQL…

AI大模型随机初始化权重并打印网络结构方法(以Deepseekv3为例,单机可跑)

背景 当前大模型的权重加载和调用,主要是通过在HuggingFace官网下载并使用transformer的库来加以实现;其中大模型的权重文件较大(部分>100GB),若只是快速研究网络结构和数据流变化,则无需下载权重。本文…