溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何使用unity實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)時(shí)ui及物體的變色操作

發(fā)布時(shí)間:2021-04-12 11:40:02 來(lái)源:億速云 閱讀:1355 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了如何使用unity實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)時(shí)ui及物體的變色操作,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

1、實(shí)現(xiàn)UI的變色

設(shè)置Highlighted Color為鼠標(biāo)經(jīng)過(guò)時(shí)變的顏色(Normal為常態(tài),Pressed為按下時(shí)的顏色,Disabled為禁止的顏色)

如何使用unity實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)時(shí)ui及物體的變色操作

2、通過(guò)代碼實(shí)現(xiàn)物體的顏色改變

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cube_change : MonoBehaviour 
{    private Color CubeColor;
    private Texture CubeTexture;
    public GameObject objCube;
 // Use this for initialization
 void Start () 
       {             objCube = GameObject.Find("Cube");
             objCube.GetComponent<Renderer>().material.color = Color.blue;
 }
       public void OnMouseEnter()
       {
            objCube.GetComponent<Renderer>().material.color = Color.red;
       }
      public void OnMouseExit()
      {
            objCube.GetComponent<Renderer>().material.color = Color.blue;
       }
       // Update is called once per frame
      void Update ()
      { 
 }

//+++++++++++++++++++++++++++

unity5.0之后renderer就不能使用material,需要使用GetComponent來(lái)獲取

GameObject objcub = GameObject.CreatePrimitive(PrimitiveType.Cube);  
objcub.AddComponent<Rigidbody>();  
objcub.name = "Cube";  
//設(shè)置color 使用這個(gè)來(lái)獲取material  
objcub.GetComponent<Renderer>().material.color = Color.blue;

補(bǔ)充:Unity 實(shí)現(xiàn)鼠標(biāo)滑過(guò)UI時(shí)觸發(fā)動(dòng)畫(huà)

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

解決方案

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

2、給需要有動(dòng)畫(huà)的物體添加腳本。腳本如下:

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)畫(huà)
    public void OnPointerEnter(PointerEventData eventData)
    {
      //  AudioManager.audioManager.PlayEnterAudio();//這里可以將播放觸發(fā)提示音放在這里,沒(méi)有可以提示音可以將該行注釋掉
        if (gameObject.GetComponent<Animation>()!=null) {
            if ( gameObject.GetComponent<Animation>() .isPlaying) {
                return;
            }
            gameObject.GetComponent<Animation>().wrapMode = WrapMode.Loop;
            gameObject.GetComponent<Animation>().Play();
        }
    }
//鼠標(biāo)離開(kāi)時(shí)關(guān)閉動(dòng)畫(huà)
    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人物接近時(shí)觸發(fā)事件或動(dòng)畫(huà)demo

定義物體GameObject o;

效果:當(dāng)人物接近物體時(shí),物體觸發(fā)動(dòng)畫(huà),比如位移

1.創(chuàng)建o的動(dòng)畫(huà)km和gm

2.創(chuàng)建空物體 Empty,大小稍微比o大一點(diǎn),拖入o,用來(lái)接受觸發(fā)判定,防止物體移動(dòng)過(guò)后觸發(fā)器跟著移動(dòng),勾選 is trigger

2.人物控制器

using System.Collections;
using System.Collections.Generic;
using UnityEngine; 
public class DoorController : MonoBehaviour
{
private Animation ani;
 
void Start() {
//獲取子組件下的第一個(gè)組件,再獲取子組件animation,
//如果是獲取自身組件,直接GetComponent<XXX>()
ani = transform.GetChild(0).GetComponent<Animation>();
}
 
private void OnTriggerEnter(Collider other){
//當(dāng)物體接觸到時(shí)則播放animation中的km動(dòng)畫(huà)
ani.Play("km");
}
 
private void OnTriggerExit(Collider other){
//當(dāng)物體接觸到時(shí)則播放animation中的gm動(dòng)畫(huà)
ani.Play("gm");
}
 
void Update()
{
 
}
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何使用unity實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)時(shí)ui及物體的變色操作”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI