TriLibLocalLoad.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using TriLibCore;
  3. using TriLibCore.Utils;
  4. using UnityEngine;
  5. public class TriLibLocalLoad
  6. {
  7. /// <summary>
  8. /// Creates the AssetLoaderOptions instance, configures the Web Request, and downloads the Model.
  9. /// </summary>
  10. /// <remarks>
  11. /// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions".
  12. /// </remarks>
  13. public static void Load(string ModelURL, Action<AssetLoaderContext> OnLoad, Action<AssetLoaderContext> OnMaterialsLoad, Action<AssetLoaderContext, float> OnProgress, Action<IContextualizedError> OnError, GameObject go = null, AssetLoaderOptions assetLoaderOptions=null)
  14. {
  15. if (ModelURL.Contains("http"))
  16. {
  17. loadUrl(ModelURL, OnLoad, OnMaterialsLoad, OnProgress, OnError, go, assetLoaderOptions);
  18. }
  19. else
  20. {
  21. loadFile(ModelURL, OnLoad, OnMaterialsLoad, OnProgress, OnError, go, assetLoaderOptions);
  22. }
  23. }
  24. static void loadUrl(string ModelURL, Action<AssetLoaderContext> OnLoad, Action<AssetLoaderContext> OnMaterialsLoad, Action<AssetLoaderContext, float> OnProgress, Action<IContextualizedError> OnError, GameObject go = null, AssetLoaderOptions assetLoaderOptions = null)
  25. {
  26. if(assetLoaderOptions==null)
  27. assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
  28. Debug.Log("assetLoaderOptions==>" + assetLoaderOptions.LoadPointClouds);
  29. var webRequest = AssetDownloader.CreateWebRequest(ModelURL);
  30. AssetDownloader.LoadModelFromUri(webRequest, OnLoad, OnMaterialsLoad, OnProgress, OnError, go, assetLoaderOptions);
  31. }
  32. static void loadFile(string ModelPath, Action<AssetLoaderContext> OnLoad, Action<AssetLoaderContext> OnMaterialsLoad, Action<AssetLoaderContext, float> OnProgress, Action<IContextualizedError> OnError, GameObject go = null, AssetLoaderOptions assetLoaderOptions = null)
  33. {
  34. if (assetLoaderOptions == null)
  35. assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
  36. Debug.Log("assetLoaderOptions==>" + assetLoaderOptions.LoadPointClouds);
  37. AssetLoader.LoadModelFromFile(ModelPath, OnLoad, OnMaterialsLoad, OnProgress, OnError, go, assetLoaderOptions);
  38. }
  39. /// <summary>
  40. /// Called when any error occurs.
  41. /// </summary>
  42. /// <param name="obj">The contextualized error, containing the original exception and the context passed to the method where the error was thrown.</param>
  43. private void OnError(IContextualizedError obj)
  44. {
  45. Debug.LogError($"An error occurred while loading your Model: {obj.GetInnerException()}");
  46. }
  47. /// <summary>
  48. /// Called when the Model loading progress changes.
  49. /// </summary>
  50. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  51. /// <param name="progress">The loading progress.</param>
  52. private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
  53. {
  54. Debug.Log($"Loading Model. Progress: {progress:P}");
  55. }
  56. /// <summary>
  57. /// Called when the Model (including Textures and Materials) has been fully loaded.
  58. /// </summary>
  59. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  60. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  61. private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
  62. {
  63. Debug.Log("Materials loaded. Model fully loaded.");
  64. }
  65. /// <summary>
  66. /// Called when the Model Meshes and hierarchy are loaded.
  67. /// </summary>
  68. /// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
  69. /// <param name="assetLoaderContext">The context used to load the Model.</param>
  70. private void OnLoad(AssetLoaderContext assetLoaderContext)
  71. {
  72. Debug.Log("Model loaded. Loading materials.");
  73. }
  74. }