QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

泡泡马甲APP 更多内容请下载泡泡马甲手机客户端APP 立即下载 ×
查看: 2234|回复: 0

[C#] Unity实现鼠标滑过UI时触发动画的操作

[复制链接]

等级头衔

积分成就    金币 : 2806
   泡泡 : 1516
   精华 : 6
   在线时间 : 1244 小时
   最后登录 : 2024-5-5

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2021-4-10 22:06:25 | 显示全部楼层 |阅读模式
      在有些需求中会遇到,当鼠标滑过某个UI物体上方时,为了提醒用户该物体是可以交互时,我们需要添加一个动效和提示音。这样可以提高产品的体验感。: Q+ R  c, `% E" b# B0 k8 i7 _" I
一、解决方案
4 u0 r% [0 k( T) m1、给需要有动画的物体制作相应的Animation动画。(相同动效可以使用同一动画复用)" i- f5 P$ @& q( I
2、给需要有动画的物体添加脚本。脚本如下:
  ^0 F, L/ ?: V. v( R
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. public class OnBtnEnter : MonoBehaviour, IPointerEnterHandler,IPointerExitHandler
  7. {
  8.     //鼠标进入按钮触发音效和动画
  9.     public void OnPointerEnter(PointerEventData eventData)
  10.     {
  11.       //  AudioManager.audioManager.PlayEnterAudio();//这里可以将播放触发提示音放在这里,没有可以提示音可以将该行注释掉
  12.         if (gameObject.GetComponent<Animation>()!=null) {
  13.             if ( gameObject.GetComponent<Animation>() .isPlaying) {
  14.                 return;
  15.             }
  16.             gameObject.GetComponent<Animation>().wrapMode = WrapMode.Loop;
  17.             gameObject.GetComponent<Animation>().Play();
  18.         }
  19.     }
  20. //鼠标离开时关闭动画
  21.     public void OnPointerExit(PointerEventData eventData)
  22.     {
  23.         if ( gameObject.GetComponent<Animation>() != null )
  24.         {
  25.             if ( gameObject.GetComponent<Animation>().isPlaying )
  26.             {
  27.                 gameObject.GetComponent<Animation>().wrapMode = WrapMode.Once;
  28.                 return;               
  29.             }
  30.             gameObject.GetComponent<Animation>().Stop();
  31.         }
  32.     }
  33. }
      补充:unity 通过OnMouseEnter(),OnMouseExit()实现鼠标悬停时各种效果(UI+3D物体)
6 }& Y6 {  u0 H3 W9 ~5 p2 b
  • OnMouseEnter() 鼠标进入
  • OnMouseExit() 鼠标离开8 a# l. M# d* C4 G1 r

8 K" m& ?& u1 o5 A; S. p$ J0 ~; v二、3D物体0 A" Q- P7 t. i/ e  E
1.jpg

2 i$ x$ a1 g( S& p2 {7 z       OnMouseEnter(),OnMouseExit()都是通过collider触发的,且碰撞器不能是trigger,鼠标进入,或离开collider时,自动调用这两个函数。* x4 K6 r! ^% }3 Z3 x5 b
       另外,OnMouseOver()类似,与OnMouseEnter()区别是,OnMouseOver()会当鼠标在该物体上collider内时,每帧调用1次,OnMouseEnter()仅在鼠标进入时调用1次。
. w, r; O, G+ X二、UI
1 g% @; ~) A1 o# D) L+ e       UI部分通过eventTrigger组件实现类似功能
2 H; I9 _$ Z! p! A1 i3 _
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;//使用text,image组件
  5. public class eventTriggrtTest : MonoBehaviour {
  6.     public Image image;
  7.     float ColorAlpha = 0f;//图片透明程度
  8.     public float speed = 0.75f;
  9.     bool flag = false;
  10.     private void Start()
  11.     {
  12.         image.GetComponent<Image>().color = new Color(255, 255, 255, ColorAlpha);
  13.     }
  14.     void Update()
  15.     {
  16.     //    Debug.Log("OnMouseEnter");
  17.         if(flag == true)
  18.         {
  19.             if (ColorAlpha <= 0.75)
  20.             {
  21.                 ColorAlpha += Time.deltaTime * speed;
  22.                 image.GetComponent<Image>().color = new Color(255, 255, 255, ColorAlpha);
  23.             }
  24.         }
  25.            Debug.Log(ColorAlpha);
  26.     }
  27.     public void OnMouseEnter()
  28.     {
  29.         flag = true;
  30.     }
  31.     public void OnMouseExit()
  32.     {
  33.         //    Debug.Log("OnMouseExit");
  34.         flag = false;
  35.             ColorAlpha = 0;
  36.             image.GetComponent<Image>().color = new Color(255, 255, 255, ColorAlpha);      
  37.     }   
  38. }
      因UI无法使用OnMouseOver(),所以想实现渐变效果,可通过添加一个bool flag判断,在update()方法中实现逐帧渐变效果。: J0 A. b8 [1 }: B  _
) {2 [2 @; S! \' }$ U$ v. {8 v8 ]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号 )

GMT+8, 2024-5-7 15:33

Powered by paopaomj X3.4 © 2016-2024 sitemap

快速回复 返回顶部 返回列表