AvatarLoaderSample.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using UnityEngine;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEngine.UI;
  6. namespace TriLib.Extras
  7. {
  8. /// <summary>
  9. /// Represents the avatar loader sample scene.
  10. /// </summary>
  11. public class AvatarLoaderSample : MonoBehaviour
  12. {
  13. /// <summary>
  14. /// Use this field to specity the Standard Assets FreeLookCam Prefab.
  15. /// </summary>
  16. public GameObject FreeLookCamPrefab;
  17. /// <summary>
  18. /// Use this field to specity the Standard Assets ThirdPersonController Prefab.
  19. /// </summary>
  20. public GameObject ThirdPersonControllerPrefab;
  21. /// <summary>
  22. /// Active Camera Game Object reference.
  23. /// </summary>
  24. public GameObject ActiveCameraGameObject;
  25. /// <summary>
  26. /// Use this field to specify your models directory within the current Application directory.
  27. /// </summary>
  28. public string ModelsDirectory = "Models";
  29. /// <summary>
  30. /// Use this field to specify the <see cref="UnityEngine.UI.Text"/> component where the sample information is displayed.
  31. /// </summary>
  32. public Text InformationText;
  33. /// <summary>
  34. /// Available avatar files list.
  35. /// </summary>
  36. private string[] _files;
  37. /// <summary>
  38. /// UI Window area.
  39. /// </summary>
  40. private Rect _windowRect;
  41. /// <summary>
  42. /// UI scroll position.
  43. /// </summary>
  44. private Vector3 _scrollPosition;
  45. /// <summary>
  46. /// Avatar Loader script reference.
  47. /// </summary>
  48. private AvatarLoader _avatarLoader;
  49. /// <summary>
  50. /// Setups the Avatar Loader instance reference and fills the available files list.
  51. /// </summary>
  52. protected void Start()
  53. {
  54. _avatarLoader = FindObjectOfType<AvatarLoader>();
  55. if (_avatarLoader == null)
  56. {
  57. Debug.LogError("Could not find any Avatar Loader script instance.");
  58. return;
  59. }
  60. #if UNITY_EDITOR
  61. var modelsPath = string.Format("{0}/Samples/{1}", TriLibProjectUtils.FindPathRelativeToProject("TriLibExtras", "t:DefaultAsset TriLibExtras"), ModelsDirectory);
  62. #else
  63. var modelsPath = Path.Combine(Path.GetFullPath("."), ModelsDirectory);
  64. #endif
  65. var supportedExtensions = AssetLoaderBase.GetSupportedFileExtensions();
  66. _files = Directory.GetFiles(modelsPath, "*.*").Where(x => supportedExtensions.Contains("*" + FileUtils.GetFileExtension(x) + ";")).ToArray();
  67. _windowRect = new Rect(20, 20, 240, Screen.height - 40);
  68. InformationText.text = string.Format(InformationText.text, ModelsDirectory, modelsPath);
  69. }
  70. /// <summary>
  71. /// Shows available files and let user select them from the UI.
  72. /// </summary>
  73. protected void OnGUI()
  74. {
  75. if (_files == null || _avatarLoader == null || FreeLookCamPrefab == null || ThirdPersonControllerPrefab == null)
  76. {
  77. return;
  78. }
  79. _windowRect = GUI.Window(0, _windowRect, HandleWindowFunction, "Available Models");
  80. }
  81. /// <summary>
  82. /// Handles the available files UI Window.
  83. /// </summary>
  84. /// <param name="id">Window identifier.</param>
  85. private void HandleWindowFunction(int id)
  86. {
  87. GUILayout.BeginVertical();
  88. _scrollPosition = GUILayout.BeginScrollView(_scrollPosition);
  89. foreach (var file in _files)
  90. {
  91. if (GUILayout.Button(Path.GetFileName(file)))
  92. {
  93. LoadFile(file);
  94. }
  95. }
  96. GUILayout.EndScrollView();
  97. GUILayout.EndVertical();
  98. }
  99. private void LoadFile(string file)
  100. {
  101. var thirdPersonController = Instantiate(ThirdPersonControllerPrefab);
  102. thirdPersonController.transform.DestroyChildren(true);
  103. if (_avatarLoader.LoadAvatar(file, thirdPersonController))
  104. {
  105. if (ActiveCameraGameObject != null)
  106. {
  107. Destroy(ActiveCameraGameObject.gameObject);
  108. }
  109. ActiveCameraGameObject = Instantiate(FreeLookCamPrefab);
  110. }
  111. else
  112. {
  113. if (ActiveCameraGameObject != null)
  114. {
  115. Destroy(ActiveCameraGameObject.gameObject);
  116. }
  117. Destroy(thirdPersonController);
  118. }
  119. }
  120. }
  121. }