LoadModelFromURLSample.cs 3.2 KB

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