diff options
Diffstat (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/UI')
4 files changed, 253 insertions, 0 deletions
diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/UI/WelcomeScreen.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/UI/WelcomeScreen.cs new file mode 100644 index 0000000..424dad2 --- /dev/null +++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/UI/WelcomeScreen.cs @@ -0,0 +1,200 @@ +using System; +using System.Linq; +using UnityEditor; +using UnityEditor.SceneManagement; +using UnityEditor.UIElements; +using UnityEngine; +using UnityEngine.UIElements; +using UnityEditor.Compilation; +using UnityEditor.Scripting.ScriptCompilation; + +namespace Pathfinding { + internal class WelcomeScreen : UnityEditor.EditorWindow { + [SerializeField] + private VisualTreeAsset m_VisualTreeAsset = default; + + public bool isImportingSamples; + private bool askedAboutQuitting; + + [InitializeOnLoadMethod] + public static void TryCreate () { + if (!PathfindingEditorSettings.instance.hasShownWelcomeScreen) { + // Wait a bit before showing the window to avoid stuttering + // as all the other windows in Unity load. + // This makes the animation smoother. + var delay = 0.5f; + var t0 = Time.realtimeSinceStartup; + EditorApplication.CallbackFunction create = null; + create = () => { + if (Time.realtimeSinceStartup - t0 > delay) { + EditorApplication.update -= create; + PathfindingEditorSettings.instance.hasShownWelcomeScreen = true; + PathfindingEditorSettings.instance.Save(); + Create(); + } + }; + EditorApplication.update += create; + } + } + + public static void Create () { + var window = GetWindow<WelcomeScreen>( + true, + "A* Pathfinding Project", + true + ); + window.minSize = window.maxSize = new Vector2(400, 400*1.618f); + window.ShowUtility(); + } + + + public void CreateGUI () { + VisualElement root = rootVisualElement; + + VisualElement labelFromUXML = m_VisualTreeAsset.Instantiate(); + root.Add(labelFromUXML); + + var sampleButton = root.Query<Button>("importSamples").First(); + var samplesImportedIndicator = root.Query("samplesImported").First(); + samplesImportedIndicator.visible = GetSamples(out var sample) && sample.isImported; + + sampleButton.clicked += ImportSamples; + root.Query<Button>("documentation").First().clicked += OpenDocumentation; + root.Query<Button>("getStarted").First().clicked += OpenGetStarted; + root.Query<Button>("changelog").First().clicked += OpenChangelog; + root.Query<Label>("version").First().text = "Version " + AstarPath.Version.ToString(); + AnimateLogo(root.Query("logo").First()); + } + + static string FirstSceneToLoad = "Recast3D"; + + public void OnEnable () { + if (isImportingSamples) { + // This will be after the domain reload that happened after the samples were imported + OnPostImportedSamples(); + } + } + + public void OnPostImportedSamples () { + isImportingSamples = false; + // Load the example scene + var sample = UnityEditor.PackageManager.UI.Sample.FindByPackage("com.arongranberg.astar", "").First(); + if (sample.isImported) { + var relativePath = "Assets/" + System.IO.Path.GetRelativePath(Application.dataPath, sample.importPath); + Debug.Log(relativePath); + var scenes = AssetDatabase.FindAssets("t:scene", new string[] { relativePath }); + string bestScene = null; + for (int i = 0; i < scenes.Length; i++) { + scenes[i] = AssetDatabase.GUIDToAssetPath(scenes[i]); + if (scenes[i].Contains(FirstSceneToLoad)) { + bestScene = scenes[i]; + } + } + if (bestScene == null) bestScene = scenes.FirstOrDefault(); + if (bestScene != null) { + if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) { + EditorSceneManager.OpenScene(bestScene); + } + } + } + } + + void AnimateLogo (VisualElement logo) { + var t0 = Time.realtimeSinceStartup; + EditorApplication.CallbackFunction introAnimation = null; + int ticks = 0; + introAnimation = () => { + var t = Time.realtimeSinceStartup - t0; + if (ticks == 1) { + logo.RemoveFromClassList("largeIconEntry"); + } + Repaint(); + ticks++; + if (ticks > 1 && t > 5) { + EditorApplication.update -= introAnimation; + } + }; + EditorApplication.update += introAnimation; + } + + bool GetSamples (out UnityEditor.PackageManager.UI.Sample sample) { + sample = default; + + var samples = UnityEditor.PackageManager.UI.Sample.FindByPackage("com.arongranberg.astar", ""); + if (samples == null) { + return false; + } + var samplesArr = samples.ToArray(); + if (samplesArr.Length != 1) { + Debug.LogError("Expected exactly 1 sample. Found " + samplesArr.Length + ". This should not happen"); + return false; + } + + sample = samplesArr[0]; + return true; + } + + private void ImportSamples () { + if (!GetSamples(out var sample)) { + Debug.LogError("The A* Pathfinding Project is not installed via the Unity package manager. Cannot import samples."); + return; + } + + if (sample.isImported) { + // Show dialog box + if (!EditorUtility.DisplayDialog("Import samples", "Samples are already imported. Do you want to reimport them?", "Reimport", "Cancel")) { + return; + } + } + + isImportingSamples = true; + + CompilationPipeline.assemblyCompilationFinished += OnAssemblyCompilationFinished; + + if (!sample.Import(UnityEditor.PackageManager.UI.Sample.ImportOptions.OverridePreviousImports)) { + Debug.LogError("Failed to import samples"); + return; + } + + OnPostImportedSamples(); + } + + void OnAssemblyCompilationFinished (string assembly, CompilerMessage[] message) { + for (int i = 0; i < message.Length; i++) { + // E.g. + // error CS0006: Metadata file 'Assets/AstarPathfindingProject/Plugins/Clipper/Pathfinding.ClipperLib.dll' could not be found + // error CS0006: Metadata file 'Assets/AstarPathfindingProject/Plugins/DotNetZip/Pathfinding.Ionic.Zip.Reduced.dll' could not be found + // error CS0006: Metadata file 'Assets/AstarPathfindingProject/Plugins/Poly2Tri/Pathfinding.Poly2Tri.dll' could not be found + // I believe this can happen if the user previously has had the package imported into the Assets folder (e.g. version 4), + // and then it is imported via the package manager, and the samples imported. + // Unity seems to miss that the dll files now have new locations, and gets confused. + if (message[i].type == CompilerMessageType.Error && message[i].message.Contains("CS0006")) { + Debug.LogError("Compilation failed due to a Unity bug. Asking user to restart Unity."); + + if (!askedAboutQuitting) { + if (EditorUtility.DisplayDialog("Restart Unity", "Your version of Unity has a bug that, unfortunately, requires the editor to be restarted, after importing the samples.", "Quit Unity", "Cancel")) { + askedAboutQuitting = true; + EditorApplication.update += () => { + if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) { + EditorApplication.Exit(0); + } + }; + } + } + } + } + } + + private void OpenDocumentation () { + Application.OpenURL(AstarUpdateChecker.GetURL("documentation")); + } + + private void OpenGetStarted () { + Application.OpenURL(AstarUpdateChecker.GetURL("documentation") + "getstarted.html"); + } + + private void OpenChangelog () { + Application.OpenURL(AstarUpdateChecker.GetURL("changelog")); + } + } +} diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/UI/WelcomeScreen.cs.meta b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/UI/WelcomeScreen.cs.meta new file mode 100644 index 0000000..c0772ae --- /dev/null +++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/UI/WelcomeScreen.cs.meta @@ -0,0 +1,14 @@ +fileFormatVersion: 2 +guid: 826fe48313dac354bb9b4be850c8845e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: + - m_ViewDataDictionary: {instanceID: 0} + - m_VisualTreeAsset: {fileID: 9197481963319205126, guid: f270bacb724b66246a7f31e2a8e71fe1, + type: 3} + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/UI/WelcomeScreen.uxml b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/UI/WelcomeScreen.uxml new file mode 100644 index 0000000..1fcfe9e --- /dev/null +++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/UI/WelcomeScreen.uxml @@ -0,0 +1,29 @@ +<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False"> + <Style src="project://database/Assets/AstarPathfindingProject/Editor/AstarPathfindingProjectEditor.uss?fileID=7433441132597879392&guid=f1e4e8798274ca7448321794e89178b9&type=3#AstarPathfindingProjectEditor" /> + <ui:VisualElement name="VisualElement" class="welcomeWindow"> + <ui:VisualElement name="logo" class="largeIcon largeIconEntry" /> + <ui:Label text="<b>Welcome to the A* Pathfinding Project!</b> Here are some things to help you get started." style="color: rgb(255, 255, 255); -unity-text-align: upper-center; font-size: 16px; -unity-font-style: normal; margin-bottom: 30px; -unity-paragraph-spacing: 32px;" /> + <ui:Button name="importSamples" enable-rich-text="true" emoji-fallback-support="true" class="welcomeButton" style="flex-direction: row; align-items: center; justify-content: flex-end; padding-left: 10px;"> + <ui:VisualElement class="buttonIcon samples" /> + <ui:Label text="Import Examples" emoji-fallback-support="false" class="blah" /> + <ui:VisualElement style="flex-grow: 1;" /> + <ui:VisualElement name="samplesImported" class="buttonIconRight" /> + </ui:Button> + <ui:Button name="getStarted" enable-rich-text="true" emoji-fallback-support="true" class="welcomeButton" style="flex-direction: row; align-items: center; justify-content: flex-end; padding-left: 10px;"> + <ui:VisualElement class="buttonIcon education" /> + <ui:Label text="Get Started Guide" emoji-fallback-support="false" class="blah" /> + <ui:VisualElement style="flex-grow: 1;" /> + </ui:Button> + <ui:Button name="changelog" enable-rich-text="true" emoji-fallback-support="true" class="welcomeButton" style="flex-direction: row; align-items: center; justify-content: flex-end; padding-left: 10px;"> + <ui:VisualElement class="buttonIcon changelog" /> + <ui:Label text="Changelog" emoji-fallback-support="false" class="blah" /> + <ui:VisualElement style="flex-grow: 1;" /> + </ui:Button> + <ui:Button name="documentation" enable-rich-text="true" emoji-fallback-support="true" class="welcomeButton" style="flex-direction: row; align-items: center; justify-content: flex-end; padding-left: 10px;"> + <ui:VisualElement class="buttonIcon documentation" /> + <ui:Label text="Documentation" emoji-fallback-support="false" class="blah" /> + <ui:VisualElement style="flex-grow: 1;" /> + </ui:Button> + <ui:Label text="Version X.Y.Z" name="version" style="margin-top: auto; margin-bottom: 10px; color: rgb(103, 103, 103);" /> + </ui:VisualElement> +</ui:UXML> diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/UI/WelcomeScreen.uxml.meta b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/UI/WelcomeScreen.uxml.meta new file mode 100644 index 0000000..7fb0e1a --- /dev/null +++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/UI/WelcomeScreen.uxml.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: f270bacb724b66246a7f31e2a8e71fe1 +ScriptedImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 2 + userData: + assetBundleName: + assetBundleVariant: + script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0} |