AvatarLoader.cs 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using TriLibCore.Extensions;
  2. using TriLibCore.General;
  3. using TriLibCore.Mappers;
  4. using UnityEngine;
  5. namespace TriLibCore.Samples
  6. {
  7. /// <summary>Represents a TriLib sample which allows the user to load and control a custom avatar.</summary>
  8. public class AvatarLoader : AssetViewerBase
  9. {
  10. /// <summary>
  11. /// Game object that is used to hide the model while it is loading.
  12. /// </summary>
  13. [SerializeField]
  14. private GameObject _wrapper;
  15. /// <summary>
  16. /// Shows the file picker so the user can load an avatar from the local file system.
  17. /// </summary>
  18. public void LoadAvatarFromFile()
  19. {
  20. LoadModelFromFile(_wrapper);
  21. }
  22. /// <summary>
  23. /// Shows the file picker so the user can load a new animation for the current avatar from the local file system.
  24. /// </summary>
  25. public void LoadAnimationFromFile()
  26. {
  27. LoadModelFromFile(_wrapper, OnAnimationMaterialsLoad);
  28. }
  29. /// <summary>Event triggered when the Model (including Textures and Materials) has been fully loaded, when user loads a new Animation.</summary>
  30. /// <param name="assetLoaderContext">The Asset Loader Context reference. Asset Loader Context contains the information used during the Model loading process, which is available to almost every Model processing method</param>
  31. private void OnAnimationMaterialsLoad(AssetLoaderContext assetLoaderContext)
  32. {
  33. }
  34. /// <summary>Event triggered when the Model (including Textures and Materials) has been fully loaded.</summary>
  35. /// <param name="assetLoaderContext">The Asset Loader Context reference. Asset Loader Context contains the information used during the Model loading process, which is available to almost every Model processing method</param>
  36. protected override void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
  37. {
  38. base.OnMaterialsLoad(assetLoaderContext);
  39. if (assetLoaderContext.RootGameObject != null)
  40. {
  41. var existingInnerAvatar = AvatarController.Instance.InnerAvatar;
  42. if (existingInnerAvatar != null)
  43. {
  44. Destroy(existingInnerAvatar);
  45. }
  46. var controller = AvatarController.Instance.Animator.runtimeAnimatorController;
  47. var bounds = assetLoaderContext.RootGameObject.CalculateBounds();
  48. var factor = AvatarController.Instance.CharacterController.height / bounds.size.y;
  49. assetLoaderContext.RootGameObject.transform.localScale = factor * Vector3.one;
  50. AvatarController.Instance.InnerAvatar = assetLoaderContext.RootGameObject;
  51. assetLoaderContext.RootGameObject.transform.SetParent(AvatarController.Instance.transform, false);
  52. AvatarController.Instance.Animator = assetLoaderContext.RootGameObject.GetComponent<Animator>();
  53. AvatarController.Instance.Animator.runtimeAnimatorController = controller;
  54. }
  55. }
  56. /// <summary>Checks if the Dispatcher instance exists and stores this class instance as the Singleton and adjusts avatar size.</summary>
  57. protected override void Start()
  58. {
  59. base.Start();
  60. AssetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
  61. AssetLoaderOptions.AnimationType = AnimationType.Humanoid;
  62. AssetLoaderOptions.HumanoidAvatarMapper = Resources.Load<HumanoidAvatarMapper>("Mappers/Avatar/MixamoAndBipedByNameHumanoidAvatarMapper");
  63. var bounds = AvatarController.Instance.InnerAvatar.CalculateBounds();
  64. var factor = AvatarController.Instance.CharacterController.height / bounds.size.y;
  65. AvatarController.Instance.InnerAvatar.transform.localScale = factor * Vector3.one;
  66. }
  67. /// <summary>
  68. /// Handles the input.
  69. /// </summary>
  70. private void Update()
  71. {
  72. if (Input.GetMouseButtonDown(1))
  73. {
  74. Cursor.lockState = Cursor.lockState == CursorLockMode.None ? CursorLockMode.Locked : CursorLockMode.None;
  75. }
  76. if (Cursor.lockState == CursorLockMode.Locked)
  77. {
  78. UpdateCamera();
  79. }
  80. }
  81. }
  82. }