2013-11-20 34 views
1

我试图让我的GUI创作更容易一些,并在同一时间了解它。 我不想购买或使用第三方插件。OnMouseEnter on Rect,Unity3D

我试图让我可以整体使用按键脚本,我至今也对此做出:

using UnityEngine; 
using System.Collections; 

[ExecuteInEditMode()] 
public class SceneBTN : MonoBehaviour { 

    public GUIStyle myGui; 
    public int theHeight = 50; 
    public int theWidth = 200; 
    public float verticalPlacement = 50; 
    public string buttonText = "Button Text"; 

    void OnGUI() { 
     Rect BTNtext = new Rect(Screen.width/2 - (theWidth/2), Screen.height - verticalPlacement, theWidth, theHeight); 
     GUI.Label(BTNtext, buttonText, myGui); 
    } 

    void OnMouseEnter() { 
     print("test"); 
    } 

} 

当我尝试OnMouseEnter在我期待着得到测试的打印在控制台,但没有出现。

我想我缺少一些非常基本的东西,因为我已经找到了对stackoverflow很大的帮助,我再次转向你们(你很棒的方式)。

下面是截图: enter image description here

回答

2

做到这一点,最简单的方法是在OnGUI方法的地方下面的代码

GUI.Button (new Rect(0,0,10,10), new GUIContent("Button 1", "Button 1")); 
     string hover = GUI.tooltip; 

     if(hover=="Button 1"){ 
      Debug.Log("Mouse is over button 1"); 
     }