最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity 實現(xiàn)鼠標滑過UI時觸發(fā)動畫的操作

 更新時間:2021年04月10日 16:01:58   作者:愛尚游Bin  
這篇文章主要介紹了Unity 實現(xiàn)鼠標滑過UI時觸發(fā)動畫的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

在有些需求中會遇到,當(dāng)鼠標滑過某個UI物體上方時,為了提醒用戶該物體是可以交互時,我們需要添加一個動效和提示音。這樣可以提高產(chǎn)品的體驗感。

解決方案

1、給需要有動畫的物體制作相應(yīng)的Animation動畫。(相同動效可以使用同一動畫復(fù)用)

2、給需要有動畫的物體添加腳本。腳本如下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class OnBtnEnter : MonoBehaviour, IPointerEnterHandler,IPointerExitHandler
{
    //鼠標進入按鈕觸發(fā)音效和動畫
    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();
        }
    }
//鼠標離開時關(guān)閉動畫
    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()實現(xiàn)鼠標懸停時各種效果(UI+3D物體)

OnMouseEnter() 鼠標進入

OnMouseExit() 鼠標離開

一、3D物體

OnMouseEnter(),OnMouseExit()都是通過collider觸發(fā)的,且碰撞器不能是trigger,鼠標進入,或離開collider時,自動調(diào)用這兩個函數(shù)。

另外,OnMouseOver()類似,與OnMouseEnter()區(qū)別是,OnMouseOver()會當(dāng)鼠標在該物體上collider內(nèi)時,每幀調(diào)用1次,OnMouseEnter()僅在鼠標進入時調(diào)用1次。

二、UI

UI部分通過eventTrigger組件實現(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無法使用OnMouseOver(),所以想實現(xiàn)漸變效果,可通過添加一個bool flag判斷,在update()方法中實現(xiàn)逐幀漸變效果。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評論

玉门市| 四会市| 万山特区| 远安县| 泰来县| 甘南县| 兴和县| 长兴县| 五莲县| 惠水县| 三台县| 铜梁县| 剑阁县| 唐河县| 白山市| 米林县| 登封市| 黄陵县| 剑川县| 调兵山市| 滨海县| 玉屏| 顺义区| 济宁市| 招远市| 马山县| 板桥市| 二连浩特市| 浑源县| 资兴市| 沅陵县| 犍为县| 囊谦县| 固镇县| 洮南市| 根河市| 金阳县| 洛扎县| 托克托县| 阳朔县| 大埔区|