LoadModelFromURLSample.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using TriLibCore.General;
  2. using UnityEngine;
  3. namespace TriLibCore.Samples
  4. {
  5. /// <summary>
  6. /// Represents a sample that loads a compressed (Zipped) Model.
  7. /// </summary>
  8. public class LoadModelFromURLSample : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// Creates the AssetLoaderOptions instance, configures the Web Request, and downloads the Model.
  12. /// </summary>
  13. /// <remarks>
  14. /// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions".
  15. /// </remarks>
  16. private void Start()
  17. {
  18. var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
  19. var webRequest = AssetDownloader.CreateWebRequest("https://ricardoreis.net/trilib/demos/sample/TriLibSampleModel.zip");
  20. AssetDownloader.LoadModelFromUri(webRequest, OnLoad, OnMaterialsLoad, OnProgress, OnError, null, assetLoaderOptions);
  21. }
  22. /// <summary>
  23. /// Called when any error occurs.
  24. /// </summary>
  25. /// <param name="obj">The contextualized error, containing the original exception and the context passed to the method where the error was thrown.</param>
  26. private void OnError(IContextualizedError obj)
  27. {
  28. Debug.LogError($"An error ocurred while loading your Model: {obj.GetInnerException()}");
  29. }
  30. /// <summary>
  31. /// Called when the Model loading progress changes.
  32. /// </summary>
  33. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  34. /// <param name="progress">The loading progress.</param>
  35. private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
  36. {
  37. Debug.Log($"Loading Model. Progress: {progress:P}");
  38. }
  39. /// <summary>
  40. /// Called when the Model (including Textures and Materials) has been fully loaded, or after any error occurs.
  41. /// </summary>
  42. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  43. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  44. private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
  45. {
  46. Debug.Log("Materials loaded. Model fully loaded.");
  47. }
  48. /// <summary>
  49. /// Called when the Model Meshes and hierarchy are loaded.
  50. /// </summary>
  51. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  52. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  53. private void OnLoad(AssetLoaderContext assetLoaderContext)
  54. {
  55. Debug.Log("Model loaded. Loading materials.");
  56. }
  57. }
  58. }