From 48400ff5cea79ae637e602508def16e2830594b2 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Wed, 10 Apr 2024 16:26:29 -0400 Subject: [PATCH] Add better error handling to particle system creation. Add skinned model -> animation graph workflow, though a bit hacky. --- .../Content/Proxy/ParticleEmitterProxy.cs | 4 ++ .../Editor/Content/Proxy/SkinnedModelProxy.cs | 55 +++++++++++++++++++ .../Windows/Assets/AnimationGraphWindow.cs | 9 +++ 3 files changed, 68 insertions(+) diff --git a/Source/Editor/Content/Proxy/ParticleEmitterProxy.cs b/Source/Editor/Content/Proxy/ParticleEmitterProxy.cs index 9ac4d698c..be2530efa 100644 --- a/Source/Editor/Content/Proxy/ParticleEmitterProxy.cs +++ b/Source/Editor/Content/Proxy/ParticleEmitterProxy.cs @@ -91,6 +91,10 @@ namespace FlaxEditor.Content } ParticleEmitter emitter = FlaxEngine.Content.LoadAsync(particleItem.ID); + if (emitter == null || emitter.WaitForLoaded()) + { + Editor.LogError("Failed to load base particle emitter."); + } ParticleSystemPreview tempPreview = new ParticleSystemPreview(false); ParticleSystemTimeline timeline = new ParticleSystemTimeline(tempPreview); diff --git a/Source/Editor/Content/Proxy/SkinnedModelProxy.cs b/Source/Editor/Content/Proxy/SkinnedModelProxy.cs index e05ca80d9..97e423a2d 100644 --- a/Source/Editor/Content/Proxy/SkinnedModelProxy.cs +++ b/Source/Editor/Content/Proxy/SkinnedModelProxy.cs @@ -2,6 +2,10 @@ using System; using FlaxEditor.Content.Thumbnails; +using FlaxEditor.GUI.ContextMenu; +using FlaxEditor.GUI.Docking; +using FlaxEditor.Options; +using FlaxEditor.Surface; using FlaxEditor.Viewport.Previews; using FlaxEditor.Windows; using FlaxEditor.Windows.Assets; @@ -39,6 +43,57 @@ namespace FlaxEditor.Content /// public override Type AssetType => typeof(SkinnedModel); + /// + public override void OnContentWindowContextMenu(ContextMenu menu, ContentItem item) + { + base.OnContentWindowContextMenu(menu, item); + + if (item is BinaryAssetItem binaryAssetItem) + { + var button = menu.AddButton("Create Animation Graph", CreateAnimationGraphClicked); + button.Tag = binaryAssetItem; + } + } + + private void CreateAnimationGraphClicked(ContextMenuButton obj) + { + var binaryAssetItem = (BinaryAssetItem)obj.Tag; + CreateAnimationGraph(binaryAssetItem); + } + + /// + /// Creates the animation graph from the given particle emitter. + /// + /// The skinned model item to use as the base model for the animation graph. + public static void CreateAnimationGraph(BinaryAssetItem skinnedModelItem) + { + var animationGraphName = skinnedModelItem.ShortName + " Graph"; + var animationGraphProxy = Editor.Instance.ContentDatabase.GetProxy(); + Editor.Instance.Windows.ContentWin.NewItem(animationGraphProxy, null, item => OnAnimationGraphCreated(item, skinnedModelItem), animationGraphName); + } + + private static void OnAnimationGraphCreated(ContentItem item, BinaryAssetItem skinnedModelItem) + { + var skinnedModel = FlaxEngine.Content.LoadAsync(skinnedModelItem.ID); + if (skinnedModel == null || skinnedModel.WaitForLoaded()) + { + Editor.LogError("Failed to load base skinned model."); + } + + // Hack the animation graph window to modify the base model of the animation graph. + AnimationGraphWindow win = new AnimationGraphWindow(Editor.Instance, item as AssetItem); + win.Show(); + + // Ensure the window knows the asset is loaded so we can save it later. + win.Asset.WaitForLoaded(); + win.Update(0); // Call Update() to refresh the loaded flag. + + win.SetBaseModel(skinnedModel); + win.Surface.MarkAsEdited(); + win.Save(); + win.Close(); + } + /// public override void OnThumbnailDrawPrepare(ThumbnailRequest request) { diff --git a/Source/Editor/Windows/Assets/AnimationGraphWindow.cs b/Source/Editor/Windows/Assets/AnimationGraphWindow.cs index 9d65e5f7b..e75becf72 100644 --- a/Source/Editor/Windows/Assets/AnimationGraphWindow.cs +++ b/Source/Editor/Windows/Assets/AnimationGraphWindow.cs @@ -295,6 +295,15 @@ namespace FlaxEditor.Windows.Assets base.SetParameter(index, value); } + /// + /// Sets the base model of the animation graph this window is editing. + /// + /// The new base model. + public void SetBaseModel(SkinnedModel baseModel) + { + _properties.BaseModel = baseModel; + } + /// protected override void UnlinkItem() {