AssetLoaderWindow.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.UI;
  6. namespace TriLib.Samples
  7. {
  8. [RequireComponent(typeof(AssetDownloader))]
  9. public class AssetLoaderWindow : MonoBehaviour
  10. {
  11. public static AssetLoaderWindow Instance { get; private set; }
  12. public void HandleEvent(string animationName)
  13. {
  14. this._rootGameObject.GetComponent<Animation>().Play(animationName);
  15. this._stopAnimationButton.interactable = true;
  16. }
  17. public void DestroyItems()
  18. {
  19. IEnumerator enumerator = this._containerTransform.GetEnumerator();
  20. try
  21. {
  22. while (enumerator.MoveNext())
  23. {
  24. object obj = enumerator.Current;
  25. Transform transform = (Transform)obj;
  26. UnityEngine.Object.Destroy(transform.gameObject);
  27. }
  28. }
  29. finally
  30. {
  31. IDisposable disposable;
  32. if ((disposable = (enumerator as IDisposable)) != null)
  33. {
  34. disposable.Dispose();
  35. }
  36. }
  37. }
  38. protected void Awake()
  39. {
  40. this._loadLocalAssetButton.onClick.AddListener(new UnityAction(this.LoadLocalAssetButtonClick));
  41. this._loadRemoteAssetButton.onClick.AddListener(new UnityAction(this.LoadRemoteAssetButtonClick));
  42. this._stopAnimationButton.onClick.AddListener(new UnityAction(this.StopAnimationButtonClick));
  43. this._resetRotationButton.onClick.AddListener(new UnityAction(this.ResetRotationButtonClick));
  44. this.HideControls();
  45. AssetLoaderWindow.Instance = this;
  46. }
  47. protected void Update()
  48. {
  49. if (this._rootGameObject != null)
  50. {
  51. this._rootGameObject.transform.Rotate((!this._spinXToggle.isOn) ? 0f : (20f * Time.deltaTime), (!this._spinYToggle.isOn) ? 0f : (-20f * Time.deltaTime), 0f, Space.World);
  52. }
  53. }
  54. private void HideControls()
  55. {
  56. this._loadLocalAssetButton.interactable = true;
  57. this._loadRemoteAssetButton.interactable = true;
  58. this._spinningText.gameObject.SetActive(false);
  59. this._spinXToggle.gameObject.SetActive(false);
  60. this._spinYToggle.gameObject.SetActive(false);
  61. this._resetRotationButton.gameObject.SetActive(false);
  62. this._stopAnimationButton.gameObject.SetActive(false);
  63. this._animationsText.gameObject.SetActive(false);
  64. this._animationsScrollRect.gameObject.SetActive(false);
  65. }
  66. private void LoadLocalAssetButtonClick()
  67. {
  68. FileOpenDialog instance = FileOpenDialog.Instance;
  69. instance.Title = "Please select a File";
  70. instance.Filter = AssetLoader.GetSupportedFileExtensions();
  71. instance.ShowFileOpenDialog(delegate(string filename)
  72. {
  73. this.PreLoadSetup();
  74. AssetLoaderOptions assetLoaderOptions = this.GetAssetLoaderOptions();
  75. using (AssetLoader assetLoader = new AssetLoader())
  76. {
  77. assetLoader.OnMetadataProcessed += this.AssetLoader_OnMetadataProcessed;
  78. try
  79. {
  80. this._rootGameObject = assetLoader.LoadFromFile(filename, assetLoaderOptions, null);
  81. }
  82. catch (Exception ex)
  83. {
  84. ErrorDialog.Instance.ShowDialog(ex.ToString());
  85. }
  86. }
  87. if (this._rootGameObject != null)
  88. {
  89. this.PostLoadSetup();
  90. }
  91. });
  92. }
  93. private void AssetLoader_OnMetadataProcessed(AssimpMetadataType metadataType, uint metadataIndex, string metadataKey, object metadataValue)
  94. {
  95. Debug.Log(string.Concat(new object[]
  96. {
  97. "Found metadata of type [",
  98. metadataType,
  99. "] at index [",
  100. metadataIndex,
  101. "] and key [",
  102. metadataKey,
  103. "] with value [",
  104. metadataValue,
  105. "]"
  106. }));
  107. }
  108. private AssetLoaderOptions GetAssetLoaderOptions()
  109. {
  110. AssetLoaderOptions assetLoaderOptions = AssetLoaderOptions.CreateInstance();
  111. assetLoaderOptions.DontLoadCameras = false;
  112. assetLoaderOptions.DontLoadLights = false;
  113. assetLoaderOptions.UseCutoutMaterials = this._cutoutToggle.isOn;
  114. assetLoaderOptions.AddAssetUnloader = true;
  115. return assetLoaderOptions;
  116. }
  117. private void PreLoadSetup()
  118. {
  119. this.HideControls();
  120. if (this._rootGameObject != null)
  121. {
  122. UnityEngine.Object.Destroy(this._rootGameObject);
  123. this._rootGameObject = null;
  124. }
  125. }
  126. private void PostLoadSetup()
  127. {
  128. Camera main = Camera.main;
  129. main.FitToBounds(this._rootGameObject.transform, 3f);
  130. this._backgroundCanvas.planeDistance = main.farClipPlane * 0.99f;
  131. this._spinningText.gameObject.SetActive(true);
  132. this._spinXToggle.isOn = false;
  133. this._spinXToggle.gameObject.SetActive(true);
  134. this._spinYToggle.isOn = false;
  135. this._spinYToggle.gameObject.SetActive(true);
  136. this._resetRotationButton.gameObject.SetActive(true);
  137. this.DestroyItems();
  138. Animation component = this._rootGameObject.GetComponent<Animation>();
  139. if (component != null)
  140. {
  141. this._animationsText.gameObject.SetActive(true);
  142. this._animationsScrollRect.gameObject.SetActive(true);
  143. this._stopAnimationButton.gameObject.SetActive(true);
  144. this._stopAnimationButton.interactable = false;
  145. IEnumerator enumerator = component.GetEnumerator();
  146. try
  147. {
  148. while (enumerator.MoveNext())
  149. {
  150. object obj = enumerator.Current;
  151. AnimationState animationState = (AnimationState)obj;
  152. this.CreateItem(animationState.name);
  153. }
  154. }
  155. finally
  156. {
  157. IDisposable disposable;
  158. if ((disposable = (enumerator as IDisposable)) != null)
  159. {
  160. disposable.Dispose();
  161. }
  162. }
  163. }
  164. }
  165. private void LoadRemoteAssetButtonClick()
  166. {
  167. URIDialog.Instance.ShowDialog(delegate(string assetURI, string assetExtension)
  168. {
  169. AssetDownloader component = base.GetComponent<AssetDownloader>();
  170. component.DownloadAsset(assetURI, assetExtension, new ObjectLoadedHandle(this.LoadDownloadedAsset), null, this.GetAssetLoaderOptions(), null);
  171. this._loadLocalAssetButton.interactable = false;
  172. this._loadRemoteAssetButton.interactable = false;
  173. });
  174. }
  175. private void LoadDownloadedAsset(GameObject loadedGameObject)
  176. {
  177. this.PreLoadSetup();
  178. if (loadedGameObject != null)
  179. {
  180. this._rootGameObject = loadedGameObject;
  181. this.PostLoadSetup();
  182. }
  183. else
  184. {
  185. AssetDownloader component = base.GetComponent<AssetDownloader>();
  186. ErrorDialog.Instance.ShowDialog(component.Error);
  187. }
  188. }
  189. private void CreateItem(string text)
  190. {
  191. AnimationText animationText = UnityEngine.Object.Instantiate<AnimationText>(this._animationTextPrefab, this._containerTransform);
  192. animationText.Text = text;
  193. }
  194. private void ResetRotationButtonClick()
  195. {
  196. this._spinXToggle.isOn = false;
  197. this._spinYToggle.isOn = false;
  198. this._rootGameObject.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
  199. }
  200. private void StopAnimationButtonClick()
  201. {
  202. this._rootGameObject.GetComponent<Animation>().Stop();
  203. this._stopAnimationButton.interactable = false;
  204. }
  205. [SerializeField]
  206. private Button _loadLocalAssetButton;
  207. [SerializeField]
  208. private Button _loadRemoteAssetButton;
  209. [SerializeField]
  210. private Text _spinningText;
  211. [SerializeField]
  212. private Toggle _cutoutToggle;
  213. [SerializeField]
  214. private Toggle _spinXToggle;
  215. [SerializeField]
  216. private Toggle _spinYToggle;
  217. [SerializeField]
  218. private Button _resetRotationButton;
  219. [SerializeField]
  220. private Button _stopAnimationButton;
  221. [SerializeField]
  222. private Text _animationsText;
  223. [SerializeField]
  224. private ScrollRect _animationsScrollRect;
  225. [SerializeField]
  226. private Transform _containerTransform;
  227. [SerializeField]
  228. private AnimationText _animationTextPrefab;
  229. [SerializeField]
  230. private Canvas _backgroundCanvas;
  231. private GameObject _rootGameObject;
  232. }
  233. }