| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379 |
- #if UNITY_EDITOR
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using TMPro;
- using DamageNumbersPro;
- using UnityEngine.Rendering;
- using UnityEditor.SceneManagement;
- namespace DamageNumbersPro.Internal {
- public static class DNPEditorInternal
- {
- // Public
- public static int currentTab;
- public static bool repaintViews;
- public static HashSet<string> hints;
- public static DamageNumber[] damageNumbers;
- public static TextMeshPro[] textMeshPros;
- // Private
- static Transform[] meshAs;
- static Transform[] meshBs;
- // Presets
- static Dictionary<string,DNPPreset[]> allPresets;
- // GUI Resources
- public static GUIStyle labelStyle;
- public static GUIStyle buttonStyle;
- public static GUIStyle rightTextStyle;
- public static GUIStyle centerTextStyle;
- public static GUIStyle bottomRightTextStyle;
- public static GUIStyle topRightTextStyle;
- static GUIStyle whiteBoxStyle;
- static Texture2D whiteBoxTexture;
- static Texture bannerTexture;
- public static float currentWidth;
- // External Editor
- static int currentEditor;
- static bool cleanEditor;
- static Material[] currentMaterials;
- static MaterialEditor materialEditor;
- static Editor textMeshProEditor;
- static bool generatedGUIStyles;
- public static void PrepareInspector(DamageNumberEditor damageNumberEditor)
- {
- // Clean Editors
- cleanEditor = true;
- generatedGUIStyles = false;
- // Get Damage Numbers
- damageNumbers = new DamageNumber[damageNumberEditor.targets.Length];
- for (int i = 0; i < damageNumberEditor.targets.Length; i++)
- {
- damageNumbers[i] = (DamageNumber)damageNumberEditor.targets[i];
- }
- // Type
- bool isMesh = damageNumbers[0].IsMesh();
- // Get Presets
- allPresets = new Dictionary<string, DNPPreset[]>();
- allPresets.Add("Style", Resources.LoadAll<DNPPreset>("DNP/Style"));
- allPresets.Add("Fade In", Resources.LoadAll<DNPPreset>("DNP/Fade In"));
- allPresets.Add("Fade Out", Resources.LoadAll<DNPPreset>("DNP/Fade Out"));
- allPresets.Add("Behaviour", Resources.LoadAll<DNPPreset>("DNP/Behaviour"));
- // Get Structural Objects
- textMeshPros = new TextMeshPro[damageNumbers.Length];
- meshAs = new Transform[damageNumbers.Length];
- meshBs = new Transform[damageNumbers.Length];
- if(isMesh)
- {
- for (int n = 0; n < damageNumbers.Length; n++)
- {
- Transform dnTransform = damageNumbers[n].transform;
- // TMP
- Transform textMeshProTransform = dnTransform.Find("TMP");
- if (textMeshProTransform != null)
- {
- textMeshPros[n] = textMeshProTransform.GetComponent<TextMeshPro>();
- }
- // MeshA
- meshAs[n] = dnTransform.Find("MeshA");
- // MeshB
- meshBs[n] = dnTransform.Find("MeshB");
- }
- }
- // Get Banner Texture
- if (damageNumbers != null && damageNumbers.Length > 0 && damageNumbers[0] != null)
- {
- bannerTexture = Resources.Load<Texture>("DNP/Textures/DNP_Banner");
- }
- // Create White Box Texture
- Color[] pixels = new Color[4];
- for (int n = 0; n < pixels.Length; n++)
- {
- pixels[n] = Color.white;
- }
- whiteBoxTexture = new Texture2D(2, 2);
- whiteBoxTexture.SetPixels(pixels);
- whiteBoxTexture.Apply();
- // Close Hints
- hints = new HashSet<string>();
- }
- public static void OnInspectorGUI(DamageNumberEditor damageNumberEditor)
- {
- // Prepare Inspector
- if (damageNumbers == null || damageNumbers.Length == 0 || damageNumbers[0] == null)
- {
- PrepareInspector(damageNumberEditor);
- }
- // Prepare Styles
- PrepareStyles();
- // Repaint
- if (repaintViews)
- {
- repaintViews = false;
- UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
- }
- }
- #region Inspector Top
- public static void DrawInspectorTop(bool isMesh)
- {
- // Banner
- Rect bannerRect = DrawBanner(isMesh);
- // Tabs
- DrawTabs(bannerRect);
- // Distance
- EditorGUILayout.Space(42);
- }
- static Rect DrawBanner(bool isMesh)
- {
- Rect bannerRect = default;
- if (bannerTexture != null)
- {
- // Banner
- EditorGUILayout.BeginVertical();
- GUILayout.Label("", GUILayout.Height(100 + 0.38f * Mathf.Min(0,EditorGUIUtility.currentViewWidth - 430)));
- bannerRect = GUILayoutUtility.GetLastRect();
- float ratio = (bannerRect.width / bannerRect.height) / 8f;
- Rect clipRect = new Rect((1 - ratio) * 0.5f, 0, ratio, 1);
- GUI.DrawTextureWithTexCoords(bannerRect, bannerTexture, clipRect, true);
- EditorGUILayout.EndVertical();
- // Version Info
- Rect infoRect = new Rect(bannerRect);
- infoRect.width -= 3;
- infoRect.y += 4;
- infoRect.height -= 8;
- GUI.Label(infoRect, "<color=#FFFFFF><b><size=10>v</size>" + DamageNumberEditor.version + " </b></color>", bottomRightTextStyle);
- // Type Info
- GUI.Label(infoRect, "<color=#FFFFFF><b>" + (isMesh ? "Mesh" : "GUI") + "<size=10> </size></b></color>", topRightTextStyle);
- // Button
- Rect documentationRect = GUILayoutUtility.GetLastRect();
- documentationRect.x += 9;
- documentationRect.y += 9;
- documentationRect.width = 56;
- documentationRect.height = 18;
- if (GUI.Button(documentationRect, "<b>Manual</b>", buttonStyle))
- {
- Application.OpenURL("https://ekincantas.com/damage-numbers-pro/");
- }
- documentationRect.y += 21;
- if (GUI.Button(documentationRect, "<b>Discord</b>", buttonStyle))
- {
- Application.OpenURL("https://discord.gg/nWbRkN8Zxr");
- }
- // Box
- BoxLastRect();
- EditorGUILayout.Space();
- // Calculate Width for GUI Scaling
- float newWidth = GUILayoutUtility.GetLastRect().width;
- if(newWidth > 50)
- {
- currentWidth = newWidth;
- }
- }
- return bannerRect;
- }
- static void DrawTabs(Rect lastRect)
- {
- // Position
- lastRect.y += lastRect.height - 3;
- lastRect.height = 29;
- // Row 1
- int lastTab = currentTab;
- currentTab = GUI.Toolbar(lastRect, currentTab, new string[] { "Main", "Text", "Fade In", "Fade Out" }, buttonStyle);
- lastRect.height -= 1;
- BoxRect(lastRect);
- string rotAndScaleText = "Rotation & Size";
- if(currentWidth < 388)
- {
- rotAndScaleText = "<size=11>Rotation & Size</size>";
- if(currentWidth < 356)
- {
- rotAndScaleText = "<size=10>Rotation & Size</size>";
- if (currentWidth < 324)
- {
- rotAndScaleText = "<size=9>Rotation & Size</size>";
-
- if (currentWidth < 303)
- {
- rotAndScaleText = "<size=8>Rotation & Size</size>";
- if (currentWidth < 276)
- {
- rotAndScaleText = "<size=7>Rotation & Size</size>";
- }
- }
- }
- }
- }
- string spamText = "Spam Control";
- if(currentWidth < 340)
- {
- spamText = "<size=11>Spam Control</size>";
- if (currentWidth < 324)
- {
- spamText = "<size=10>Spam Control</size>";
- if (currentWidth < 304)
- {
- spamText = "<size=9>Spam Control</size>";
- if (currentWidth < 367)
- {
- spamText = "<size=8>Spam Control</size>";
- }
- }
- }
- }
- string performanceText = "Performance";
- if(currentWidth < 336)
- {
- performanceText = "<size=11>Performance</size>";
- if (currentWidth < 310)
- {
- performanceText = "<size=10>Performance</size>";
- if (currentWidth < 292)
- {
- performanceText = "<size=9>Performance</size>";
- }
- }
- }
- string movementText = "Movement";
- if(currentWidth < 293)
- {
- movementText = "<size=11>Movement</size>";
- if (currentWidth < 282)
- {
- movementText = "<size=10>Movement</size>";
- if (currentWidth < 263)
- {
- movementText = "<size=9>Movement</size>";
- }
- }
- }
- // Row 2
- lastRect.y += lastRect.height - 3;
- lastRect.height = 22;
- currentTab = 4 + GUI.Toolbar(lastRect, currentTab - 4, new string[] { movementText, rotAndScaleText, spamText, performanceText }, buttonStyle);
- lastRect.height += 2;
- // On Switch
- if (currentTab != lastTab)
- {
- EditorGUIUtility.keyboardControl = EditorGUIUtility.hotControl = 0;
- hints = new HashSet<string>(); // Clear Hints
- }
- // Box
- BoxRect(lastRect, true, false);
- }
- #endregion
- #region Special Sections
- public static void FinalInformation()
- {
- Color finalInformationColor = new Color(0.93f, 0.95f, 1);
- GUIStyle linkStyle = new GUIStyle(labelStyle);
- linkStyle.normal.textColor = linkStyle.focused.textColor = linkStyle.hover.textColor = EditorGUIUtility.isProSkin ? new Color(0.8f, 0.9f, 1f, 1) : new Color(0.1f, 0.2f, 0.4f, 1);
- linkStyle.active.textColor = EditorGUIUtility.isProSkin ? new Color(0.6f, 0.8f, 1f, 1) : new Color(0.15f, 0.4f, 0.6f, 1);
- EditorGUILayout.Space(2);
- StartBox(finalInformationColor);
- EditorGUILayout.BeginVertical();
- GUI.color = new Color(1, 1, 1f, 0.75f);
- if(currentWidth < 285)
- {
- if(currentWidth < 265)
- {
- Label("<size=10><b>Thank you for using Damage Numbers Pro.</b></size>");
- }
- else
- {
- Label("<size=11><b>Thank you for using Damage Numbers Pro.</b></size>");
- }
- }
- else
- {
- Label("<b>Thank you for using Damage Numbers Pro.</b>");
- }
- Label("<b>Contact me if you need any help.</b>");
- EditorGUILayout.Space(8);
- EditorGUILayout.BeginHorizontal();
- GUI.color = new Color(1, 1, 1, 1);
- // Link Shrinking
- string docLink = "https://ekincantas.com/damage-numbers-pro/";
- if(currentWidth < 420f)
- {
- docLink = "https://ekincantas.com/damage-numbers...";
- if (currentWidth < 398)
- {
- docLink = "https://ekincantas.com/damage...";
- if (currentWidth < 340)
- {
- docLink = "https://ekincantas.com/...";
- if(currentWidth < 293)
- {
- docLink = "Open Link";
- }
- }
- }
- }
- string discordLink = "https://discord.gg/nWbRkN8Zxr";
- if (currentWidth < 335f)
- {
- discordLink = "https://discord.gg/...";
- if (currentWidth < 264)
- {
- discordLink = "Open Link";
- }
- }
- EditorGUILayout.LabelField("<b>Documentation:</b>", labelStyle, GUILayout.Width(100));
- if (GUILayout.Button("<b>" + docLink + "</b>", linkStyle))
- {
- Application.OpenURL("https://ekincantas.com/damage-numbers-pro/");
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.BeginHorizontal();
- GUI.color = new Color(1, 1, 1, 1);
- EditorGUILayout.LabelField("<b>Discord:</b>", labelStyle, GUILayout.Width(100));
- if (GUILayout.Button("<b>" + discordLink + "</b>", linkStyle))
- {
- Application.OpenURL("https://discord.gg/nWbRkN8Zxr");
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.BeginHorizontal();
- GUI.color = new Color(1, 1, 1, 1);
- string emailPrefix = "<b>Email:</b>";
- if(currentWidth < 259)
- {
- emailPrefix = "<size=11><b>Email:</b></size>";
- if(currentWidth < 256)
- {
- emailPrefix = "<size=10><b>Email:</b></size>";
- if (currentWidth < 254)
- {
- emailPrefix = "<size=9><b>Email:</b></size>";
- }
- }
- }
- EditorGUILayout.LabelField(emailPrefix, labelStyle, GUILayout.Width(100 + Mathf.Min(0,(currentWidth - 320))));
- EditorGUILayout.SelectableLabel("<b>ekincantascontact@gmail.com</b>", linkStyle, GUILayout.Height(16));
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.EndVertical();
- GUI.backgroundColor = Color.white;
- CloseBox(finalInformationColor);
- EditorGUILayout.Space(5);
- }
- public static void Externalnspectors(bool isMesh, Object target)
- {
- EditorGUILayout.Space(2);
- Lines();
- Color externalInspectorColor = new Color(0.93f, 0.95f, 1);
- EditorGUILayout.Space(2);
- StartBox(externalInspectorColor);
- GUI.backgroundColor = externalInspectorColor;
- EditorGUILayout.BeginVertical();
- bool editingPrefabPreview = EditingPrefabPreview(target);
- if (editingPrefabPreview)
- {
- GUI.color = new Color(1, 1, 1f, 0.75f);
- ScalingLabel("<b>Open</b> the prefab to access the <b>presets</b>, <b>material</b> and <b>text mesh pro</b> tabs.", 440);
- OpenPrefabButton(target);
- GUI.backgroundColor = Color.white;
- EditorGUILayout.EndVertical();
- CloseBox(externalInspectorColor);
- return;
- }
- int previousEditor = currentEditor;
- if (cleanEditor)
- {
- previousEditor = -1; // Force Clean
- cleanEditor = false;
- }
- // Tab Names
- string textMeshProTab = "TextMeshPro";
- string materialTab = "Material";
- if (damageNumbers != null && damageNumbers.Length > 1)
- {
- textMeshProTab = damageNumbers.Length + " TextMeshPros";
- materialTab = "Materials";
- }
- if(currentWidth < 266)
- {
- textMeshProTab = "<size=11>" + textMeshProTab + "</size>";
- }
- // Tab Rect
- GUILayout.BeginVertical();
- GUILayout.Space(13);
- GUILayout.EndVertical();
- Rect tabRect = GUILayoutUtility.GetLastRect();
- tabRect.x -= 5;
- tabRect.width += 12;
- tabRect.y -= 5;
- tabRect.height = 25;
- // Draw Tab
- currentEditor = GUI.Toolbar(tabRect, currentEditor, new string[] { "Presets", materialTab , textMeshProTab });
- // Box Tab Rect
- tabRect.y += 23;
- tabRect.height = 3;
- tabRect.width -= 3;
- if (currentEditor == 1)
- {
- EditorGUILayout.Space(14);
- // Material
- if (previousEditor != 1)
- {
- ResetMaterials();
- }
- if (materialEditor != null)
- {
- materialEditor.DrawHeader();
- materialEditor.OnInspectorGUI();
- }
- }
- else if (currentEditor == 2)
- {
- EditorGUILayout.Space(14);
- if (damageNumbers.Length > 1)
- {
- GUI.color = new Color(1, 1, 1, 0.7f);
- Label("The fancy inspector does not work for <b>multiple</b> damage numbers.");
- Label("You can also <b>manually select</b> the text-mesh-pro components.");
- Label("- Sorry for the inconvenience.");
- GUI.color = Color.white;
- EditorGUILayout.Space(8);
- }
- if (isMesh)
- {
- // Text Mesh Pro
- if (previousEditor != 2)
- {
- if (textMeshProEditor != null)
- {
- Object.DestroyImmediate(textMeshProEditor);
- }
- textMeshProEditor = Editor.CreateEditor(textMeshPros, null);
- }
- // Editor
- if (textMeshProEditor != null)
- {
- textMeshProEditor.DrawHeader();
- if (textMeshPros.Length > 1)
- {
- textMeshProEditor.DrawDefaultInspector();
- }
- else
- {
- textMeshProEditor.OnInspectorGUI();
- }
- }
- }
- else
- {
- // Text Mesh Pro
- if (previousEditor != 2)
- {
- if (textMeshProEditor != null)
- {
- Object.DestroyImmediate(textMeshProEditor);
- }
- TextMeshProUGUI[] tmps = new TextMeshProUGUI[damageNumbers.Length];
- for(int i = 0; i < damageNumbers.Length; i++)
- {
- TMP_Text tmpText = damageNumbers[i].GetTextMesh();
- if(tmpText.GetType() == typeof(TextMeshProUGUI))
- {
- tmps[i] = (TextMeshProUGUI)tmpText;
- }
- else
- {
- return;
- }
- }
- textMeshProEditor = Editor.CreateEditor(tmps, null);
- }
- // Editor
- if (textMeshProEditor != null)
- {
- textMeshProEditor.DrawHeader();
- if (textMeshPros.Length > 1)
- {
- textMeshProEditor.DrawDefaultInspector();
- }
- else
- {
- textMeshProEditor.OnInspectorGUI();
- }
- // Match both TMPs
- foreach(DamageNumber dn in damageNumbers)
- {
- TMP_Text[] tmps = dn.GetTextMeshs();
- if(tmps.Length > 1)
- {
- EditorUtility.CopySerialized(((TextMeshProUGUI)tmps[0]), ((TextMeshProUGUI)tmps[1]));
- }
- }
- }
- }
- }
- else
- {
- EditorGUILayout.Space(14);
- ShowPresets(isMesh);
- }
- EditorGUILayout.EndVertical();
- GUI.backgroundColor = Color.white;
- CloseBox(externalInspectorColor);
- // Line under Tabs
- GUI.color = externalInspectorColor;
- DrawOutlineBox(tabRect);
- tabRect.width += 3;
- DrawBox(tabRect);
- GUI.color = Color.white;
- }
- public static void ResetMaterials()
- {
- List<Material> allMaterials = new List<Material>();
- foreach (DamageNumber dn in damageNumbers)
- {
- dn.GetReferencesIfNecessary();
- foreach (Material mat in dn.GetSharedMaterials())
- {
- if (allMaterials.Contains(mat) == false)
- {
- allMaterials.Add(mat);
- }
- }
- }
- currentMaterials = new Material[allMaterials.Count];
- for (int n = 0; n < allMaterials.Count; n++)
- {
- currentMaterials[n] = allMaterials[n];
- }
- if (materialEditor != null)
- {
- Object.DestroyImmediate(materialEditor);
- }
- materialEditor = (MaterialEditor)Editor.CreateEditor(currentMaterials);
- }
- static void ShowPresets(bool isMesh)
- {
- PresetCategory("Style", isMesh);
- EditorGUILayout.Space(12);
- PresetCategory("Fade In", isMesh);
- EditorGUILayout.Space(12);
- PresetCategory("Fade Out", isMesh);
- EditorGUILayout.Space(12);
- PresetCategory("Behaviour", isMesh);
- }
- static void PresetCategory(string category, bool isMesh)
- {
- GUI.color = new Color(1, 1, 1, 0.7f);
- EditorGUILayout.LabelField("<size=14><b> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " + category + " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </b></size>", centerTextStyle);
- GUI.color = Color.white;
- DNPPreset[] presets = allPresets[category];
- if(presets == null || presets.Length == 0)
- {
- GUI.color = new Color(1, 1, 1, 0.7f);
- Label("Presets could not be loaded.");
- Label("Maybe you deleted or moved a folder ?");
- GUI.color = Color.white;
- return;
- }
- int buttonsPerRow = 4;
- if(EditorGUIUtility.currentViewWidth < 440)
- {
- buttonsPerRow = 3;
- if(EditorGUIUtility.currentViewWidth < 375)
- {
- buttonsPerRow = 2;
- }
- }
- int currentCount = 0;
- foreach (DNPPreset preset in presets)
- {
- // Check Applied
- bool isApplied = true;
- foreach (DamageNumber dn in damageNumbers)
- {
- if(!preset.IsApplied(dn))
- {
- isApplied = false;
- break;
- }
- }
- if(isApplied)
- {
- GUI.enabled = false;
- }
- // Increase Count
- currentCount++;
- if (currentCount % buttonsPerRow == 1)
- {
- if(currentCount > 1)
- {
- EditorGUILayout.EndHorizontal();
- }
- EditorGUILayout.BeginHorizontal();
- GUILayout.Space(5);
- }
- // Apply Button
- if (GUILayout.Button(preset.name))
- {
- if(isMesh)
- {
- Object[] undoObjects = new Object[damageNumbers.Length + textMeshPros.Length];
- for (int i = 0; i < damageNumbers.Length; i++)
- {
- undoObjects[i] = damageNumbers[i];
- }
- for (int i = 0; i < textMeshPros.Length; i++)
- {
- undoObjects[i + damageNumbers.Length] = textMeshPros[i];
- }
- Undo.RecordObjects(undoObjects, "Applied the [" + preset.name + "] " + category + " Preset.");
- }
- else
- {
- Object[] undoObjects = new Object[damageNumbers.Length * 3];
- for (int i = 0; i < damageNumbers.Length; i += 3)
- {
- undoObjects[i] = damageNumbers[i];
- undoObjects[i + 1] = damageNumbers[i].GetTextMeshs()[0];
- undoObjects[i + 2] = damageNumbers[i].GetTextMeshs()[1];
- }
- Undo.RecordObjects(undoObjects, "Applied the [" + preset.name + "] " + category + " Preset.");
- }
- foreach(DamageNumber dn in damageNumbers)
- {
- preset.Apply(dn);
- }
- foreach(DamageNumber dn in damageNumbers)
- {
- dn.UpdateText();
- }
- }
- // Reenable GUI
- GUI.enabled = true;
- }
- GUI.enabled = false;
- GUI.color = new Color(0, 0, 0, 0);
- int modulo = currentCount % buttonsPerRow;
- if(modulo > 0)
- {
- for (int n = 0; n < buttonsPerRow - modulo; n++)
- {
- GUILayout.Button("- - - - -");
- }
- }
- GUI.color = Color.white;
- GUI.enabled = true;
- EditorGUILayout.EndHorizontal();
- }
- #endregion
- #region Structure
- public static bool CheckStructure(DamageNumberEditor damageNumberEditor)
- {
- // Type
- bool isMesh = damageNumbers[0].IsMesh(); ;
- // Check if structure is flawed
- bool isStructureFlawed = false;
- bool isOutdated = false;
- if(isMesh)
- {
- for (int n = 0; n < damageNumbers.Length; n++)
- {
- DamageNumber dn = damageNumbers[n];
- TextMeshPro tmp = textMeshPros[n];
- Transform meshA = meshAs[n];
- Transform meshB = meshBs[n];
- if (tmp == null || meshA == null || meshB == null)
- {
- isStructureFlawed = true;
- if (dn.transform.Find("TextA") != null)
- {
- isOutdated = true;
- }
- break;
- }
- }
- }
- else
- {
- for (int n = 0; n < damageNumbers.Length; n++)
- {
- DamageNumber dn = damageNumbers[n];
- if (dn.transform.Find("TMPA") == null || dn.transform.Find("TMPB") == null)
- {
- isStructureFlawed = true;
- break;
- }
- }
- }
- // Create button to fix structure
- if (isStructureFlawed)
- {
- // Start Box
- StartBox(new Color(1, 1, 0.8f));
- // Structure Build Button
- GUI.color = new Color(1, 1, 0.8f);
- if (GUILayout.Button(isOutdated ? "Upgrade Structure" : "Build Structure", GUILayout.Width(140)))
- {
- if(isMesh)
- {
- foreach (DamageNumber dn in damageNumbers)
- {
- PrepareMeshStructure(dn.gameObject);
- }
- }
- else
- {
- foreach (DamageNumber dn in damageNumbers)
- {
- PrepareGUIStructure(dn.gameObject);
- }
- }
- PrepareInspector(damageNumberEditor);
- }
- // Text
- GUI.color = new Color(1, 1, 1, 0.7f);
- if (isOutdated)
- {
- EditorGUILayout.LabelField("Version 4.0 has changed the structure of damage numbers.", labelStyle);
- EditorGUILayout.LabelField("Click the button above to <b>upgrade</b> this damage number.", labelStyle);
- }
- else
- {
- EditorGUILayout.LabelField("Important components are missing.", labelStyle);
- EditorGUILayout.LabelField("Click the button above to <b>prepare</b> this damage number.", labelStyle);
- }
- // Close Box
- CloseBox(new Color(1, 1, 0.7f));
- EditorGUILayout.Space();
- return true;
- }
- else
- {
- return false;
- }
- }
- public static void PrepareMeshStructure(GameObject go)
- {
- // Add Sorting Group
- if (go.GetComponent<SortingGroup>() == null)
- {
- go.AddComponent<SortingGroup>().sortingOrder = 1000;
- }
- // Rename TextA to TMP
- Transform textA = go.transform.Find("TextA");
- if (textA != null)
- {
- textA.gameObject.name = "TMP";
- }
- // Destroy TextB
- if (go.transform.Find("TextB"))
- {
- MonoBehaviour.DestroyImmediate(go.transform.Find("TextB").gameObject, true);
- }
- // Create TMP
- if (go.transform.Find("TMP") == null)
- {
- NewTextMesh("TMP", go.transform);
- }
- // Create MeshA
- if (go.transform.Find("MeshA") == null)
- {
- DamageNumber.NewMesh("MeshA", go.transform);
- }
- // Create MeshB
- if (go.transform.Find("MeshB") == null)
- {
- DamageNumber.NewMesh("MeshB", go.transform);
- }
- // Undo
- Undo.RegisterCreatedObjectUndo(go, "Create new Damage Number (Mesh).");
- }
- public static void PrepareGUIStructure(GameObject go)
- {
- // Create TMP
- if (go.transform.Find("TMPA") == null)
- {
- NewTextGUI("TMPA", go.transform);
- }
- if (go.transform.Find("TMPB") == null)
- {
- NewTextGUI("TMPB", go.transform);
- }
- // Add Rect Component
- if(go.GetComponent<RectTransform>() == null)
- {
- go.AddComponent<RectTransform>();
- }
- // Undo
- Undo.RegisterCreatedObjectUndo(go, "Create new Damage Number (GUI).");
- }
- public static GameObject NewTextMesh(string tmName, Transform parent)
- {
- // GameObject
- GameObject newTM = new GameObject();
- newTM.name = tmName;
- // TextMeshPro
- TextMeshPro tmp = newTM.AddComponent<TextMeshPro>();
- tmp.fontSize = 5;
- tmp.verticalAlignment = VerticalAlignmentOptions.Middle;
- tmp.horizontalAlignment = HorizontalAlignmentOptions.Center;
- tmp.text = "1";
- tmp.textWrappingMode = TextWrappingModes.NoWrap;
- // Size Delta
- RectTransform rectTransform = tmp.GetComponent<RectTransform>();
- if (rectTransform != null)
- {
- rectTransform.sizeDelta = new Vector2(4, 2);
- }
- // Transform
- newTM.transform.SetParent(parent, true);
- newTM.transform.localPosition = Vector3.zero;
- newTM.transform.localScale = Vector3.one;
- newTM.transform.localEulerAngles = Vector3.zero;
- return newTM;
- }
- public static GameObject NewTextGUI(string tmName, Transform parent)
- {
- // GameObject
- GameObject newTM = new GameObject();
- newTM.name = tmName;
- // TextMeshPro
- TextMeshProUGUI tmp = newTM.AddComponent<TextMeshProUGUI>();
- tmp.fontSize = 30;
- tmp.verticalAlignment = VerticalAlignmentOptions.Middle;
- tmp.horizontalAlignment = HorizontalAlignmentOptions.Center;
- tmp.text = "1";
- tmp.textWrappingMode = TextWrappingModes.NoWrap;
- tmp.raycastTarget = false;
- // Size Delta
- RectTransform rectTransform = tmp.GetComponent<RectTransform>();
- if (rectTransform != null)
- {
- rectTransform.sizeDelta = new Vector2(4, 2);
- }
- // Transform
- newTM.transform.SetParent(parent, true);
- newTM.transform.localPosition = Vector3.zero;
- newTM.transform.localScale = Vector3.one;
- newTM.transform.localEulerAngles = Vector3.zero;
- return newTM;
- }
- public static void FixTextMeshPro()
- {
- bool destroyedSomething = false;
- foreach (TextMeshPro textMesh in textMeshPros)
- {
- Transform tmp = textMesh.transform;
- tmp.localPosition = Vector3.zero;
- tmp.gameObject.SetActive(true);
- for (int n = 0; n < tmp.childCount; n++)
- {
- DestroyOrDisable(tmp.GetChild(n).gameObject);
- destroyedSomething = true;
- }
- tmp.gameObject.SetActive(false);
- }
-
- foreach(Transform meshA in meshAs)
- {
- for(int n = 0; n < meshA.childCount; n++)
- {
- Transform child = meshA.GetChild(n);
- if(child.GetComponent<MeshRenderer>() != null)
- {
- destroyedSomething = true;
- DestroyOrDisable(child.gameObject);
- }
- }
- }
- foreach (Transform meshB in meshBs)
- {
- for (int n = 0; n < meshB.childCount; n++)
- {
- Transform child = meshB.GetChild(n);
- if (child.GetComponent<MeshRenderer>() != null)
- {
- destroyedSomething = true;
- DestroyOrDisable(child.gameObject);
- }
- }
- }
- if(destroyedSomething)
- {
- foreach(DamageNumber dn in damageNumbers)
- {
- dn.GetReferences();
- }
- }
- }
- private static void DestroyOrDisable(GameObject go)
- {
- if (PrefabUtility.IsPartOfNonAssetPrefabInstance(go))
- {
- go.SetActive(false);
- }
- else
- {
- Object.DestroyImmediate(go);
- }
- }
- #endregion
- #region Miscellaneous
- public static void BeginInspector()
- {
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.BeginVertical();
- }
- public static void EndInspector()
- {
- EditorGUILayout.EndVertical();
- EditorGUILayout.Space(2);
- EditorGUILayout.EndHorizontal();
- }
- static void PrepareStyles()
- {
- if(generatedGUIStyles)
- {
- return;
- }
- // Label
- labelStyle = new GUIStyle(GUI.skin.label);
- labelStyle.richText = true;
- // Button
- buttonStyle = new GUIStyle(GUI.skin.button);
- buttonStyle.richText = true;
- buttonStyle.alignment = TextAnchor.MiddleCenter;
- // Right Anchor
- rightTextStyle = new GUIStyle(labelStyle);
- rightTextStyle.alignment = TextAnchor.MiddleRight;
- // Center Anchor
- centerTextStyle = new GUIStyle(labelStyle);
- centerTextStyle.alignment = TextAnchor.MiddleCenter;
- // Bottom Right Anchor
- bottomRightTextStyle = new GUIStyle(labelStyle);
- bottomRightTextStyle.alignment = TextAnchor.LowerRight;
- // Top Right Anchor
- topRightTextStyle = new GUIStyle(labelStyle);
- topRightTextStyle.alignment = TextAnchor.UpperRight;
- // White Box
- whiteBoxStyle = new GUIStyle(GUI.skin.box);
- whiteBoxStyle.normal.background = whiteBoxStyle.onNormal.background = whiteBoxStyle.active.background =
- whiteBoxStyle.onActive.background = whiteBoxStyle.focused.background = whiteBoxStyle.onFocused.background =
- whiteBoxStyle.hover.background = whiteBoxStyle.onHover.background = whiteBoxTexture;
- // Rich Everything
- for(int n = 0; n < GUI.skin.customStyles.Length; n++)
- {
- if(GUI.skin.customStyles[n] != null)
- {
- GUI.skin.customStyles[n].richText = true;
- }
- }
- // Finish
- generatedGUIStyles = true;
- }
- public static bool HintButton(string category)
- {
- bool toggled = hints.Contains(category);
- string buttonName = toggled ? "<size=8> </size><b>?</b>" : "?";
- if (GUILayout.Button(buttonName, buttonStyle, GUILayout.Width(21)))
- {
- if (hints.Contains(category))
- {
- hints.Remove(category);
- }
- else
- {
- hints.Add(category);
- }
- toggled = !toggled;
- }
- if (toggled)
- {
- GUI.color = new Color(1, 1, 1, 0.8f);
- GUI.Toolbar(GUILayoutUtility.GetLastRect(), 0, new string[] { buttonName }, buttonStyle);
- GUI.color = new Color(1, 1, 1, 1f);
- }
- return toggled;
- }
- public static void RepaintInspector()
- {
- repaintViews = true;
- GUI.FocusControl("");
- }
- public static bool EditingPrefabPreview(Object target)
- {
- return EditorUtility.IsPersistent(target) && PrefabStageUtility.GetCurrentPrefabStage() == null;
- }
- public static void OpenPrefabButton(Object target)
- {
- GUI.color = Color.white;
- if (GUILayout.Button("Click here to open the prefab."))
- {
- try
- {
- PrefabStageUtility.OpenPrefab(AssetDatabase.GetAssetPath(target));
- }
- catch
- {
- EditorUtility.DisplayDialog("Something went wrong.", "Please open the prefab manually, by double-clicking it in the project window.", "Okay");
- }
- }
- }
- #endregion
- #region Label Utility
- public static void Lines()
- {
- GUI.color = new Color(1, 1, 1, 0.5f);
- EditorGUILayout.LabelField("− − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − −", GUILayout.Height(7f));
- GUI.color = new Color(1, 1, 1, 1f);
- }
- public static void Label(string text)
- {
- EditorGUILayout.LabelField(text, labelStyle);
- }
- public static void ScalingLabel(string text, float scaleWidth)
- {
- int size = Mathf.FloorToInt(Mathf.Clamp(12 * Mathf.Pow(DNPEditorInternal.currentWidth / scaleWidth, 1.525f), 9, 12));
- Label("<size=" + size + ">" + text + "</size>");
- }
- public static string CheckmarkString(bool state)
- {
- if(EditorGUIUtility.isProSkin)
- {
- return state ? "<size=15><b><color=#00FF00>✓</color></b></size>" : "<size=16><b><color=#FF0000>✗</color></b></size>";
- }
- else
- {
- return state ? "<size=15><b><color=#00BB00>✓</color></b></size>" : "<size=16><b><color=#BB0000>✗</color></b></size>";
- }
- }
- #endregion
- #region Box Utility
- public static void StartBox(Color color, bool isActivated = true)
- {
- GUI.color = color;
- StartBox(isActivated);
- GUI.color = Color.white;
- }
- public static void StartBox(bool isActivated = true)
- {
- // Start Box
- if (EditorGUIUtility.isProSkin)
- {
- if (isActivated)
- {
- GUI.backgroundColor = new Color(0.3f, 0.3f, 0.3f, 1);
- }
- else
- {
- GUI.backgroundColor = new Color(0.25f, 0.25f, 0.25f, 1);
- }
- }
- else
- {
- if (isActivated)
- {
- GUI.backgroundColor = new Color(0.68f, 0.68f, 0.68f, 1);
- }
- else
- {
- GUI.backgroundColor = new Color(0.72f, 0.72f, 0.72f, 1);
- }
- }
- GUILayout.BeginHorizontal(whiteBoxStyle);
- GUILayout.Space(5);
- GUILayout.BeginVertical();
- GUILayout.Space(5);
- GUI.backgroundColor = Color.white;
- }
- public static void CloseBox(Color color, bool isActivated = true)
- {
- GUI.color = color;
- CloseBox(isActivated);
- GUI.color = Color.white;
- }
- public static void CloseBox(bool isActivated = true)
- {
- GUILayout.Space(5);
- EditorGUILayout.EndVertical();
- GUILayout.Space(5);
- EditorGUILayout.EndHorizontal();
- Rect lastRect = GUILayoutUtility.GetLastRect();
- lastRect.width -= 2;
- BoxLastRect(isActivated);
- }
- static void BoxLastRect(bool isActivated = true)
- {
- Rect lastRect = GUILayoutUtility.GetLastRect();
- BoxRect(lastRect, isActivated);
- }
- static void BoxRect(Rect targetRect, bool isActivated = true, bool withTop = true)
- {
- Rect leftBar = new Rect(targetRect);
- leftBar.width = 3;
- Rect rightBar = new Rect(targetRect);
- rightBar.x += rightBar.width - 3;
- rightBar.width = 3;
- Rect topBar = new Rect(targetRect);
- topBar.height = 3;
- topBar.x += 3;
- topBar.width -= 6;
- Rect bottomBar = new Rect(targetRect);
- bottomBar.y += bottomBar.height - 3;
- bottomBar.height = 3;
- bottomBar.x += 3;
- bottomBar.width -= 6;
- DrawOutlineBox(leftBar, isActivated);
- DrawOutlineBox(rightBar, isActivated);
- DrawOutlineBox(bottomBar, isActivated);
- if (withTop)
- {
- DrawOutlineBox(topBar, isActivated);
- }
- DrawBox(leftBar, isActivated);
- DrawBox(rightBar, isActivated);
- DrawBox(bottomBar, isActivated);
- if (withTop)
- {
- DrawBox(topBar, isActivated);
- }
- }
- static void DrawBox(Rect rectPosition, bool isActivated = true)
- {
- Color boxColor = default;
- if (EditorGUIUtility.isProSkin)
- {
- if (isActivated)
- {
- boxColor = new Color(0.66f, 0.66f, 0.66f, 1);
- }
- else
- {
- boxColor = new Color(0.38f, 0.38f, 0.38f, 1);
- }
- }
- else
- {
- if (isActivated)
- {
- boxColor = new Color(0.55f, 0.55f, 0.55f, 1);
- }
- else
- {
- boxColor = new Color(0.67f, 0.67f, 0.67f, 1);
- }
- }
- DrawBox(rectPosition, boxColor);
- }
- static void DrawBox(Rect rectPosition, Color boxColor)
- {
- GUI.backgroundColor = boxColor;
- GUI.Box(rectPosition, "", whiteBoxStyle);
- GUI.backgroundColor = Color.white;
- }
- static void DrawOutlineBox(Rect rectPosition, bool isActivated = true)
- {
- // Adjust Position
- rectPosition.width += 2;
- rectPosition.height += 2;
- // Draw Box
- if (EditorGUIUtility.isProSkin)
- {
- if (isActivated)
- {
- DrawBox(rectPosition, new Color(0.24f, 0.24f, 0.24f, 1));
- }
- else
- {
- DrawBox(rectPosition, new Color(0.21f, 0.21f, 0.21f, 1));
- }
- }
- else
- {
- if (isActivated)
- {
- DrawBox(rectPosition, new Color(0.35f, 0.35f, 0.35f, 1));
- }
- else
- {
- DrawBox(rectPosition, new Color(0.5f, 0.5f, 0.5f, 1));
- }
- }
- }
- #endregion
- }
- }
- #endif
|