using System; using System.Collections.Generic; using System.IO; using log4net; using UnityEditor; using UnityEngine; namespace P4Connect { public static class Skin { const float FoldOutLineSize = 20.0f; const int FoldOutFontSize = 16; public static GUIStyle FatButtonStyle; public static GUIStyle RButtonStyle; public static GUIStyle GButtonStyle; public static GUIStyle YButtonStyle; public static GUIStyle BlinkButtonStyle; public static GUIStyle BigButtonStyle; public static GUIStyle BigRadioStyle; public static GUIStyle ToolBarStyle; public static GUIStyle ToolBarLabelStyle; public static GUIStyle ToolBarButtonStyle; public static GUIStyle HeaderStyle; public static GUIStyle HeaderButton; public static GUIStyle ToggleStyle; public static GUIStyle NormalStyle; public static GUIStyle FoldOutStyle; public static GUIStyle FoldOutEntryStyle; public static Texture2D ToolbarTexture; public static Texture2D HeaderTexture; public static Texture2D NormalTexture; public static GUIContent RefreshIcon; public static Font FabricaFont; public static Font DefaultFont; private static string _defaultFontPath = "Assets/P4Connect/Editor/Fabrica.otf"; private static readonly ILog log = LogManager.GetLogger(typeof(Skin)); private static string _skinName = "P4Connect.skin"; //private static GUISkin _p4Skin; public static GUISkin P4Skin; // Access the P4Skin, if the asset can't be found, construct it. // Only called once when initializing styles. public static GUISkin GetSkin() { if (P4Skin != null) return P4Skin; string skinPath = Path.Combine(Utils.GetEditorAssetRelativeDirectory(), _skinName); P4Skin = AssetDatabase.LoadAssetAtPath(skinPath, typeof (GUISkin)) as GUISkin; if (P4Skin != null) { log.Debug("Found Asset Skin - Should Skip"); // Comment out the following line if you are developing a new skin // return (P4Skin); } // We couldn't find one, so we construct it ourselves log.Debug("Creating Skin"); try { var defaultSkin = EditorGUIUtility.GetBuiltinSkin(EditorGUIUtility.isProSkin ? EditorSkin.Scene : EditorSkin.Inspector); P4Skin = UnityEngine.Object.Instantiate(defaultSkin); if (P4Skin == null) { log.Debug("Could not create Skin, using default"); return defaultSkin; } DefaultFont = AssetDatabase.LoadAssetAtPath(_defaultFontPath); if (DefaultFont == null) { log.Debug("Unable to initialize font: " + _defaultFontPath); } } catch (Exception ex) { log.Debug("Exception: " + ex.Message); } ToolbarTexture = new Texture2D(1, 1); ToolbarTexture.SetPixel(0,0,Color.magenta); ToolbarTexture.Apply(); HeaderTexture = new Texture2D(1, 1); HeaderTexture.SetPixel(0, 0, Color.cyan); HeaderTexture.Apply(); NormalTexture = new Texture2D(1, 1); NormalTexture.SetPixel(0, 0, Color.white); NormalTexture.Apply(); BigButtonStyle = new GUIStyle("Button") { name = "bigButton", alignment = TextAnchor.MiddleCenter, fontSize = 18, fixedHeight = 100, fixedWidth = 100, }; BigRadioStyle = new GUIStyle("Button") { name = "bigRadio", alignment = TextAnchor.MiddleCenter, fontSize = 14, fixedHeight = 100, fixedWidth = 100, }; FatButtonStyle = new GUIStyle("Button") { name = "fatButton", alignment = TextAnchor.MiddleCenter, fontSize = 16, fixedHeight = 50, }; GButtonStyle = new GUIStyle(FatButtonStyle) { name = "greenFatButton", normal = new GUIStyleState() { background = Icons.GetIcon("button-green.png") as Texture2D, textColor = Color.black }, }; RButtonStyle = new GUIStyle(FatButtonStyle) { name = "redFatButton", normal = new GUIStyleState() { background = Icons.GetIcon("button-red.png") as Texture2D, textColor = Color.white }, }; YButtonStyle = new GUIStyle(FatButtonStyle) { name = "yellowFatButton", normal = new GUIStyleState() { background = Icons.GetIcon("button-yellow.png") as Texture2D, textColor = Color.black }, }; ToolBarStyle = new GUIStyle("toolbar") { name = "toolBar", font = DefaultFont, fontSize = 14, fixedHeight = 34 }; ToolBarLabelStyle = new GUIStyle("toolbarbutton") { name = "toolBarLabel", normal = new GUIStyleState() { textColor = Color.black, background = ToolbarTexture}, alignment = TextAnchor.MiddleLeft, font = DefaultFont, fontSize = 14, fixedHeight = 30 }; ToolBarButtonStyle = new GUIStyle("toolbarbutton") { name = "toolBarButton", normal = new GUIStyleState() { textColor = Color.black, background = Icons.GetIcon("box-green.png") as Texture2D }, onNormal = new GUIStyleState() { textColor = Color.black, background = Icons.GetIcon("box-green.png") as Texture2D }, onFocused = new GUIStyleState() { textColor = Color.black, background = Icons.GetIcon("box-green.png") as Texture2D }, alignment = TextAnchor.MiddleCenter, font = DefaultFont, fontSize = 14, fixedHeight = 30, margin = new RectOffset(4, 4, 1, 1) }; HeaderStyle = new GUIStyle("miniButtonMid") { name = "headerStyle", normal = new GUIStyleState() {textColor = Color.black, background = HeaderTexture}, padding = new RectOffset(0, 0, 1, 1), margin = new RectOffset(0, 0, 0, 0), overflow = new RectOffset(0, 0, 0, 0), font = DefaultFont, fontSize = 10 }; HeaderButton = new GUIStyle("miniButtonMid") { name = "headerButtons", normal = new GUIStyleState() { textColor = Color.black, background = HeaderTexture }, alignment = TextAnchor.MiddleLeft, padding = new RectOffset(0, 0, 0, 2), margin = new RectOffset(0, 0, 0, 0), overflow = new RectOffset(0, 0, 0, 2), font = DefaultFont, fontSize = 10 }; FoldOutStyle = new GUIStyle("Foldout") { name = "foldOutStyle", fixedHeight = FoldOutLineSize, fontSize = FoldOutFontSize, font = FabricaFont }; FoldOutEntryStyle = new GUIStyle("textField") { name = "foldOutEntryStyle", fixedHeight = FoldOutLineSize, fontSize = FoldOutFontSize, font = FabricaFont }; ToggleStyle = new GUIStyle("Toggle") { name = "toggleStyle", margin = new RectOffset(0, 0, 0, 0) }; NormalStyle = new GUIStyle("textField") { name = "normalStyle", //normal = new GUIStyleState() { textColor = Color.black, background = NormalTexture }, fontSize = 10, font = DefaultFont }; // Add some custom types to the default set. var myCustomStyles = new List(P4Skin.customStyles) { BigButtonStyle, BigRadioStyle, FatButtonStyle, GButtonStyle, RButtonStyle, YButtonStyle, ToolBarLabelStyle, ToolBarButtonStyle, ToolBarStyle, HeaderStyle, HeaderButton, ToggleStyle, NormalStyle, FoldOutStyle, FoldOutEntryStyle, }; P4Skin.customStyles = myCustomStyles.ToArray(); BlinkButtonStyle = FatButtonStyle; // Write this skin out to P4Skin for future use. log.Debug("Writing Skin"); try { AssetDatabase.StartAssetEditing(); AssetDatabase.CreateAsset(P4Skin, skinPath); AssetDatabase.StopAssetEditing(); EditorUtility.SetDirty(P4Skin); } catch (Exception ex) { Debug.Log(" Exception while writing skin: " + ex.Message); } return P4Skin; } // Static initializer to be called ONCE from OnGUI() after initialization public static void InitializeSkin() { log.Debug("InitializeSkin()"); P4Skin = GetSkin(); // Set up the global style variables if (BigButtonStyle == null) { BigButtonStyle = P4Skin.GetStyle("bigButton"); BigRadioStyle = P4Skin.GetStyle("bigRadio"); FatButtonStyle = P4Skin.GetStyle("fatButton"); GButtonStyle = P4Skin.GetStyle("greenFatButton"); RButtonStyle = P4Skin.GetStyle("redFatButton"); YButtonStyle = P4Skin.GetStyle("yellowFatButton"); ToolBarStyle = P4Skin.GetStyle("toolBarStyle"); ToolBarLabelStyle = P4Skin.GetStyle("toolBarLabelStyle"); ToolBarButtonStyle = P4Skin.GetStyle("toolBarButtonStyle"); HeaderStyle = P4Skin.GetStyle("headerStyle"); HeaderButton = P4Skin.GetStyle("headerButtons"); ToggleStyle = P4Skin.GetStyle("toggleStyle"); NormalStyle = P4Skin.GetStyle("normalStyle"); FoldOutStyle = P4Skin.GetStyle("foldOutStyle"); FoldOutEntryStyle = P4Skin.GetStyle("foldOutEntryStyle"); } log.Debug("InitializeSkin() Complete"); } private static int _blinkCount = 0; private static int _blinkMax = 6; private static bool _blinkOn = true; public static void OnInspectorUpdate(EditorWindow window) { window.Repaint(); // 10 x a second _blinkCount++; if (_blinkCount >= _blinkMax) // approx every second { if (_blinkOn) { BlinkButtonStyle = GButtonStyle; } else { BlinkButtonStyle = YButtonStyle; } _blinkOn = !_blinkOn; _blinkCount = 0; } } } }