From 7f5696dd937a04746fb2a244b915932a612e21ea Mon Sep 17 00:00:00 2001 From: Neil Sarkar Date: Thu, 23 Jul 2020 23:35:50 -0700 Subject: [PATCH] Generate ScreenId enums --- Runtime/Navigation/Navigator.cs | 37 +++++++++++++------ Runtime/Navigation/ScreenId.cs | 5 ++- .../Navigation}/ScreenIdGenerator.cs | 33 +++++++++++------ .../Navigation}/ScreenIdGenerator.cs.meta | 2 +- 4 files changed, 52 insertions(+), 25 deletions(-) rename {Editor => Runtime/Navigation}/ScreenIdGenerator.cs (60%) rename {Editor => Runtime/Navigation}/ScreenIdGenerator.cs.meta (83%) diff --git a/Runtime/Navigation/Navigator.cs b/Runtime/Navigation/Navigator.cs index d7df257..1787896 100644 --- a/Runtime/Navigation/Navigator.cs +++ b/Runtime/Navigation/Navigator.cs @@ -15,6 +15,7 @@ namespace VioletUI { /// /// It is primarily used to navigate between screens, and also exposes the and lifecycle events. /// + [ExecuteAlways] public class Navigator : TidyBehaviour { public ScreenId homeScreen = ScreenId.None; public bool hasCamera; @@ -40,6 +41,7 @@ public class Navigator : TidyBehaviour { CancellationTokenSource canceler = null; void Awake() { + if (!Application.isPlaying) { return; } LoadScreens(); VisitFirstScreen(); } @@ -49,7 +51,7 @@ void LoadScreens() { } ScreenId screenId = ScreenId.None; - foreach (Screen screen in GetComponentsInChildren()) { + foreach (Screen screen in GetComponentsInChildren(true)) { var isValid = Enum.TryParse(screen.name, out screenId); if (!isValid) { throw new VioletException($"{screen.name} does not have a valid ScreenId. Make sure this screen is added to MenuBuilder."); @@ -162,7 +164,8 @@ protected virtual void VisitFirstScreen() { ScreenId ScreenToScreenId(Screen screen) { ScreenId ret; var slug = screen.name.Replace(" ", ""); - var ok = Enum.TryParse(screen.name.Replace(" ", ""), out ret); + var ok = Enum.TryParse(slug, out ret); + if (!ok) { throw new VioletEnumException($"{slug} does not exist in ScreenId. This should be set up automatically by navigator but you can add manually as a workaround."); } @@ -174,6 +177,14 @@ ScreenId ScreenToScreenId(Screen screen) { [SerializeField, HideInInspector] ScreenId originalHomeScreen; public void Edit(Screen screen) { + try { + homeScreen = ScreenToScreenId(screen); + } catch (VioletEnumException) { + ScreenIdGenerator.Generate(screen); + Debug.LogWarning($"VioletUI - Couldn't find {screen.name} in the ScreenId enum. This should be fixed if you try your action again. If not, please report a bug."); + return; + } + try { PrefabUtility.UnpackPrefabInstance(screen.gameObject, PrefabUnpackMode.OutermostRoot, InteractionMode.AutomatedAction); } catch (ArgumentException) {} @@ -182,11 +193,6 @@ public void Edit(Screen screen) { EditingScreen = screen; originalHomeScreen = homeScreen; - try { - homeScreen = ScreenToScreenId(screen); - } catch(VioletEnumException) { - print($"Do enum"); - } } public void FinishEditing(Screen screen = null) { @@ -210,12 +216,21 @@ public void AddScreen() { } float lastUpdate; - void OnValidate() { + int lastCount; + void Update() { if (Application.isPlaying) { return; } - // if (transform.childCount == 0) { return; } - // if (Time.time - lastUpdate <= .5f) { return; } + if (transform.childCount == 0) { return; } + if (transform.childCount == lastCount) { return; } + if (Time.time - lastUpdate <= .5f) { return; } + lastCount = transform.childCount; lastUpdate = Time.time; - print($"Gonna regen enum"); + RegenerateEnums(); + } + + [Button] + void RegenerateEnums() { + var screens = GetComponentsInChildren(true); + ScreenIdGenerator.Generate(screens); } #endif } diff --git a/Runtime/Navigation/ScreenId.cs b/Runtime/Navigation/ScreenId.cs index 6339158..eed3b1c 100644 --- a/Runtime/Navigation/ScreenId.cs +++ b/Runtime/Navigation/ScreenId.cs @@ -1,5 +1,6 @@ +//Names are automatically added through ScreenIdGenerator.cs, deletions are done manually :) namespace VioletUI { public enum ScreenId { - None + None = 0 } -} \ No newline at end of file +} diff --git a/Editor/ScreenIdGenerator.cs b/Runtime/Navigation/ScreenIdGenerator.cs similarity index 60% rename from Editor/ScreenIdGenerator.cs rename to Runtime/Navigation/ScreenIdGenerator.cs index 3f7d130..4fb550e 100644 --- a/Editor/ScreenIdGenerator.cs +++ b/Runtime/Navigation/ScreenIdGenerator.cs @@ -8,7 +8,19 @@ namespace VioletUI { public class ScreenIdGenerator { - static StringBuilder sb; + static StringBuilder sb = new StringBuilder(); + + public static void Generate(Screen screen) { + Generate(new Screen[] {screen}); + } + + public static void Generate(Screen[] screens) { + var screenStrings = new List(screens.Length); + foreach (var screen in screens) { + screenStrings.Add(screen.name); + } + Generate(screenStrings); + } public static void Generate(List screens) { var newScreens = Filter(screens); @@ -29,29 +41,28 @@ static List Filter(List screens) { static void AddScreens(List screens) { sb.Clear(); - sb.Append("//Names are automatically added through ScreenIdGenerator.cs, deletions are done manually :)" + Environment.NewLine); - sb.Append("public enum ScreenId {" + Environment.NewLine); - sb.Append("\tNone = 0," + Environment.NewLine); + sb.AppendLine("//Names are automatically added through ScreenIdGenerator.cs, deletions are done manually :)"); + sb.AppendLine("namespace VioletUI {"); + sb.AppendLine("\tpublic enum ScreenId {"); + sb.AppendLine("\t\tNone = 0,"); // write all existing Enum values - note that unused screens // will have to be deleted manually. this is to avoid losing references. foreach (ScreenId screenId in Enum.GetValues(typeof(ScreenId))) { if (screenId == ScreenId.None) { continue; } - sb.Append($"\t{Enum.GetName(typeof(ScreenId), screenId)} = {(int)screenId},{Environment.NewLine}"); + sb.AppendLine($"\t\t{Enum.GetName(typeof(ScreenId), screenId)} = {(int)screenId},"); } // write any new screen names with incrementing ids var nextId = Enum.GetValues(typeof(ScreenId)).Cast().Max() + 1; foreach (var screen in screens) { - sb.Append($"\t{screen} = {nextId++},{Environment.NewLine}"); + sb.AppendLine($"\t\t{screen} = {nextId++},"); } - - sb.Append("}"); - - string path = Path.Combine(Application.dataPath, "Scripts", "VioletUI", "ScreenId.cs"); + sb.AppendLine("\t}"); + sb.AppendLine("}"); + string path = Path.GetFullPath("Packages/violetui/Runtime/Navigation/ScreenId.cs"); File.WriteAllText(path, sb.ToString()); AssetDatabase.Refresh(); - } static string sanitize(string s) { diff --git a/Editor/ScreenIdGenerator.cs.meta b/Runtime/Navigation/ScreenIdGenerator.cs.meta similarity index 83% rename from Editor/ScreenIdGenerator.cs.meta rename to Runtime/Navigation/ScreenIdGenerator.cs.meta index 98ab518..acc0284 100644 --- a/Editor/ScreenIdGenerator.cs.meta +++ b/Runtime/Navigation/ScreenIdGenerator.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 59034a36babde49d1b1fa0d3e9e2e0ef +guid: 3cb3551e7a6b249a39cbd045f31ce5b6 MonoImporter: externalObjects: {} serializedVersion: 2