-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkTag.cs
67 lines (54 loc) · 1.99 KB
/
LinkTag.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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/UI Toolkit/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()
{
m_UrlLookup = new Dictionary<int, string>()
{
{ 1, "https://www.google.com/" },
{ 2, "https://forum.unity.com/forums/ui-toolkit.178/" }
};
}
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);
}
}