本文介绍LinkFileLabelField和LinkUrlLabelField

前言

本文目的在于封装一个能够Link到项目下的asset,和一个打开浏览器URL的EditorField。

源码

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public static class EditorGUILayoutExtension
{
private static GUIStyle _linkStyle = null;

private static GUIStyle LinkStyle
{
get
{
if (_linkStyle == null)
{
_linkStyle = new GUIStyle(EditorStyles.label);
_linkStyle.wordWrap = false;
_linkStyle.fontSize = 11;
_linkStyle.normal.textColor = new Color(0x00 / 255f, 0x78 / 255f, 0xDA / 255f, 1f);
_linkStyle.stretchWidth = false;
}

return _linkStyle;
}
}

public static void LinkFileLabelField(string label, string assetPath, params GUILayoutOption[] options)
{
LinkFileLabelField_internal(new GUIContent(label), AssetDatabase.LoadAssetAtPath<Object>(assetPath), options);
}

public static void LinkFileLabelField(string label, Object linkObject, params GUILayoutOption[] options)
{
LinkFileLabelField_internal(new GUIContent(label), linkObject, options);
}

public static void LinkFileLabelField(GUIContent label, string assetPath, params GUILayoutOption[] options)
{
LinkFileLabelField_internal(label, AssetDatabase.LoadAssetAtPath<Object>(assetPath), options);
}

public static void LinkFileLabelField(GUIContent label, Object linkObject, params GUILayoutOption[] options)
{
LinkFileLabelField_internal(label, linkObject, options);
}

private static void LinkFileLabelField_internal(GUIContent label, Object linkObject, params GUILayoutOption[] options)
{
var position = LinkLabelField_internal(label, options);
if (GUI.Button(position, label, LinkStyle))
{
Selection.activeObject = linkObject;
EditorGUIUtility.PingObject(linkObject);
}
}

public static void LinkUrlLabelField(string label, string url, params GUILayoutOption[] options)
{
LinkUrlLabelField_internal(new GUIContent(label), url, options);
}

public static void LinkUrlLabelField(GUIContent label, string url, params GUILayoutOption[] options)
{
LinkUrlLabelField_internal(label, url, options);
}

private static void LinkUrlLabelField_internal(GUIContent label, string url, params GUILayoutOption[] options)
{
var position = LinkLabelField_internal(label, options);
if (GUI.Button(position, label, LinkStyle))
{
Application.OpenURL(url);
}
}

private static Rect LinkLabelField_internal(GUIContent label, params GUILayoutOption[] options)
{
var position = GUILayoutUtility.GetRect(label, LinkStyle, options);
Handles.BeginGUI();
Handles.color = LinkStyle.normal.textColor;
Handles.DrawLine(new Vector3(position.xMin, position.yMax), new Vector3(position.xMax, position.yMax));
Handles.color = Color.white;
Handles.EndGUI();
EditorGUIUtility.AddCursorRect(position, MouseCursor.Link);
return position;
}
}

使用

1
2
EditorGUILayoutExtension.LinkFileLabelField("TestPrefab", "Assets/TestPrefab.prefab");
EditorGUILayoutExtension.LinkUrlLabelField("www.baidu.com","https://www.baidu.com");

在写插件的时候,文档里面可以使用LinkUrlLabelField链接到你的插件主页去,还是比较方便得啦。

本篇文章,如果有不懂,欢迎留言~click here!

以上知识分享,如有错误,欢迎指出,共同学习,共同进步。如果有不懂,欢迎留言评论!