Tanoda
AssetViewerBase.cs
Go to the documentation of this file.
1#pragma warning disable 649
2using System;
3using TriLibCore.Utils;
4using UnityEngine;
5using UnityEngine.Networking;
6using UnityEngine.UI;
7
8namespace TriLibCore.Samples
9{
11 public class AssetViewerBase : MonoBehaviour
12 {
14 public static AssetViewerBase Instance { get; private set; }
15
19 [SerializeField] private RectTransform _loadingBar;
20
24 [SerializeField] private GameObject _helpWrapper;
25
29 [SerializeField] private GameObject _loadingWrapper;
30
34 [SerializeField] private GameObject _modelUrlDialog;
35
39 [SerializeField] private InputField _modelUrl;
40
44 [SerializeField] protected Slider PlaybackSlider;
45
49 [SerializeField] protected Text PlaybackTime;
50
54 [SerializeField] protected Dropdown PlaybackAnimation;
55
59 [SerializeField] protected Selectable Play;
60
64 [SerializeField] protected Selectable Stop;
65
70
74 public Vector2 CameraAngle;
75
79 public GameObject RootGameObject { get; protected set; }
80
85 protected const float InputMultiplierRatio = 0.1f;
86
90 protected const float MaxPitch = 80f;
91
93 protected void UpdateCamera()
94 {
95 CameraAngle.x = Mathf.Repeat(CameraAngle.x + Input.GetAxis("Mouse X"), 360f);
96 CameraAngle.y = Mathf.Clamp(CameraAngle.y + Input.GetAxis("Mouse Y"), -MaxPitch, MaxPitch);
97 }
98
102 public void ShowHelp()
103 {
104 _helpWrapper.SetActive(true);
105 }
106
110 public void HideHelp()
111 {
112 _helpWrapper.SetActive(false);
113 }
114
118 public void ShowModelUrlDialog()
119 {
120 _modelUrlDialog.SetActive(true);
121 _modelUrl.Select();
122 _modelUrl.ActivateInputField();
123 }
124
128 public void HideModelUrlDialog()
129 {
130 _modelUrlDialog.SetActive(false);
131 _modelUrl.text = null;
132 }
133
137 protected void LoadModelFromFile(GameObject wrapperGameObject = null,
138 Action<AssetLoaderContext> onMaterialsLoad = null)
139 {
140 SetLoading(false);
141 var filePickerAssetLoader = AssetLoaderFilePicker.Create();
142 filePickerAssetLoader.LoadModelFromFilePickerAsync("Select a File", OnLoad,
144 wrapperGameObject ?? gameObject, AssetLoaderOptions);
145 }
146
148 protected void LoadModelFromURL(UnityWebRequest request, string fileExtension,
149 GameObject wrapperGameObject = null, object customData = null,
150 Action<AssetLoaderContext> onMaterialsLoad = null)
151 {
153 SetLoading(true);
154 OnBeginLoadModel(true);
155 fileExtension = fileExtension.ToLowerInvariant();
156 var isZipFile = fileExtension == "zip" || fileExtension == ".zip";
158 wrapperGameObject, AssetLoaderOptions, customData, isZipFile ? null : fileExtension, isZipFile);
159 }
160
165 {
166 if (string.IsNullOrWhiteSpace(_modelUrl.text)) return;
167 var request = AssetDownloader.CreateWebRequest(_modelUrl.text);
168 var fileExtension =
169 FileUtils.GetFileExtension(request.uri.Segments[request.uri.Segments.Length - 1], false);
170 LoadModelFromURL(request, fileExtension);
171 }
172
175 protected virtual void OnBeginLoadModel(bool hasFiles)
176 {
177 if (hasFiles)
178 {
179 if (RootGameObject != null) Destroy(RootGameObject);
180 SetLoading(true);
181 }
182 }
183
188 protected void SetLoading(bool value)
189 {
190 var selectables = FindObjectsOfType<Selectable>();
191 for (var i = 0; i < selectables.Length; i++)
192 {
193 var button = selectables[i];
194 button.interactable = !value;
195 }
196#if UNITY_WSA || UNITY_WEBGL || TRILIB_FORCE_SYNC
197 _loadingWrapper.gameObject.SetActive(value);
198#else
199 _loadingBar.gameObject.SetActive(value);
200#endif
201 }
202
204 protected virtual void Start()
205 {
206 Dispatcher.CheckInstance();
208 Instance = this;
209 }
210
217 protected virtual void OnProgress(AssetLoaderContext assetLoaderContext, float value)
218 {
219 _loadingBar.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Screen.width * value);
220 }
221
224 protected virtual void OnError(IContextualizedError contextualizedError)
225 {
226 Debug.LogError(contextualizedError);
227 RootGameObject = null;
228 SetLoading(false);
229 }
230
236 protected virtual void OnLoad(AssetLoaderContext assetLoaderContext)
237 {
238 }
239
245 protected virtual void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
246 {
247 SetLoading(false);
248 }
249
253 public virtual void PlayAnimation()
254 {
255 }
256
258 public virtual void StopAnimation()
259 {
260 }
261
264 public virtual void PlaybackAnimationChanged(int index)
265 {
266 }
267
272 public virtual void PlaybackSliderChanged(float value)
273 {
274 }
275 }
276}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
TriLibCore.AssetLoaderOptions AssetLoaderOptions
Definition: TriLibLoader.cs:9
Represents a class to download and load Models.
static Coroutine LoadModelFromUri(UnityWebRequest unityWebRequest, Action< AssetLoaderContext > onLoad, Action< AssetLoaderContext > onMaterialsLoad, Action< AssetLoaderContext, float > onProgress, Action< IContextualizedError > onError=null, GameObject wrapperGameObject=null, AssetLoaderOptions assetLoaderOptions=null, object customContextData=null, string fileExtension=null, bool? isZipFile=null)
Loads a Model from the given URI Asynchronously (Accepts zip files).
static UnityWebRequest CreateWebRequest(string uri, HttpRequestMethod httpRequestMethod=HttpRequestMethod.Get, string data=null, int timeout=2000)
Creates a Unity Web Request from the given parameters.
Represents an Asset Loader which loads files using a platform-specific file picker.
static AssetLoaderFilePicker Create()
Creates the Asset Loader File Picker Singleton instance.
void LoadModelFromFilePickerAsync(string title, Action< AssetLoaderContext > onLoad, Action< AssetLoaderContext > onMaterialsLoad, Action< AssetLoaderContext, float > onProgress, Action< bool > onBeginLoad, Action< IContextualizedError > onError, GameObject wrapperGameObject, AssetLoaderOptions assetLoaderOptions)
Loads a Model from the OS file picker asynchronously, or synchronously when the OS doesn't support Th...
Represents a base class used in TriLib samples.
Text PlaybackTime
Animation playback time.
Vector2 CameraAngle
Current camera pitch and yaw angles.
void ShowModelUrlDialog()
Shows the model URL dialog.
Dropdown PlaybackAnimation
Animation selector.
virtual void StopAnimation()
Stops playing the selected animation.
void HideHelp()
Hides the help box.
void HideModelUrlDialog()
Hides the model URL dialog.
virtual void OnLoad(AssetLoaderContext assetLoaderContext)
Event is triggered when the Model Meshes and hierarchy are loaded.
void SetLoading(bool value)
Enables/disables the loading flag.
void LoadModelFromURL(UnityWebRequest request, string fileExtension, GameObject wrapperGameObject=null, object customData=null, Action< AssetLoaderContext > onMaterialsLoad=null)
Loads a model from a URL.
GameObject RootGameObject
Loaded game object.
virtual void PlayAnimation()
Plays the selected animation.
virtual void OnProgress(AssetLoaderContext assetLoaderContext, float value)
Event is triggered when the Model loading progress changes.
Slider PlaybackSlider
Animation playback slider.
void LoadModelFromFile(GameObject wrapperGameObject=null, Action< AssetLoaderContext > onMaterialsLoad=null)
Shows the file picker for loading a model from local file-system.
static AssetViewerBase Instance
Gets the Asset Viewer Singleton instance.
const float InputMultiplierRatio
Mouse input multiplier. Higher values will make the mouse movement more sensible.
virtual void PlaybackSliderChanged(float value)
Event triggered when the animation slider value has been changed by the user.
void LoadModelFromURLWithDialogValues()
Shows the URL selector for loading a model from network.
virtual void OnBeginLoadModel(bool hasFiles)
Event triggered when the user selects a file or cancels the Model selection dialog.
virtual void Start()
Checks if the Dispatcher instance exists and stores this class instance as the Singleton.
void ShowHelp()
Shows the help box.
AssetLoaderOptions AssetLoaderOptions
Options used in this sample.
virtual void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
Event is triggered when the Model (including Textures and Materials) has been fully loaded.
void UpdateCamera()
Updates the Camera based on mouse Input.
virtual void PlaybackAnimationChanged(int index)
Switches to the animation selected on the Dropdown.
const float MaxPitch
Maximum camera pitch and light pitch (rotation around local X-axis).
virtual void OnError(IContextualizedError contextualizedError)
Event is triggered when any error occurs.
Represents a Class used to add paste capabilities to WebGL projects.
Definition: PasteManager.cs:10
static void CheckInstance()
Checks if the Singleton instance exists.
Definition: PasteManager.cs:20