using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace TyphoonGUIStyle
{
///
/// GUI样式
///
public class Styles
{
private static string _rootPath = null;
private static string RootPath
{
get
{
if (_rootPath == null)
{
var p1 = $"Assets/typhoon.unity.guistyle";
var p2 = $"Packages/typhoon.unity.guistyle";
if (Directory.Exists(p1))
{
_rootPath = p1;
}
else if (Directory.Exists(p2))
{
_rootPath = p2;
}
}
return _rootPath;
}
}
public static Color ColorSelected = EditorGUIUtility.isProSkin
? new Color(0.24f, 0.37f, 0.59f, 1f)
: new Color(0.24f, 0.49f, 0.91f, 1f);
public static Color ColorGreen = EditorGUIUtility.isProSkin
? new Color(0.07f, 0.42f, 0.16f, 1f)
: new Color(0.58f, 1f, 0.7f, 1f);
public static Color ColorBlue = EditorGUIUtility.isProSkin
? new Color(0.15f, 0.38f, 0.41f, 1f)
: new Color(0.58f, 0.95f, 1f, 1f);
public static Color ColorRed = EditorGUIUtility.isProSkin
? new Color(0.42f, 0.07f, 0.07f, 1f)
: new Color(1f, 0.58f, 0.59f, 1f);
public static Color ColorYellow = EditorGUIUtility.isProSkin
? new Color(0.43f, 0.4f, 0f, 1f)
: new Color(1f, 0.97f, 0.58f, 1f);
#region 图片
private static Dictionary _texture2Ds = new Dictionary();
private static Texture2D Get2D(string name)
{
if (_texture2Ds.TryGetValue(name, out var match))
{
return match;
}
return null;
}
private static Dictionary _styles = new Dictionary();
private static GUIStyle GetStyle(string name)
{
if (_styles.TryGetValue(name, out var match))
{
return match;
}
return null;
}
public static Texture2D Green2DNormal => LoadTheme2D("green_normal.png");
public static Texture2D Green2DHover => LoadTheme2D("green_hover.png");
public static Texture2D Green2DActive => LoadTheme2D("green_active.png");
public static Texture2D Blue2DNormal => LoadTheme2D("blue_normal.png");
public static Texture2D Blue2DHover => LoadTheme2D("blue_hover.png");
public static Texture2D Blue2DActive => LoadTheme2D("blue_active.png");
public static Texture2D Yellow2DNormal => LoadTheme2D("yellow_normal.png");
public static Texture2D Yellow2DHover => LoadTheme2D("yellow_hover.png");
public static Texture2D Yellow2DActive => LoadTheme2D("yellow_active.png");
public static Texture2D Red2DNormal => LoadTheme2D("red_normal.png");
public static Texture2D Red2DHover => LoadTheme2D("red_hover.png");
public static Texture2D Red2DActive => LoadTheme2D("red_active.png");
public static Texture2D General2DNormal => LoadTheme2D("n_normal.png");
public static Texture2D General2DHover => LoadTheme2D("n_hover.png");
public static Texture2D General2DActive => LoadTheme2D("n_active.png");
public static Texture2D Menu2DActive => LoadTheme2D("menu_active.png");
public static Texture2D HelpBox2D => LoadTheme2D("help_box.png");
public static Texture2D TitleBar2D => LoadTheme2D("title_bar.png");
public static Texture2D IconWarning => LoadIcon2D("warning.png");
public static Texture2D IconInfo => LoadIcon2D("info.png");
public static Texture2D IconError => LoadIcon2D("error.png");
#endregion
#region 默认按钮-圆角
public static GUIStyle Btn => CreateColorButtonStyle("Default.Btn", General2DNormal, General2DHover,
General2DActive, (style) => { style.alignment = TextAnchor.MiddleCenter; });
public static GUIStyle BtnLeft => CreateColorButtonStyle("Default.BtnLeft", General2DNormal, General2DHover,
General2DActive, (style) => { style.alignment = TextAnchor.MiddleLeft; });
public static GUIStyle BtnRight => CreateColorButtonStyle("Default.BtnRight", General2DNormal, General2DHover,
General2DActive, (style) => { style.alignment = TextAnchor.MiddleRight; });
#endregion
#region 菜单栏按钮-圆角
public static GUIStyle MenuButton => CreateColorButtonStyle("Default.MenuButton", General2DNormal,
General2DHover,
General2DActive, (style) => { style.alignment = TextAnchor.MiddleLeft; });
public static GUIStyle MenuButtonActive => CreateColorButtonStyle("Default.MenuButtonActive", Menu2DActive,
Menu2DActive,
Menu2DActive, (style) =>
{
style.alignment = TextAnchor.MiddleLeft;
style.fontStyle = FontStyle.Bold;
});
#endregion
#region 绿色按钮-圆角
public static GUIStyle BtnGreen => CreateColorButtonStyle("Default.BtnGreen", Green2DNormal, Green2DHover,
Green2DActive,
(style) => { style.alignment = TextAnchor.MiddleCenter; });
public static GUIStyle BtnGreenLeft => CreateColorButtonStyle("Default.BtnGreenLeft", Green2DNormal,
Green2DHover,
Green2DActive,
(style) => { style.alignment = TextAnchor.MiddleLeft; });
public static GUIStyle BtnGreenRight => CreateColorButtonStyle("Default.BtnGreenRight", Green2DNormal,
Green2DHover,
Green2DActive,
(style) => { style.alignment = TextAnchor.MiddleRight; });
#endregion
#region 蓝色按钮-圆角
public static GUIStyle BtnBlue => CreateColorButtonStyle("Default.BtnBlue", Blue2DNormal, Blue2DHover,
Blue2DActive,
(style) => { style.alignment = TextAnchor.MiddleCenter; });
public static GUIStyle BtnBlueLeft => CreateColorButtonStyle("Default.BtnBlueLeft", Blue2DNormal, Blue2DHover,
Blue2DActive,
(style) => { style.alignment = TextAnchor.MiddleLeft; });
public static GUIStyle BtnBlueRight => CreateColorButtonStyle("Default.BtnBlueRight", Blue2DNormal, Blue2DHover,
Blue2DActive,
(style) => { style.alignment = TextAnchor.MiddleRight; });
#endregion
#region 黄色按钮-圆角
public static GUIStyle BtnYellow => CreateColorButtonStyle("Default.BtnYellow", Yellow2DNormal, Yellow2DHover,
Yellow2DActive,
(style) => { style.alignment = TextAnchor.MiddleCenter; });
public static GUIStyle BtnYellowLeft => CreateColorButtonStyle("Default.BtnYellowLeft", Yellow2DNormal,
Yellow2DHover,
Yellow2DActive,
(style) => { style.alignment = TextAnchor.MiddleLeft; });
public static GUIStyle BtnYellowRight => CreateColorButtonStyle("Default.BtnYellowRight", Yellow2DNormal,
Yellow2DHover,
Yellow2DActive,
(style) => { style.alignment = TextAnchor.MiddleRight; });
#endregion
#region 红色按钮-圆角
public static GUIStyle BtnRed => CreateColorButtonStyle("Default.BtnRed", Red2DNormal, Red2DHover,
Red2DActive,
(style) => { style.alignment = TextAnchor.MiddleCenter; });
public static GUIStyle BtnRedLeft => CreateColorButtonStyle("Default.BtnRedLeft", Red2DNormal, Red2DHover,
Red2DActive,
(style) => { style.alignment = TextAnchor.MiddleLeft; });
public static GUIStyle BtnRedRight => CreateColorButtonStyle("Default.BtnRedRight", Red2DNormal, Red2DHover,
Red2DActive,
(style) => { style.alignment = TextAnchor.MiddleRight; });
#endregion
#region Label
public static GUIStyle HelpBox => CreateStyle("Default.HelpBox", GUI.skin.textArea, (style) =>
{
style.border = new RectOffset(6, 6, 6, 6);
style.margin = new RectOffset(2, 2, 2, 2);
style.padding = new RectOffset(4, 4, 4, 4);
style.normal.background = HelpBox2D;
style.normal.scaledBackgrounds = Array.Empty();
style.alignment = TextAnchor.UpperLeft;
style.stretchWidth = true;
style.fontSize = 11;
});
public static GUIStyle BoldLabel => CreateStyle("Default.BoldLabel", "label", (style) =>
{
style.border = new RectOffset(2, 2, 2, 2);
style.margin = new RectOffset(2, 2, 2, 2);
style.padding = new RectOffset(4, 4, 4, 4);
style.fontStyle = FontStyle.Bold;
style.alignment = TextAnchor.UpperLeft;
});
public static GUIStyle BoldLabelMiddle => CreateStyle("Default.BoldLabelMiddle", "label", (style) =>
{
style.border = new RectOffset(2, 2, 2, 2);
style.margin = new RectOffset(2, 2, 2, 2);
style.padding = new RectOffset(4, 4, 4, 4);
style.fontStyle = FontStyle.Bold;
style.alignment = TextAnchor.MiddleLeft;
});
public static GUIStyle TitleBar => CreateStyle("Default.TitleBar", "label", (style) =>
{
style.border = new RectOffset(2, 2, 2, 2);
style.margin = new RectOffset(2, 2, 2, 2);
style.padding = new RectOffset(4, 4, 4, 4);
style.overflow = new RectOffset(0, 0, 0, 0);
style.normal.background = TitleBar2D;
style.fontStyle = FontStyle.Bold;
style.stretchWidth = true;
style.alignment = TextAnchor.MiddleLeft;
});
#endregion
public class Square
{
//Square 绿色
public static Texture2D SquareGreen2DNormal => LoadTheme2D("square_g_normal.png");
public static Texture2D SquareGreen2DHover => LoadTheme2D("square_g_hover.png");
public static Texture2D SquareGreen2DActive => LoadTheme2D("square_g_active.png");
//Square 蓝色
public static Texture2D SquareBlue2DNormal => LoadTheme2D("square_b_normal.png");
public static Texture2D SquareBlue2DHover => LoadTheme2D("square_b_hover.png");
public static Texture2D SquareBlue2DActive => LoadTheme2D("square_b_active.png");
//Square 红色
public static Texture2D SquareRed2DNormal => LoadTheme2D("square_r_normal.png");
public static Texture2D SquareRed2DHover => LoadTheme2D("square_r_hover.png");
public static Texture2D SquareRed2DActive => LoadTheme2D("square_r_active.png");
//Square 黄色
public static Texture2D SquareYellow2DNormal => LoadTheme2D("square_y_normal.png");
public static Texture2D SquareYellow2DHover => LoadTheme2D("square_y_hover.png");
public static Texture2D SquareYellow2DActive => LoadTheme2D("square_y_active.png");
//Square 默认色
public static Texture2D SquareGeneral2DNormal => LoadTheme2D("square_n_normal.png");
public static Texture2D SquareGeneral2DHover => LoadTheme2D("square_n_hover.png");
public static Texture2D SquareGeneral2DActive => LoadTheme2D("square_n_active.png");
public static Texture2D SquareMenu2DActive => LoadTheme2D("square_menu_active.png");
#region 默认按钮-方形
public static GUIStyle Btn => CreateColorSquareButtonStyle("Square.Btn", SquareGeneral2DNormal,
SquareGeneral2DHover, SquareGeneral2DActive, (style) => { style.alignment = TextAnchor.MiddleCenter; });
public static GUIStyle BtnLeft => CreateColorSquareButtonStyle("Square.BtnLeft",
SquareGeneral2DNormal,
SquareGeneral2DHover, SquareGeneral2DActive, (style) => { style.alignment = TextAnchor.MiddleLeft; });
public static GUIStyle BtnRight => CreateColorSquareButtonStyle("Square.BtnRight",
SquareGeneral2DNormal,
SquareGeneral2DHover, SquareGeneral2DActive, (style) => { style.alignment = TextAnchor.MiddleRight; });
#endregion
#region 菜单栏按钮-方形
public static GUIStyle MenuButton => CreateColorSquareButtonStyle("Square.MenuButton",
SquareGeneral2DNormal,
SquareGeneral2DHover, SquareGeneral2DActive,
(style) => { style.alignment = TextAnchor.MiddleLeft; });
public static GUIStyle MenuButtonActive => CreateColorSquareButtonStyle("Square.MenuButtonActive",
SquareMenu2DActive,
SquareMenu2DActive, SquareMenu2DActive,
(style) =>
{
style.alignment = TextAnchor.MiddleLeft;
style.fontStyle = FontStyle.Bold;
});
#endregion
#region 绿色按钮-方形
public static GUIStyle BtnGreen => CreateColorSquareButtonStyle("Square.BtnGreen",
SquareGreen2DNormal,
SquareGreen2DHover,
SquareGreen2DActive,
(style) => { style.alignment = TextAnchor.MiddleCenter; });
public static GUIStyle BtnGreenLeft => CreateColorSquareButtonStyle("Square.BtnGreenLeft",
SquareGreen2DNormal,
SquareGreen2DHover,
SquareGreen2DActive,
(style) => { style.alignment = TextAnchor.MiddleLeft; });
public static GUIStyle BtnGreenRight => CreateColorSquareButtonStyle("Square.BtnGreenRight",
SquareGreen2DNormal,
SquareGreen2DHover,
SquareGreen2DActive,
(style) => { style.alignment = TextAnchor.MiddleRight; });
#endregion
#region 蓝色按钮-方形
public static GUIStyle BtnBlue => CreateColorSquareButtonStyle("Square.BtnBlue",
SquareBlue2DNormal,
SquareBlue2DHover,
SquareBlue2DActive,
(style) => { style.alignment = TextAnchor.MiddleCenter; });
public static GUIStyle BtnBlueLeft => CreateColorSquareButtonStyle("Square.BtnBlueLeft",
SquareBlue2DNormal,
SquareBlue2DHover,
SquareBlue2DActive,
(style) => { style.alignment = TextAnchor.MiddleLeft; });
public static GUIStyle BtnBlueRight => CreateColorSquareButtonStyle("Square.BtnBlueRight",
SquareBlue2DNormal,
SquareBlue2DHover,
SquareBlue2DActive,
(style) => { style.alignment = TextAnchor.MiddleRight; });
#endregion
#region 黄色按钮-方形
public static GUIStyle BtnYellow => CreateColorSquareButtonStyle("Square.BtnYellow",
SquareYellow2DNormal,
SquareYellow2DHover,
SquareYellow2DActive,
(style) => { style.alignment = TextAnchor.MiddleCenter; });
public static GUIStyle BtnYellowLeft => CreateColorSquareButtonStyle("Square.BtnYellowLeft",
SquareYellow2DNormal,
SquareYellow2DHover,
SquareYellow2DActive,
(style) => { style.alignment = TextAnchor.MiddleLeft; });
public static GUIStyle BtnYellowRight => CreateColorSquareButtonStyle("Square.BtnYellowRight",
SquareYellow2DNormal,
SquareYellow2DHover,
SquareYellow2DActive,
(style) => { style.alignment = TextAnchor.MiddleRight; });
#endregion
#region 红色按钮-方形
public static GUIStyle BtnRed => CreateColorSquareButtonStyle("Square.BtnRed",
SquareRed2DNormal,
SquareRed2DHover,
SquareRed2DActive,
(style) => { style.alignment = TextAnchor.MiddleCenter; });
public static GUIStyle BtnRedLeft => CreateColorSquareButtonStyle("Square.BtnRedLeft",
SquareRed2DNormal,
SquareRed2DHover,
SquareRed2DActive,
(style) => { style.alignment = TextAnchor.MiddleLeft; });
public static GUIStyle BtnRedRight => CreateColorSquareButtonStyle("Square.BtnRedRight",
SquareRed2DNormal,
SquareRed2DHover,
SquareRed2DActive,
(style) => { style.alignment = TextAnchor.MiddleRight; });
#endregion
}
private static GUIStyle CreateStyle(string name, string proto, Action onCreate)
{
var style = GetStyle(name);
if (style == null)
{
style = new GUIStyle(proto);
onCreate?.Invoke(style);
_styles.Add(name, style);
}
return style;
}
private static GUIStyle CreateStyle(string name, GUIStyle proto, Action onCreate)
{
var style = GetStyle(name);
if (style == null)
{
style = new GUIStyle(proto);
onCreate?.Invoke(style);
_styles.Add(name, style);
}
return style;
}
private static GUIStyle CreateColorButtonStyle(string name, Texture2D normal, Texture2D hover,
Texture2D active, Action onCreate)
{
return CreateStyle(name, "button", (style) =>
{
style.border = new RectOffset(6, 6, 6, 6);
style.margin = new RectOffset(2, 2, 2, 2);
style.padding = new RectOffset(4, 4, 4, 4);
style.overflow = new RectOffset(0, 0, 0, 0);
style.alignment = TextAnchor.MiddleCenter;
style.normal.background = normal;
style.normal.scaledBackgrounds = Array.Empty();
style.hover.background = hover;
style.hover.scaledBackgrounds = Array.Empty();
style.focused.background = active;
style.focused.scaledBackgrounds = Array.Empty();
style.active.background = active;
style.active.scaledBackgrounds = Array.Empty();
style.onNormal.background = normal;
style.onNormal.scaledBackgrounds = Array.Empty();
style.onHover.background = hover;
style.onHover.scaledBackgrounds = Array.Empty();
style.onActive.background = active;
style.onActive.scaledBackgrounds = Array.Empty();
style.onFocused.background = active;
style.onFocused.scaledBackgrounds = Array.Empty();
onCreate?.Invoke(style);
});
}
//方形按钮
private static GUIStyle CreateColorSquareButtonStyle(string name, Texture2D normal, Texture2D hover,
Texture2D active, Action onCreate)
{
return CreateStyle(name, "button", (style) =>
{
style.border = new RectOffset(2, 2, 2, 2);
style.margin = new RectOffset(2, 2, 2, 2);
style.padding = new RectOffset(4, 4, 4, 4);
style.overflow = new RectOffset(0, 0, 0, 0);
style.alignment = TextAnchor.MiddleCenter;
style.normal.background = normal;
style.normal.scaledBackgrounds = Array.Empty();
style.hover.background = hover;
style.hover.scaledBackgrounds = Array.Empty();
style.focused.background = active;
style.focused.scaledBackgrounds = Array.Empty();
style.active.background = active;
style.active.scaledBackgrounds = Array.Empty();
style.onNormal.background = normal;
style.onNormal.scaledBackgrounds = Array.Empty();
style.onHover.background = hover;
style.onHover.scaledBackgrounds = Array.Empty();
style.onActive.background = active;
style.onActive.scaledBackgrounds = Array.Empty();
style.onFocused.background = active;
style.onFocused.scaledBackgrounds = Array.Empty();
onCreate?.Invoke(style);
});
}
private static Texture2D LoadIcon2D(string name)
{
var tex = Get2D(name);
if (tex == null)
{
var path = $"{RootPath}/Texture/icon/{name}";
tex = AssetDatabase.LoadAssetAtPath(path);
_texture2Ds.Add(name, tex);
}
return tex;
}
private static Texture2D LoadTheme2D(string name)
{
var tex = Get2D(name);
if (tex == null)
{
var path = EditorGUIUtility.isProSkin
? $"{RootPath}/Texture/dark/{name}"
: $"{RootPath}/Texture/light/{name}";
tex = AssetDatabase.LoadAssetAtPath(path);
_texture2Ds.Add(name, tex);
}
return tex;
}
}
}