Skip to content

Commit

Permalink
Made GUIStyle acquisition compatible with older editor versions
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnulusGames committed Jan 21, 2024
1 parent 7b2f39a commit 6434f77
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/NuGetForUnity/Editor/Ui/NugetWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ private static void GUILayoutLink(string url)
rect.yMin -= 2f;
rect.xMin += 30f;

if (GUI.Button(rect, url, EditorStyles.linkLabel))
if (GUI.Button(rect, url, Styles.LinkLabelStyle))
{
Application.OpenURL(url);
}
Expand Down
36 changes: 35 additions & 1 deletion src/NuGetForUnity/Editor/Ui/Styles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal static class Styles
private static GUIStyle cachedSearchFieldStyle;
private static GUIStyle cachedHeaderStyle;
private static GUIStyle cachedBackgroundStyle;
private static GUIStyle cachedLinkLabelStyle;

public static Color LineColor
{
Expand Down Expand Up @@ -107,7 +108,8 @@ public static GUIStyle SearchFieldStyle
return cachedSearchFieldStyle;
}

cachedSearchFieldStyle = new GUIStyle(EditorStyles.toolbarSearchField)
var original = GetStyle("ToolbarSearchTextField");
cachedSearchFieldStyle = new GUIStyle(original)
{
fontSize = 12,
fixedHeight = 20f,
Expand All @@ -117,6 +119,20 @@ public static GUIStyle SearchFieldStyle
}
}

public static GUIStyle LinkLabelStyle
{
get
{
if (cachedLinkLabelStyle != null)
{
return cachedLinkLabelStyle;
}

cachedLinkLabelStyle = GetStyle("LinkLabel");
return cachedLinkLabelStyle;
}
}

/// <summary>
/// From here: http://forum.unity3d.com/threads/changing-the-background-color-for-beginhorizontal.66015/.
/// </summary>
Expand All @@ -139,6 +155,24 @@ private static Texture2D CreateSingleColorTexture(Color color)

return result;
}

private static GUIStyle GetStyle(string styleName)
{
GUIStyle style = GUI.skin.FindStyle(styleName);

if (style == null)
{
style = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).FindStyle(styleName);
}

if (style == null)
{
Debug.LogError("Missing built-in guistyle " + styleName);
style = new GUIStyle();
}

return style;
}
}
}

Expand Down

0 comments on commit 6434f77

Please sign in to comment.