unity實(shí)現(xiàn)鼠標(biāo)經(jīng)過時(shí)ui及物體的變色操作
1、實(shí)現(xiàn)UI的變色
設(shè)置Highlighted Color為鼠標(biāo)經(jīng)過時(shí)變的顏色(Normal為常態(tài),Pressed為按下時(shí)的顏色,Disabled為禁止的顏色)
2、通過代碼實(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)滑過UI時(shí)觸發(fā)動(dòng)畫
在有些需求中會(huì)遇到,當(dāng)鼠標(biāo)滑過某個(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人物接近時(shí)觸發(fā)事件或動(dòng)畫demo
定義物體GameObject o;
效果:當(dāng)人物接近物體時(shí),物體觸發(fā)動(dòng)畫,比如位移
1.創(chuàng)建o的動(dòng)畫km和gm
2.創(chuàng)建空物體 Empty,大小稍微比o大一點(diǎn),拖入o,用來(lái)接受觸發(fā)判定,防止物體移動(dòng)過后觸發(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)畫 ani.Play("km"); } private void OnTriggerExit(Collider other){ //當(dāng)物體接觸到時(shí)則播放animation中的gm動(dòng)畫 ani.Play("gm"); } void Update() { } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- Unity3d 使用Gizmos畫一個(gè)圓圈
- unity 如何使用LineRenderer 動(dòng)態(tài)劃線
- Unity 通過LineRenderer繪制兩點(diǎn)之間的直線操作
- Unity 實(shí)現(xiàn)給物體替換材質(zhì)球
- 基于Unity Line Renderer組件的常用屬性說明
- Unity3D 計(jì)時(shí)器的實(shí)現(xiàn)代碼(三種寫法總結(jié))
- unity 鼠標(biāo)懸停事件操作
- Unity 從Resources中動(dòng)態(tài)加載Sprite圖片的操作
- unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周
相關(guān)文章
Unity實(shí)現(xiàn)簡(jiǎn)單場(chǎng)景分層移動(dòng)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡(jiǎn)單場(chǎng)景分層移動(dòng),分為前景、場(chǎng)景、背景等,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09winform使用委托和事件來(lái)完成兩個(gè)窗體之間通信的實(shí)例
這篇文章介紹了winform使用委托和事件來(lái)完成兩個(gè)窗體之間通信的實(shí)例,有需要的朋友可以參考一下2013-09-09WPF中不規(guī)則窗體與WindowsFormsHost控件兼容問題的解決方法
這篇文章主要介紹了WPF中不規(guī)則窗體與WindowsFormsHost控件兼容問題的解決方法,對(duì)比以往的解決方案,給出了一個(gè)具有普遍性的技巧,具有一定的借鑒價(jià)值,需要的朋友可以參考下2014-11-11淺談C#單例模式的實(shí)現(xiàn)和性能對(duì)比
這篇文章主要介紹了淺談C#單例模式的實(shí)現(xiàn)和性能對(duì)比的相關(guān)資料,詳細(xì)的介紹了6種實(shí)現(xiàn)方式,需要的朋友可以參考下2017-09-09Dynamic和Var的區(qū)別及dynamic使用詳解
C#中的很多關(guān)鍵詞用法比較容易混淆,var和dynamic就是其中一組,他們都可以申明動(dòng)態(tài)類型的變量,但是本質(zhì)上他們還是有不少區(qū)別的,下面通過本文給大家介紹Dynamic和Var的區(qū)別及如何正確使用dynamic,需要的朋友參考下2016-01-01算法證明每一位都相同十進(jìn)制數(shù)不是完全平方數(shù)
這篇文章主要為大家介紹了算法證明每一位都相同十進(jìn)制數(shù)不是完全平方數(shù)的過程論述,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05C# Form自定義光標(biāo)的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了C# Form自定義光標(biāo)的簡(jiǎn)單實(shí)現(xiàn),有需要的朋友可以參考一下2014-01-01