using System; using System.Collections.Generic; using System.Linq; using NitroxModel.Helper; using UnityEngine; namespace NitroxClient.Unity.Helper { public static class GUISkinUtils { private static Dictionary guiSkins = new Dictionary(); public static GUISkin CreateDerived(GUISkin baseSkin = null, string name = null) { if (baseSkin == null) { GUISkin prevSkin = GUI.skin; GUI.skin = null; baseSkin = GUI.skin; GUI.skin = prevSkin; } GUISkin copy = ScriptableObject.CreateInstance(); copy.name = name ?? Guid.NewGuid().ToString(); copy.box = new GUIStyle(baseSkin.box); copy.button = new GUIStyle(baseSkin.button); copy.label = new GUIStyle(baseSkin.label); copy.scrollView = new GUIStyle(baseSkin.scrollView); copy.textArea = new GUIStyle(baseSkin.textArea); copy.textField = new GUIStyle(baseSkin.textField); copy.toggle = new GUIStyle(baseSkin.toggle); copy.window = new GUIStyle(baseSkin.window); copy.horizontalScrollbar = new GUIStyle(baseSkin.horizontalScrollbar); copy.horizontalScrollbarLeftButton = new GUIStyle(baseSkin.horizontalScrollbarLeftButton); copy.horizontalScrollbarRightButton = new GUIStyle(baseSkin.horizontalScrollbarRightButton); copy.horizontalScrollbarThumb = new GUIStyle(baseSkin.horizontalScrollbarThumb); copy.horizontalSlider = new GUIStyle(baseSkin.horizontalSlider); copy.horizontalSliderThumb = new GUIStyle(baseSkin.horizontalSliderThumb); copy.verticalScrollbar = new GUIStyle(baseSkin.verticalScrollbar); copy.verticalScrollbarDownButton = new GUIStyle(baseSkin.verticalScrollbarDownButton); copy.verticalScrollbarThumb = new GUIStyle(baseSkin.verticalScrollbarThumb); copy.verticalScrollbarUpButton = new GUIStyle(baseSkin.verticalScrollbarUpButton); copy.verticalSlider = new GUIStyle(baseSkin.verticalSlider); copy.verticalSliderThumb = new GUIStyle(baseSkin.verticalSliderThumb); copy.customStyles = baseSkin.customStyles.Select(s => new GUIStyle(s)).ToArray(); copy.hideFlags = baseSkin.hideFlags; copy.settings.cursorColor = baseSkin.settings.cursorColor; copy.settings.cursorFlashSpeed = baseSkin.settings.cursorFlashSpeed; copy.settings.doubleClickSelectsWord = baseSkin.settings.doubleClickSelectsWord; copy.settings.tripleClickSelectsLine = baseSkin.settings.tripleClickSelectsLine; copy.settings.selectionColor = baseSkin.settings.selectionColor; // TODO: Create identical copy of font. copy.font = baseSkin.font; // Gives a giberish font: copy.font = UnityEngine.Object.Instantiate(baseSkin.font); return copy; } public static GUISkin RegisterDerived(string name, GUISkin baseSkin = null) { Validate.NotNull(name); Validate.IsFalse(guiSkins.ContainsKey(name), $"Name of new GUISkin already exists."); GUISkin newSkin = CreateDerived(baseSkin, name); guiSkins.Add(name, newSkin); return newSkin; } /// /// Creates a new default Unity skin if there are no existing skins with the given . /// /// Name for the new skin. /// Optional skin initializer. /// Optional base skin to copy from. /// New or cached skin. public static GUISkin RegisterDerivedOnce(string name, Action skinInitializer = null, GUISkin baseSkin = null) { if (!guiSkins.ContainsKey(name)) { GUISkin newSkin = RegisterDerived(name, baseSkin); skinInitializer?.Invoke(newSkin); } return guiSkins[name]; } /// /// Switches the active skin until after the action. /// /// Skin to switch to. /// Render function to run. public static void RenderWithSkin(GUISkin skin, Action render) { Validate.NotNull(render); GUISkin prevSkin = GUI.skin; GUI.skin = skin; render(); GUI.skin = prevSkin; } /// /// Adds or sets a custom style to the skin. /// /// /// Custom skins can be used by passing the "nameofskin" to the parameter of and similar. /// /// Skin to add a custom style to. /// Name of the new custom style. /// Style to base your custom style on. /// Function that changes the custom style to your liking. public static void SetCustomStyle(this GUISkin skin, string name, GUIStyle baseStyle, Action modify) { GUIStyle style = new GUIStyle(baseStyle); style.name = name; modify(style); int index = Array.FindIndex(skin.customStyles, item => item.name == style.name); if (index >= 0) { skin.customStyles[index] = style; } else { // Increase array size and add style. List styles = new List(skin.customStyles) { style }; skin.customStyles = styles.ToArray(); } } } }