Add hyperlinks in text
learn to add hyperlinks in text by an example.
読み終わるまでの所要時間 4 分最終更新 21日前
You can use rich text tags to add hyperlinks to text for the Editor UI or at runtime. For demonstration purposes, this example shows how to add hyperlinks in a custom Editor window.
Example overview
This example creates a custom Editor window with three hyperlinks. The first two links are created with tags that use link IDs. The third link uses an tag with an attribute to define the destination.
<link><a>hrefThe third link is created with an tag that uses the attribute to define the link's destination.
<a>hrefThe tag is a rich text tag that you can use to create links in UI Toolkit. Use the tag if you need to create many links or want to define custom link behavior with C# scripts.
<link><link>This example uses the following events to define the tag logic:
<link>- PointerDownLinkTagEvent
- PointerUpLinkTagEvent
- PointerMoveLinkTagEvent
- PointerOutLinkTagEvent
- PointerOverLinkTagEvent
You can find the completed files that this example creates in this GitHub repository.
Prerequisites
This guide is for developers familiar with the Unity Editor, UI Toolkit, and C# scripting. Before you start, get familiar with the following:
Create the example
To create a custom Editor window with hyperlinks with UI Toolkit, follow these steps:
-
Create a project in Unity with any template.
-
Right-click in thefolder, and select Create > UI Toolkit > Editor Window.
Assets -
In UI Toolkit Editor Window Creator, enterin the C# field.
LinkTag -
Select Confirm. This creates anfolder with the C# script, UXML, and USS files for your custom window.
Editor -
Double-clickto open it in the UI Builder.
LinkTag.uxml -
In the UI Builder, select the first label.
-
Replace the Text field with the following content:Link to <link="1"><color=#40a0ff><u>Unity</u></color></link>Link to <link="2"><color=#40a0ff><u>UITK Discussions</u></color></link>!
-
Enable Enable Rich Text.
-
Select Add Style Class to List.
-
Enterand press the Enter key. This adds a
linkclass to the first label.link -
Select the second label, and replace the Text field with the following content:Link to <a href="https://www.google.com/"><u>Google</u></a> using <noparse><a></noparse> tag.
-
Enable Enable Rich Text.
-
Save and close the UI Builder.
-
Replace the content ofwith the following:
LinkTag.ussLabel { font-size: 75px;}/*Turn the mouse pointer into a hand pointer when you hover over the link text. Note that the cursor override for the link value doesn’t work at runtime.*/.link-cursor { cursor: link;} -
Replace the content ofwith the following:
LinkTag.cs
using System.Collections.Generic;using UnityEditor;using UnityEngine;using UnityEngine.UIElements;using UnityEngine.UIElements.Experimental;public class LinkTag : EditorWindow{ [SerializeField] private VisualTreeAsset m_VisualTreeAsset = default; [MenuItem("Window/LinkTag Sample")] public static void ShowExample() { LinkTag wnd = GetWindow<LinkTag>(); wnd.titleContent = new GUIContent("LinkTag Sample"); } readonly string linkCursorClassName = "link-cursor"; Dictionary<int, string> m_UrlLookup; Label linkLabel; public void OnEnable() { // Use the dictionary to map link IDs to URLs. // The link ID is the number in the link tag, and the URL is the destination. // This is a simple example, in a real application you can define a more complex structure. m_UrlLookup = new Dictionary<int, string>() { { 1, "https://www.google.com/" }, { 2, "https://discussions.unity.com/" } }; } public void CreateGUI() { VisualElement uxml = m_VisualTreeAsset.Instantiate(); rootVisualElement.Add(uxml); linkLabel = rootVisualElement.Q<Label>(className: "link"); linkLabel.RegisterCallback<PointerDownLinkTagEvent>(HyperlinkOnPointerDown); linkLabel.RegisterCallback<PointerUpLinkTagEvent>(HyperlinkOnPointerUp); linkLabel.RegisterCallback<PointerMoveLinkTagEvent>(HyperlinkPointerMove); linkLabel.RegisterCallback<PointerOverLinkTagEvent>(HyperlinkOnPointerOver); linkLabel.RegisterCallback<PointerOutLinkTagEvent>(HyperlinkOnPointerOut); } void HyperlinkOnPointerOver(PointerOverLinkTagEvent _) { linkLabel.AddToClassList(linkCursorClassName); } void HyperlinkPointerMove(PointerMoveLinkTagEvent _) { } void HyperlinkOnPointerOut(PointerOutLinkTagEvent _) { linkLabel.RemoveFromClassList(linkCursorClassName); } void HyperlinkOnPointerDown(PointerDownLinkTagEvent _) { } void HyperlinkOnPointerUp(PointerUpLinkTagEvent evt) { var linkID = int.Parse(evt.linkID); if (m_UrlLookup.TryGetValue(linkID, out var url)) Application.OpenURL(url); }}
Test the link
To verify that the link works, follow these steps:
- From the manual, select Window > LinkTag Sample.
- Select the links in the custom window to open the linked websites.