TriLibModelLoad.cs 3.5 KB

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