unity 鼠標(biāo)懸停事件操作
筆者在網(wǎng)上發(fā)現(xiàn)了,很多種方法 ,當(dāng)然咱們找最好用的,也簡單的 下面廢話不多說直接上代碼 我在啰嗦幾句 第一這個腳本掛在需要相應(yīng)的游戲體上 第二被掛游戲體必須帶有collider, 第三僅僅制作完上面的兩步 本應(yīng)該沒有問題,
筆者又發(fā)現(xiàn)一個問題 就是只有鼠標(biāo)在物體的右上方才會很靈敏的相應(yīng)到 在在左下方反而沒什么反應(yīng) ,為此筆者在腳本上加上了一句
this.GetComponent<BoxCollider> ().size = new Vector3 (1.5f, 1.5f, 1.5f);
原來物體的size 為(1,1,1) 我的目的是加大物體本身的碰撞體 結(jié)果順利的實(shí)現(xiàn)了
下面是完整
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Shubiaoxuanting : MonoBehaviour{ bool ischanger; //上移動 0為未上移 1為上移 int move_up=0; void Start () { ischanger = false; //擴(kuò)大碰撞體的尺寸 方便檢測鼠標(biāo)懸停 this.GetComponent<BoxCollider> ().size = new Vector3 (1.5f, 1.5f, 1.5f); //給麻將初始狀態(tài)的位置 this.transform.position = new Vector3 (this.transform.position.x, 0, this.transform.position.z); } //鼠標(biāo)在物體上面引起的動作 void OnMouseOver(){ ischanger = true; this.transform.position = new Vector3 (this.transform.position.x, 0.2f, this.transform.position.z); } // void OnMouseEnter(){ // ischanger = true; // move_up = 1; // Debug.Log ("3333333"); // // } //鼠標(biāo)不再上面引起的動作 void OnMouseExit(){ ischanger = false; this.transform.position = new Vector3 (this.transform.position.x, 0, this.transform.position.z); } /鼠標(biāo)按下 /鼠標(biāo)按下 void OnMouseDown(){ } } //鼠標(biāo)松開 //鼠標(biāo)松開 void OnMousePut(){} void OnMousePut(){} void Update () { // if (ischanger){ //Debug.Log ("33333"); //this.transform.position = new Vector3 (this.transform.position.x, 0.2f, this.transform.position.z) } else { // this.transform.position = new Vector3 (this.transform.position.x, 0, this.transform.position.z); // }}}
補(bǔ)充:Unity UGUI Button鼠標(biāo)的懸停事件(利用重寫unity中的button來實(shí)現(xiàn))
我就廢話不多說了,大家還是直接看代碼吧~
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems; public class LearnButton : Button { /// /// 配合Unity的其他方法使用,就能達(dá)到你想要的效果!這里只是拋磚引玉,大家有更好的方法歡迎跟我交流! /// /// /// protected override void DoStateTransition(SelectionState state, bool instant) { base.DoStateTransition(state, instant); switch (state) { case SelectionState.Disabled: break; case SelectionState.Highlighted: Debug.Log("鼠標(biāo)移到button上!"); break; case SelectionState.Normal: Debug.Log("鼠標(biāo)離開Button!"); break; case SelectionState.Pressed: break; default: break; } } }
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
- unity AudioSource播放完聲音后要執(zhí)行的函數(shù)或條件操作
- unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周
- Unity3d 使用Gizmos畫一個圓圈
- unity 如何使用LineRenderer 動態(tài)劃線
- Unity 通過LineRenderer繪制兩點(diǎn)之間的直線操作
- Unity 實(shí)現(xiàn)給物體替換材質(zhì)球
- 基于Unity Line Renderer組件的常用屬性說明
- Unity3D 計時器的實(shí)現(xiàn)代碼(三種寫法總結(jié))
- 解決unity rotate旋轉(zhuǎn)物體 限制物體旋轉(zhuǎn)角度的大坑
相關(guān)文章
C#中接口的顯式實(shí)現(xiàn)與隱式實(shí)現(xiàn)及其相關(guān)應(yīng)用案例詳解
最近在學(xué)習(xí)演化一款游戲項(xiàng)目框架時候,框架作者巧妙使用接口中方法的顯式實(shí)現(xiàn)來變相對接口中方法進(jìn)行“密封”,增加實(shí)現(xiàn)接口的類訪問方法的“成本”,這篇文章主要介紹了C#中接口的顯式實(shí)現(xiàn)與隱式實(shí)現(xiàn)及其相關(guān)應(yīng)用案例,需要的朋友可以參考下2024-05-05unity使用socket實(shí)現(xiàn)聊天室功能
這篇文章主要為大家詳細(xì)介紹了unity使用socket實(shí)現(xiàn)聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-03-03同時兼容JS和C#的RSA加密解密算法詳解(對web提交的數(shù)據(jù)加密傳輸)
這篇文章主要給大家介紹了關(guān)于同時兼容JS和C#的RSA加密解密算法,通過該算法可以對web提交的數(shù)據(jù)進(jìn)行加密傳輸,文中通過圖文及示例代碼介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面來一起看看吧。2017-07-07