Unity 實(shí)現(xiàn)鼠標(biāo)滑過(guò)UI時(shí)觸發(fā)動(dòng)畫的操作
在有些需求中會(huì)遇到,當(dāng)鼠標(biāo)滑過(guò)某個(gè)UI物體上方時(shí),為了提醒用戶該物體是可以交互時(shí),我們需要添加一個(gè)動(dòng)效和提示音。這樣可以提高產(chǎn)品的體驗(yàn)感。
解決方案
1、給需要有動(dòng)畫的物體制作相應(yīng)的Animation動(dòng)畫。(相同動(dòng)效可以使用同一動(dòng)畫復(fù)用)
2、給需要有動(dòng)畫的物體添加腳本。腳本如下:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class OnBtnEnter : MonoBehaviour, IPointerEnterHandler,IPointerExitHandler
{
//鼠標(biāo)進(jìn)入按鈕觸發(fā)音效和動(dòng)畫
public void OnPointerEnter(PointerEventData eventData)
{
// AudioManager.audioManager.PlayEnterAudio();//這里可以將播放觸發(fā)提示音放在這里,沒有可以提示音可以將該行注釋掉
if (gameObject.GetComponent<Animation>()!=null) {
if ( gameObject.GetComponent<Animation>() .isPlaying) {
return;
}
gameObject.GetComponent<Animation>().wrapMode = WrapMode.Loop;
gameObject.GetComponent<Animation>().Play();
}
}
//鼠標(biāo)離開時(shí)關(guān)閉動(dòng)畫
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();
}
}
}
補(bǔ)充:unity 通過(guò)OnMouseEnter(),OnMouseExit()實(shí)現(xiàn)鼠標(biāo)懸停時(shí)各種效果(UI+3D物體)
OnMouseEnter() 鼠標(biāo)進(jìn)入
OnMouseExit() 鼠標(biāo)離開
一、3D物體

OnMouseEnter(),OnMouseExit()都是通過(guò)collider觸發(fā)的,且碰撞器不能是trigger,鼠標(biāo)進(jìn)入,或離開collider時(shí),自動(dòng)調(diào)用這兩個(gè)函數(shù)。
另外,OnMouseOver()類似,與OnMouseEnter()區(qū)別是,OnMouseOver()會(huì)當(dāng)鼠標(biāo)在該物體上collider內(nèi)時(shí),每幀調(diào)用1次,OnMouseEnter()僅在鼠標(biāo)進(jìn)入時(shí)調(diào)用1次。
二、UI
UI部分通過(guò)eventTrigger組件實(shí)現(xiàn)類似功能
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無(wú)法使用OnMouseOver(),所以想實(shí)現(xiàn)漸變效果,可通過(guò)添加一個(gè)bool flag判斷,在update()方法中實(shí)現(xiàn)逐幀漸變效果。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
c#批量抓取免費(fèi)代理并且驗(yàn)證有效性的實(shí)戰(zhàn)教程
突破反爬蟲限制的方法之一就是多用幾個(gè)代理IP,下面這篇文章主要給大家介紹了關(guān)于利用c#批量抓取免費(fèi)代理并且驗(yàn)證有效性的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
C#生成設(shè)置范圍內(nèi)的Double類型隨機(jī)數(shù)的方法
這篇文章主要介紹了C#生成設(shè)置范圍內(nèi)的Double類型隨機(jī)數(shù)的方法,對(duì)于C#的初學(xué)者有很好的借鑒價(jià)值,需要的朋友可以參考下2014-08-08
C#如何將DataTable導(dǎo)出到Excel解決方案
由于公司項(xiàng)目中需要將系統(tǒng)內(nèi)用戶操作的所有日志進(jìn)行轉(zhuǎn)存?zhèn)浞荩紤]到以后可能還需要還原,所以最后決定將日志數(shù)據(jù)備份到Excel中2012-11-11
解決unity rotate旋轉(zhuǎn)物體 限制物體旋轉(zhuǎn)角度的大坑
這篇文章主要介紹了解決unity rotate旋轉(zhuǎn)物體 限制物體旋轉(zhuǎn)角度的大坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
在C#中調(diào)用Python代碼的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了在C#中調(diào)用Python代碼的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03

