using System; using System.Collections; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; namespace TriLib.Samples { [RequireComponent(typeof(AssetDownloader))] public class AssetLoaderWindow : MonoBehaviour { public static AssetLoaderWindow Instance { get; private set; } public void HandleEvent(string animationName) { this._rootGameObject.GetComponent().Play(animationName); this._stopAnimationButton.interactable = true; } public void DestroyItems() { IEnumerator enumerator = this._containerTransform.GetEnumerator(); try { while (enumerator.MoveNext()) { object obj = enumerator.Current; Transform transform = (Transform)obj; UnityEngine.Object.Destroy(transform.gameObject); } } finally { IDisposable disposable; if ((disposable = (enumerator as IDisposable)) != null) { disposable.Dispose(); } } } protected void Awake() { this._loadLocalAssetButton.onClick.AddListener(new UnityAction(this.LoadLocalAssetButtonClick)); this._loadRemoteAssetButton.onClick.AddListener(new UnityAction(this.LoadRemoteAssetButtonClick)); this._stopAnimationButton.onClick.AddListener(new UnityAction(this.StopAnimationButtonClick)); this._resetRotationButton.onClick.AddListener(new UnityAction(this.ResetRotationButtonClick)); this.HideControls(); AssetLoaderWindow.Instance = this; } protected void Update() { if (this._rootGameObject != null) { this._rootGameObject.transform.Rotate((!this._spinXToggle.isOn) ? 0f : (20f * Time.deltaTime), (!this._spinYToggle.isOn) ? 0f : (-20f * Time.deltaTime), 0f, Space.World); } } private void HideControls() { this._loadLocalAssetButton.interactable = true; this._loadRemoteAssetButton.interactable = true; this._spinningText.gameObject.SetActive(false); this._spinXToggle.gameObject.SetActive(false); this._spinYToggle.gameObject.SetActive(false); this._resetRotationButton.gameObject.SetActive(false); this._stopAnimationButton.gameObject.SetActive(false); this._animationsText.gameObject.SetActive(false); this._animationsScrollRect.gameObject.SetActive(false); } private void LoadLocalAssetButtonClick() { FileOpenDialog instance = FileOpenDialog.Instance; instance.Title = "Please select a File"; instance.Filter = AssetLoader.GetSupportedFileExtensions(); instance.ShowFileOpenDialog(delegate(string filename) { this.PreLoadSetup(); AssetLoaderOptions assetLoaderOptions = this.GetAssetLoaderOptions(); using (AssetLoader assetLoader = new AssetLoader()) { assetLoader.OnMetadataProcessed += this.AssetLoader_OnMetadataProcessed; try { this._rootGameObject = assetLoader.LoadFromFile(filename, assetLoaderOptions, null); } catch (Exception ex) { ErrorDialog.Instance.ShowDialog(ex.ToString()); } } if (this._rootGameObject != null) { this.PostLoadSetup(); } }); } private void AssetLoader_OnMetadataProcessed(AssimpMetadataType metadataType, uint metadataIndex, string metadataKey, object metadataValue) { Debug.Log(string.Concat(new object[] { "Found metadata of type [", metadataType, "] at index [", metadataIndex, "] and key [", metadataKey, "] with value [", metadataValue, "]" })); } private AssetLoaderOptions GetAssetLoaderOptions() { AssetLoaderOptions assetLoaderOptions = AssetLoaderOptions.CreateInstance(); assetLoaderOptions.DontLoadCameras = false; assetLoaderOptions.DontLoadLights = false; assetLoaderOptions.UseCutoutMaterials = this._cutoutToggle.isOn; assetLoaderOptions.AddAssetUnloader = true; return assetLoaderOptions; } private void PreLoadSetup() { this.HideControls(); if (this._rootGameObject != null) { UnityEngine.Object.Destroy(this._rootGameObject); this._rootGameObject = null; } } private void PostLoadSetup() { Camera main = Camera.main; main.FitToBounds(this._rootGameObject.transform, 3f); this._backgroundCanvas.planeDistance = main.farClipPlane * 0.99f; this._spinningText.gameObject.SetActive(true); this._spinXToggle.isOn = false; this._spinXToggle.gameObject.SetActive(true); this._spinYToggle.isOn = false; this._spinYToggle.gameObject.SetActive(true); this._resetRotationButton.gameObject.SetActive(true); this.DestroyItems(); Animation component = this._rootGameObject.GetComponent(); if (component != null) { this._animationsText.gameObject.SetActive(true); this._animationsScrollRect.gameObject.SetActive(true); this._stopAnimationButton.gameObject.SetActive(true); this._stopAnimationButton.interactable = false; IEnumerator enumerator = component.GetEnumerator(); try { while (enumerator.MoveNext()) { object obj = enumerator.Current; AnimationState animationState = (AnimationState)obj; this.CreateItem(animationState.name); } } finally { IDisposable disposable; if ((disposable = (enumerator as IDisposable)) != null) { disposable.Dispose(); } } } } private void LoadRemoteAssetButtonClick() { URIDialog.Instance.ShowDialog(delegate(string assetURI, string assetExtension) { AssetDownloader component = base.GetComponent(); component.DownloadAsset(assetURI, assetExtension, new ObjectLoadedHandle(this.LoadDownloadedAsset), null, this.GetAssetLoaderOptions(), null); this._loadLocalAssetButton.interactable = false; this._loadRemoteAssetButton.interactable = false; }); } private void LoadDownloadedAsset(GameObject loadedGameObject) { this.PreLoadSetup(); if (loadedGameObject != null) { this._rootGameObject = loadedGameObject; this.PostLoadSetup(); } else { AssetDownloader component = base.GetComponent(); ErrorDialog.Instance.ShowDialog(component.Error); } } private void CreateItem(string text) { AnimationText animationText = UnityEngine.Object.Instantiate(this._animationTextPrefab, this._containerTransform); animationText.Text = text; } private void ResetRotationButtonClick() { this._spinXToggle.isOn = false; this._spinYToggle.isOn = false; this._rootGameObject.transform.rotation = Quaternion.Euler(0f, 0f, 0f); } private void StopAnimationButtonClick() { this._rootGameObject.GetComponent().Stop(); this._stopAnimationButton.interactable = false; } [SerializeField] private Button _loadLocalAssetButton; [SerializeField] private Button _loadRemoteAssetButton; [SerializeField] private Text _spinningText; [SerializeField] private Toggle _cutoutToggle; [SerializeField] private Toggle _spinXToggle; [SerializeField] private Toggle _spinYToggle; [SerializeField] private Button _resetRotationButton; [SerializeField] private Button _stopAnimationButton; [SerializeField] private Text _animationsText; [SerializeField] private ScrollRect _animationsScrollRect; [SerializeField] private Transform _containerTransform; [SerializeField] private AnimationText _animationTextPrefab; [SerializeField] private Canvas _backgroundCanvas; private GameObject _rootGameObject; } }