unity 鼠标放在button上显示名字

新建一个脚本 继承button 里面可以获取button 的四中状态 在里面可以写一些要触发的事件
using UnityEngine;
using UnityEngine.UI;

public class Button_Faker : Button
{
enum Selection
{
Normal,
Highlighted,
Pressed,
Disabled
}
Selection selection;

protected override void DoStateTransition(SelectionState state, bool instant)
{

    base.DoStateTransition(state, instant);
    switch (state)
    {
        //四种状态
        case SelectionState.Normal:
            selection = Selection.Normal;
            break;
        case SelectionState.Highlighted:
            selection = Selection.Highlighted;
            break;
        case SelectionState.Pressed:
            selection = Selection.Pressed;
            break;
        case SelectionState.Disabled:
            selection = Selection.Disabled;
            break;
        default:
            break;
    }
}

private void OnGUI()
{
    GUI.skin.box.fontSize = 12;
    switch (selection)
    {
        case Selection.Highlighted:
            GUI.Box(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, 100, 25),this.name);
            break;
        case Selection.Pressed:
           // GUI.Box(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, 100, 25), "Pressed");
            break;
        default:
            break;
    }
}

}

unity 鼠标放在button上显示名字_第1张图片

你可能感兴趣的:(unity基础)