Unity3D用代码实现图片按键

Unity3D用代码实现图片按键

1,

    public Texture textureButton;

    void OnGUI()
    {
        if (!textureButton)
        {
            Debug.LogError("请在inspector窗口给textureButton一张图片");
            return;
        }

        if (GUI.Button(new Rect(10, 10, 500, 500), textureButton))//图片做按键
            Debug.Log("Clicked the button with an image");

        if (GUI.Button(new Rect(10, 670, 200, 30), "Click"))//文字按键
            Debug.Log("Clicked the button with text");
    }

2,

3,运行即可看到。

image-20210413175712522