这篇文章主要介绍了Unity 实现鼠标滑过UI时触发动画的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
在有些需求中会遇到,当鼠标滑过某个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()方法中实现逐帧渐变效果。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持得得之家。如有错误或未考虑完全的地方,望不吝赐教。
本文标题为:Unity 实现鼠标滑过UI时触发动画的操作
				
        
 
            
        - 在C# 8中如何使用默认接口方法详解 2023-03-29
 - Oracle中for循环的使用方法 2023-07-04
 - .NET CORE DI 依赖注入 2023-09-27
 - user32.dll 函数说明小结 2022-12-26
 - 如何使用C# 捕获进程输出 2023-03-10
 - c# 模拟线性回归的示例 2023-03-14
 - C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
 - WPF使用DrawingContext实现绘制刻度条 2023-07-04
 - Unity Shader实现模糊效果 2023-04-27
 - Unity3D实现渐变颜色效果 2023-01-16
 
						
						
						
						
						
				
				
				
				