From 703b0043e85193a4eb5632d882a5ce39495be2d2 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Sat, 14 Nov 2020 15:53:29 +0100 Subject: [PATCH] Added animation loop time setting --- Scripts/Editor/GLBImporter.cs | 2 +- Scripts/Editor/GLTFAssetUtility.cs | 16 ++++++++++++---- Scripts/Editor/GLTFImporter.cs | 6 ++++-- Scripts/Settings/AnimationSettings.cs | 11 +++++++++++ Scripts/Settings/AnimationSettings.cs.meta | 11 +++++++++++ Scripts/Settings/ImportSettings.cs | 1 + Scripts/Spec/GLTFAnimation.cs | 4 +--- 7 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 Scripts/Settings/AnimationSettings.cs create mode 100644 Scripts/Settings/AnimationSettings.cs.meta diff --git a/Scripts/Editor/GLBImporter.cs b/Scripts/Editor/GLBImporter.cs index 3582277..2f440ba 100644 --- a/Scripts/Editor/GLBImporter.cs +++ b/Scripts/Editor/GLBImporter.cs @@ -11,7 +11,7 @@ public override void OnImportAsset(AssetImportContext ctx) { if (importSettings == null) importSettings = new ImportSettings(); GameObject root = Importer.LoadFromFile(ctx.assetPath, importSettings, out animations, Format.GLB); // Save asset - GLTFAssetUtility.SaveToAsset(root, animations, ctx, importSettings.generateLightmapUVs); + GLTFAssetUtility.SaveToAsset(root, animations, ctx, importSettings); } } } \ No newline at end of file diff --git a/Scripts/Editor/GLTFAssetUtility.cs b/Scripts/Editor/GLTFAssetUtility.cs index 41b5409..92a6345 100644 --- a/Scripts/Editor/GLTFAssetUtility.cs +++ b/Scripts/Editor/GLTFAssetUtility.cs @@ -7,7 +7,7 @@ namespace Siccity.GLTFUtility { /// Contains methods for saving a gameobject as an asset public static class GLTFAssetUtility { - public static void SaveToAsset(GameObject root, AnimationClip[] animations, AssetImportContext ctx, bool generateLightmapUVs) { + public static void SaveToAsset(GameObject root, AnimationClip[] animations, AssetImportContext ctx, ImportSettings settings) { #if UNITY_2018_2_OR_NEWER ctx.AddObjectToAsset("main", root); ctx.SetMainObject(root); @@ -17,9 +17,9 @@ public static void SaveToAsset(GameObject root, AnimationClip[] animations, Asse MeshRenderer[] renderers = root.GetComponentsInChildren(true); SkinnedMeshRenderer[] skinnedRenderers = root.GetComponentsInChildren(true); MeshFilter[] filters = root.GetComponentsInChildren(true); - AddMeshes(filters, skinnedRenderers, ctx, generateLightmapUVs); + AddMeshes(filters, skinnedRenderers, ctx, settings.generateLightmapUVs); AddMaterials(renderers, skinnedRenderers, ctx); - AddAnimations(animations, ctx); + AddAnimations(animations, ctx, settings.animationSettings); } public static void AddMeshes(MeshFilter[] filters, SkinnedMeshRenderer[] skinnedRenderers, AssetImportContext ctx, bool generateLightmapUVs) { @@ -39,8 +39,16 @@ public static void AddMeshes(MeshFilter[] filters, SkinnedMeshRenderer[] skinned } } - public static void AddAnimations(AnimationClip[] animations, AssetImportContext ctx) { + public static void AddAnimations(AnimationClip[] animations, AssetImportContext ctx, AnimationSettings settings) { if (animations == null) return; + + // Editor-only animation settings + foreach (AnimationClip clip in animations) { + AnimationClipSettings clipSettings = AnimationUtility.GetAnimationClipSettings(clip); + clipSettings.loopTime = settings.looping; + AnimationUtility.SetAnimationClipSettings(clip, clipSettings); + } + HashSet visitedAnimations = new HashSet(); for (int i = 0; i < animations.Length; i++) { AnimationClip clip = animations[i]; diff --git a/Scripts/Editor/GLTFImporter.cs b/Scripts/Editor/GLTFImporter.cs index 5dfa386..a98809b 100644 --- a/Scripts/Editor/GLTFImporter.cs +++ b/Scripts/Editor/GLTFImporter.cs @@ -1,4 +1,5 @@ -using UnityEditor.Experimental.AssetImporters; +using UnityEditor; +using UnityEditor.Experimental.AssetImporters; using UnityEngine; namespace Siccity.GLTFUtility { @@ -12,8 +13,9 @@ public override void OnImportAsset(AssetImportContext ctx) { AnimationClip[] animations; if (importSettings == null) importSettings = new ImportSettings(); GameObject root = Importer.LoadFromFile(ctx.assetPath, importSettings, out animations, Format.GLTF); + // Save asset - GLTFAssetUtility.SaveToAsset(root, animations, ctx, importSettings.generateLightmapUVs); + GLTFAssetUtility.SaveToAsset(root, animations, ctx, importSettings); } } } \ No newline at end of file diff --git a/Scripts/Settings/AnimationSettings.cs b/Scripts/Settings/AnimationSettings.cs new file mode 100644 index 0000000..8521af2 --- /dev/null +++ b/Scripts/Settings/AnimationSettings.cs @@ -0,0 +1,11 @@ +using System; +using UnityEngine; +using UnityEngine.Rendering; + +namespace Siccity.GLTFUtility { + /// Defines how animations are imported + [Serializable] + public class AnimationSettings { + public bool looping; + } +} \ No newline at end of file diff --git a/Scripts/Settings/AnimationSettings.cs.meta b/Scripts/Settings/AnimationSettings.cs.meta new file mode 100644 index 0000000..d8fd3d0 --- /dev/null +++ b/Scripts/Settings/AnimationSettings.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 977131d8fdcfd7449b4c86946f77be9a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Settings/ImportSettings.cs b/Scripts/Settings/ImportSettings.cs index 4bf50c1..552d7a1 100644 --- a/Scripts/Settings/ImportSettings.cs +++ b/Scripts/Settings/ImportSettings.cs @@ -11,6 +11,7 @@ public class ImportSettings { public bool materials = true; [FormerlySerializedAs("shaders")] public ShaderSettings shaderOverrides = new ShaderSettings(); + public AnimationSettings animationSettings = new AnimationSettings(); public bool useLegacyClips; public bool generateLightmapUVs; diff --git a/Scripts/Spec/GLTFAnimation.cs b/Scripts/Spec/GLTFAnimation.cs index c5d1600..2b9a536 100644 --- a/Scripts/Spec/GLTFAnimation.cs +++ b/Scripts/Spec/GLTFAnimation.cs @@ -56,9 +56,7 @@ public ImportResult Import(GLTFAccessor.ImportResult[] accessors, GLTFNode.Impor result.clip = new AnimationClip(); result.clip.name = name; - if (importSettings.useLegacyClips) { - result.clip.legacy = true; - } + result.clip.legacy = importSettings.useLegacyClips; for (int i = 0; i < channels.Length; i++) { Channel channel = channels[i];