Tanoda
LoadHumanoidAnimationFromFilePicker.cs
Go to the documentation of this file.
1#pragma warning disable 649
2using System.Collections.Generic;
3using TriLibCore.General;
6using UnityEngine;
7using UnityEngine.UI;
8#if UNITY_EDITOR
9
10#endif
11namespace TriLibCore.Samples
12{
16 public class LoadHumanoidAnimationFromFilePicker : MonoBehaviour
17 {
23
28 public AnimationClip MecanimAnimationClipTemplate;
29
33 [SerializeField] private Button _loadAnimationButton;
34
38 [SerializeField] private Button _animationPlayTemplate;
39
43 private GameObject _loadedGameObject;
44
48 private readonly List<AnimationClip> _loadedAnimationClips = new List<AnimationClip>();
49
63 public void LoadAnimation()
64 {
66
67 var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
68 assetLoaderOptions.AddAssetUnloader = false;
69 assetLoaderOptions.ImportMaterials = false;
70 assetLoaderOptions.ImportTextures = false;
71 assetLoaderOptions.ImportMeshes = false;
72 assetLoaderOptions.SampleBindPose = true;
73 assetLoaderOptions.AnimationType = AnimationType.Humanoid;
74 assetLoaderOptions.HumanoidAvatarMapper =
75 Resources.Load<HumanoidAvatarMapper>("Mappers/Avatar/MixamoAndBipedByNameHumanoidAvatarMapper");
76
77 var legacyToHumanoidAnimationClipMapper =
78 ScriptableObject.CreateInstance<LegacyToHumanoidAnimationClipMapper>();
79 legacyToHumanoidAnimationClipMapper.MecanimAnimationClipTemplate = MecanimAnimationClipTemplate;
80 legacyToHumanoidAnimationClipMapper.name = "LegacyToHumanoidAnimationClipMapper";
81 assetLoaderOptions.FixedAllocations.Add(legacyToHumanoidAnimationClipMapper);
82
83 var simpleAnimationPlayerAnimationClipMapper =
84 ScriptableObject.CreateInstance<SimpleAnimationPlayerAnimationClipMapper>();
85 simpleAnimationPlayerAnimationClipMapper.name = "SimpleAnimationPlayerAnimationClipMapper";
86 assetLoaderOptions.FixedAllocations.Add(simpleAnimationPlayerAnimationClipMapper);
87
88 assetLoaderOptions.AnimationClipMappers = new AnimationClipMapper[]
89 {
90 legacyToHumanoidAnimationClipMapper,
91 simpleAnimationPlayerAnimationClipMapper
92 };
93
94 var assetLoaderFilePicker = AssetLoaderFilePicker.Create();
95 assetLoaderFilePicker.LoadModelFromFilePickerAsync("Select a Model file", OnLoad, OnMaterialsLoad,
96 OnProgress, OnBeginLoad, OnError, null, assetLoaderOptions);
97 }
98
103 private void OnBeginLoad(bool filesSelected)
104 {
105 _loadAnimationButton.interactable = !filesSelected;
106 }
107
115 private void OnError(IContextualizedError obj)
116 {
117 Debug.LogError($"An error ocurred while loading your Model: {obj.GetInnerException()}");
118 }
119
125 private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
126 {
127 Debug.Log($"Loading Model. Progress: {progress:P}");
128 }
129
139 private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
140 {
141 if (assetLoaderContext.RootGameObject != null)
142 {
143 Debug.Log("Materials loaded. Model fully loaded.");
144 var simpleAnimationPlayer = assetLoaderContext.RootGameObject.GetComponent<SimpleAnimationPlayer>();
145 if (simpleAnimationPlayer != null)
146 foreach (var animationClip in simpleAnimationPlayer.AnimationClips)
147 {
148 // Ignore Animations not-imported.
149 if (animationClip == null) continue;
150 var animationIndex = _loadedAnimationClips.Count;
151 var button = Instantiate(_animationPlayTemplate, _animationPlayTemplate.transform.parent);
152 button.GetComponentInChildren<Text>().text = animationClip.name;
153 button.onClick.AddListener(delegate
154 {
156 });
157 button.gameObject.SetActive(true);
158 _loadedAnimationClips.Add(animationClip);
159 }
160
161 Destroy(assetLoaderContext
162 .RootGameObject); //Don't destroy the GameObject if you're using the AssetLoaderOptions.AddAssetUnloader options as this will break the AnimationClip references
163 }
164 else
165 {
166 Debug.Log("Model could not be loaded.");
167 }
168 }
169
175 private void OnLoad(AssetLoaderContext assetLoaderContext)
176 {
177 _loadAnimationButton.interactable = true;
178 }
179 }
180}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
Represents an Asset Loader which loads files using a platform-specific file picker.
static AssetLoaderFilePicker Create()
Creates the Asset Loader File Picker Singleton instance.
void LoadModelFromFilePickerAsync(string title, Action< AssetLoaderContext > onLoad, Action< AssetLoaderContext > onMaterialsLoad, Action< AssetLoaderContext, float > onProgress, Action< bool > onBeginLoad, Action< IContextualizedError > onError, GameObject wrapperGameObject, AssetLoaderOptions assetLoaderOptions)
Loads a Model from the OS file picker asynchronously, or synchronously when the OS doesn't support Th...
Represents a Mapper that converts legacy Animation Clips into humanoid Animation Clips.
AnimationClip MecanimAnimationClipTemplate
Template mecanim animation clip. Unity runtime API can't access mecanim animation clip settings as ro...
Represents a Mapper that creates a Simple Animation Player used to play Animation Clips by their inde...
Represents a Playable used to play Animations from its Animation Clip List using names or indices as ...
IList< AnimationClip > AnimationClips
Source animation clips.
void PlayAnimation(int index)
Plays the Animation Clip with the given index.
Represents a sample that allows user loading Humanoid Animations with a File Picker.
AnimationClip MecanimAnimationClipTemplate
Template mecanim animation clip. Unity runtime API can't access mecanim animation clip settings as ro...
void LoadAnimation()
Creates the AssetLoaderOptions instance and displays the Model file-picker. First we will point the S...
SimpleAnimationPlayer OriginalModelSimpleAnimationPlayer
Simple Animation Player component located on the existing Model in the Scene. The Simple Animation Pl...