Skip to content

Commit

Permalink
Added animation loop time setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Siccity committed Nov 14, 2020
1 parent 695ab4d commit 703b004
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Scripts/Editor/GLBImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
16 changes: 12 additions & 4 deletions Scripts/Editor/GLTFAssetUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Siccity.GLTFUtility {
/// <summary> Contains methods for saving a gameobject as an asset </summary>
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);
Expand All @@ -17,9 +17,9 @@ public static void SaveToAsset(GameObject root, AnimationClip[] animations, Asse
MeshRenderer[] renderers = root.GetComponentsInChildren<MeshRenderer>(true);
SkinnedMeshRenderer[] skinnedRenderers = root.GetComponentsInChildren<SkinnedMeshRenderer>(true);
MeshFilter[] filters = root.GetComponentsInChildren<MeshFilter>(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) {
Expand All @@ -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<AnimationClip> visitedAnimations = new HashSet<AnimationClip>();
for (int i = 0; i < animations.Length; i++) {
AnimationClip clip = animations[i];
Expand Down
6 changes: 4 additions & 2 deletions Scripts/Editor/GLTFImporter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEditor.Experimental.AssetImporters;
using UnityEditor;
using UnityEditor.Experimental.AssetImporters;
using UnityEngine;

namespace Siccity.GLTFUtility {
Expand All @@ -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);
}
}
}
11 changes: 11 additions & 0 deletions Scripts/Settings/AnimationSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using UnityEngine;
using UnityEngine.Rendering;

namespace Siccity.GLTFUtility {
/// <summary> Defines how animations are imported </summary>
[Serializable]
public class AnimationSettings {
public bool looping;
}
}
11 changes: 11 additions & 0 deletions Scripts/Settings/AnimationSettings.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Scripts/Settings/ImportSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 1 addition & 3 deletions Scripts/Spec/GLTFAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 703b004

Please sign in to comment.