如何在unity中操作鼠标悬停事件

这篇文章将为大家详细讲解有关如何在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中操作鼠标悬停事件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。