Unity 实现鼠标滑过UI时触发动画的操作
作者:爱尚游Bin 时间:2021-05-31 19:24:18
在有些需求中会遇到,当鼠标滑过某个UI物体上方时,为了提醒用户该物体是可以交互时,我们需要添加一个动效和提示音。这样可以提高产品的体验感。
解决方案
1、给需要有动画的物体制作相应的Animation动画。(相同动效可以使用同一动画复用)
2、给需要有动画的物体添加脚本。脚本如下:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class OnBtnEnter : MonoBehaviour, IPointerEnterHandler,IPointerExitHandler
{
//鼠标进入按钮触发音效和动画
public void OnPointerEnter(PointerEventData eventData)
{
// AudioManager.audioManager.PlayEnterAudio();//这里可以将播放触发提示音放在这里,没有可以提示音可以将该行注释掉
if (gameObject.GetComponent<Animation>()!=null) {
if ( gameObject.GetComponent<Animation>() .isPlaying) {
return;
}
gameObject.GetComponent<Animation>().wrapMode = WrapMode.Loop;
gameObject.GetComponent<Animation>().Play();
}
}
//鼠标离开时关闭动画
public void OnPointerExit(PointerEventData eventData)
{
if ( gameObject.GetComponent<Animation>() != null )
{
if ( gameObject.GetComponent<Animation>().isPlaying )
{
gameObject.GetComponent<Animation>().wrapMode = WrapMode.Once;
return;
}
gameObject.GetComponent<Animation>().Stop();
}
}
}
补充:unity 通过OnMouseEnter(),OnMouseExit()实现鼠标悬停时各种效果(UI+3D物体)
OnMouseEnter() 鼠标进入
OnMouseExit() 鼠标离开
一、3D物体
OnMouseEnter(),OnMouseExit()都是通过collider触发的,且碰撞器不能是trigger,鼠标进入,或离开collider时,自动调用这两个函数。
另外,OnMouseOver()类似,与OnMouseEnter()区别是,OnMouseOver()会当鼠标在该物体上collider内时,每帧调用1次,OnMouseEnter()仅在鼠标进入时调用1次。
二、UI
UI部分通过eventTrigger组件实现类似功能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;//使用text,image组件
public class eventTriggrtTest : MonoBehaviour {
public Image image;
float ColorAlpha = 0f;//图片透明程度
public float speed = 0.75f;
bool flag = false;
private void Start()
{
image.GetComponent<Image>().color = new Color(255, 255, 255, ColorAlpha);
}
void Update()
{
// Debug.Log("OnMouseEnter");
if(flag == true)
{
if (ColorAlpha <= 0.75)
{
ColorAlpha += Time.deltaTime * speed;
image.GetComponent<Image>().color = new Color(255, 255, 255, ColorAlpha);
}
}
Debug.Log(ColorAlpha);
}
public void OnMouseEnter()
{
flag = true;
}
public void OnMouseExit()
{
// Debug.Log("OnMouseExit");
flag = false;
ColorAlpha = 0;
image.GetComponent<Image>().color = new Color(255, 255, 255, ColorAlpha);
}
}
因UI无法使用OnMouseOver(),所以想实现渐变效果,可通过添加一个bool flag判断,在update()方法中实现逐帧渐变效果。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/wks310/article/details/86573251
标签:Unity,鼠标,UI,动画
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解SpringBoot中如何使用布隆过滤器
2023-11-10 17:43:06
java方法及this关键字原理分析详解
2021-08-16 07:51:35
![](https://img.aspxhome.com/file/2023/4/73054_0s.png)
java8 stream自定义分组求和并排序的实现
2022-09-12 04:08:26
![](https://img.aspxhome.com/file/2023/5/79175_0s.png)
C#SuperSocket的搭建并配置启动总结
2022-01-25 15:16:24
![](https://img.aspxhome.com/file/2023/6/89756_0s.png)
RocketMQ存储文件的实现
2023-03-16 15:11:27
![](https://img.aspxhome.com/file/2023/9/63339_0s.png)
详解Spring Security认证流程
2022-04-16 15:48:46
![](https://img.aspxhome.com/file/2023/2/83502_0s.png)
一起学JAVA基础之运算符
2023-11-06 23:19:01
![](https://img.aspxhome.com/file/2023/6/73026_0s.png)
Spring Boot应用的极速部署脚本示例代码
2021-07-06 21:22:28
Java毕业设计实战之图片展览馆管理系统的实现
2021-06-16 08:23:18
![](https://img.aspxhome.com/file/2023/1/62521_0s.jpg)
详解Spring Bean的集合注入和自动装配
2023-02-18 15:02:58
![](https://img.aspxhome.com/file/2023/9/60829_0s.jpg)
Spring与Struts整合之让Spring管理控制器操作示例
2022-08-22 12:53:46
![](https://img.aspxhome.com/file/2023/2/84172_0s.png)
JAVA与SQL 中的null与NULL解析
2023-06-23 11:51:18
![](https://img.aspxhome.com/file/2023/0/72380_0s.png)
WPF实现多运算符表达式计算器
2023-07-17 10:31:40
![](https://img.aspxhome.com/file/2023/4/66574_0s.jpg)
spring boot thymeleaf 图片上传web项目根目录操作步骤
2023-11-23 10:50:46
优化常见的java排序算法
2022-03-25 05:11:54
Unity实现图形相交检测
2021-09-16 09:31:01
![](https://img.aspxhome.com/file/2023/8/80068_0s.jpg)
java线程并发控制同步工具CountDownLatch
2022-09-02 12:18:06
![](https://img.aspxhome.com/file/2023/1/75461_0s.jpg)
Java中的final关键字使用方式
2023-01-16 15:04:25
浅谈Java转义符\\\\|
2022-12-11 16:25:43
Mybatis在sqlite中无法读写byte[]类问题的解决办法
2021-08-06 21:06:07