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物体) : v$ j' M) M* a5 o$ I
OnMouseEnter() 鼠标进入
OnMouseExit() 鼠标离开 : v {1 |' q7 C4 n. ]' K0 h
3 z5 j9 Y) x/ R; b+ }) z8 e' H 二、3D物体 ! \, i( S' U! o. t4 r b
- t* ~8 y3 {, D6 k8 I& f& O
OnMouseEnter(),OnMouseExit()都是通过collider触发的,且碰撞器不能是trigger,鼠标进入,或离开collider时,自动调用这两个函数。9 m. f$ T6 ?5 h4 w$ n* A7 e! X- o
另外,OnMouseOver()类似,与OnMouseEnter()区别是,OnMouseOver()会当鼠标在该物体上collider内时,每帧调用1次,OnMouseEnter()仅在鼠标进入时调用1次。& b# ]1 r2 G3 H. X \/ \! t 二、UI - Q n. Q0 q1 _ p4 _. O UI部分通过eventTrigger组件实现类似功能 6 ], l' Z/ r4 n