123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- 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<Animation>().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<Animation>();
- 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<AssetDownloader>();
- 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<AssetDownloader>();
- ErrorDialog.Instance.ShowDialog(component.Error);
- }
- }
- private void CreateItem(string text)
- {
- AnimationText animationText = UnityEngine.Object.Instantiate<AnimationText>(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<Animation>().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;
- }
- }
|