#pragma warning disable 649 using TriLibCore.Extensions; using TriLibCore.General; using TriLibCore.Mappers; using UnityEngine; namespace TriLibCore.Samples { /// Represents a TriLib sample which allows the user to load and control a custom avatar. public class AvatarLoader : AssetViewerBase { /// /// Game object that is used to hide the model while it is loading. /// [SerializeField] private GameObject _wrapper; /// /// Mapper to be used when loading the Avatar. /// [SerializeField] private HumanoidAvatarMapper _humanoidAvatarMapper; /// /// Shows the file picker so the user can load an avatar from the local file system. /// public void LoadAvatarFromFile() { LoadModelFromFile(_wrapper); } /// Event triggered when the Model (including Textures and Materials) has been fully loaded. /// The Asset Loader Context reference. Asset Loader Context contains the Model loading data. protected override void OnMaterialsLoad(AssetLoaderContext assetLoaderContext) { base.OnMaterialsLoad(assetLoaderContext); if (assetLoaderContext.RootGameObject != null) { var existingInnerAvatar = AvatarController.Instance.InnerAvatar; if (existingInnerAvatar != null) { Destroy(existingInnerAvatar); } var controller = AvatarController.Instance.Animator.runtimeAnimatorController; var bounds = assetLoaderContext.RootGameObject.CalculateBounds(); var factor = AvatarController.Instance.CharacterController.height / bounds.size.y; assetLoaderContext.RootGameObject.transform.localScale = factor * Vector3.one; AvatarController.Instance.InnerAvatar = assetLoaderContext.RootGameObject; assetLoaderContext.RootGameObject.transform.SetParent(AvatarController.Instance.transform, false); AvatarController.Instance.Animator = assetLoaderContext.RootGameObject.GetComponent(); AvatarController.Instance.Animator.runtimeAnimatorController = controller; } } /// Configures the avatar loading and adjusts avatar size factor based on the existing avatar. protected override void Start() { base.Start(); if (AssetLoaderOptions == null) { AssetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(false, true); AssetLoaderOptions.AnimationType = AnimationType.Humanoid; AssetLoaderOptions.HumanoidAvatarMapper = _humanoidAvatarMapper; } var bounds = AvatarController.Instance.InnerAvatar.CalculateBounds(); var factor = AvatarController.Instance.CharacterController.height / bounds.size.y; AvatarController.Instance.InnerAvatar.transform.localScale = factor * Vector3.one; } /// /// Handles the input. /// private void Update() { if (GetMouseButtonDown(1)) { Cursor.lockState = Cursor.lockState == CursorLockMode.None ? CursorLockMode.Locked : CursorLockMode.None; } if (Cursor.lockState == CursorLockMode.Locked) { UpdateCamera(); } } } }