这篇文章将为大家详细讲解有关如何在unity中操作鼠标悬停事件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
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; //扩大碰撞体的尺寸 方便检测鼠标悬停
this.GetComponent<BoxCollider> ().size = new Vector3 (1.5f, 1.5f, 1.5f);
//给麻将初始状态的位置 this.transform.position = new Vector3
(this.transform.position.x, 0, this.transform.position.z); }
//鼠标在物体上面引起的动作 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");
// // }
//鼠标不再上面引起的动作
void OnMouseExit(){
ischanger = false;
this.transform.position = new Vector3 (this.transform.position.x, 0, this.transform.position.z);
} /鼠标按下
/鼠标按下
void OnMouseDown(){
}
}
//鼠标松开
//鼠标松开
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);
// }}}
补充:Unity UGUI Button鼠标的悬停事件(利用重写unity中的button来实现)
我就废话不多说了,大家还是直接看代码吧~
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class LearnButton : Button
{
///
/// 配合Unity的其他方法使用,就能达到你想要的效果!这里只是抛砖引玉,大家有更好的方法欢迎跟我交流!
///
///
///
protected override void DoStateTransition(SelectionState state, bool instant)
{
base.DoStateTransition(state, instant);
switch (state)
{
case SelectionState.Disabled:
break;
case SelectionState.Highlighted:
Debug.Log("鼠标移到button上!");
break;
case SelectionState.Normal:
Debug.Log("鼠标离开Button!");
break;
case SelectionState.Pressed:
break;
default:
break;
}
}
}
关于如何在unity中操作鼠标悬停事件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。