U3D绘制一个Button

作者:追风剑情 发布于:2014-4-24 23:32 分类:Unity3d

1.找张按钮图片放在Assets目录下

2.新建个脚本并挂在主摄像机上

using UnityEngine;
using System.Collections;

public class ButtonTest : MonoBehaviour {

    public Texture buttonTexture;

    void OnGUI()
    {
        float scaleX = Screen.width / 640.0f;  //横向放缩比
        float scaleY = Screen.height / 960.0f; //纵向放缩比

        //要绘制到的屏幕位置
        Rect rect = new Rect(50 * scaleX, 50 * scaleY, 86 * scaleX, 26 * scaleY);

        if (GUI.Button(rect, buttonTexture, new GUIStyle()))
        {
            Debug.Log("Button Click");
        }
    }
}

 

u3d_button1.png

 

运行效果:

u3c_button2.png

 

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号