Unity3D: How to create a link button, with fun

21 Jan

How to create a link style button in Unity3D? Here the considerations include:

  1. The link button should look like a label, instead of a button
  2. The link button should work like a button, with clickable responses such as navigating to a URL;
  3. When the mouse hovers, the mouse cursor should look like a hand cursor, instead of the arrow cursor.



Emmmmm, interesting! But it is not as easy as imagined to get it implemented. Let’s get started!

To meet requirement (1) and (2) , we can use below code:

imageBut you may find that this works well, but the color of text is not blue as of a true link button!


To change the text color to blue, we can improve the implementation with below code. Specifically by setting the use rich text to true to the style, and use html tags:

imageNow this looks better, and the color matches our expectation. But note the cursor, it is still arrow, instead of hand cursor.


Let’s move one step further as following:


We first get the rect of the button control as in Line (1), and then use EditorGUILayout.AddCursorRect( ) to specify the link cursor, see Line (3). Cool, this works! See the cursor now!


But hold on, note that when the mouse is over the same line, but a little bit off the text, the cursor is still hand/link cursor! This is because we are using GUILayout.Button, whose rect is the entire parent’s width! To fix this, see Line (2), where we calculate the width of the link’s text, and adjust the rect’s width. Oh yeah! Done!

image  image

To recap, we can use below function to create a Link button in Unity3D.

        private void LinkButton(string caption, string url)
            var style =;
            style.richText = true;
            caption = string.Format(“<color=#0000FF>{0}</color>”, caption);

            bool bClicked = GUILayout.Button(caption, style);

            var rect = GUILayoutUtility.GetLastRect();
            rect.width = style.CalcSize(new GUIContent(caption)).x;
            EditorGUIUtility.AddCursorRect(rect, MouseCursor.Link);

            if (bClicked)

Happy coding!

Leave a comment

Posted by on January 21, 2015 in Programming, Unity 3d


Tags: , , , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: