AvatarLoader.cs 3.7 KB

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