123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #pragma warning disable 672
- using System;
- using System.IO;
- using TriLibCore.Interfaces;
- using TriLibCore.Mappers;
- using TriLibCore.Utils;
- using UnityEngine;
- namespace TriLibCore.Samples
- {
-
-
-
- public class SimpleExternalDataMapper : ExternalDataMapper
- {
- private Func<string, Stream> _streamReceivingCallback;
- private Func<string, string> _finalPathReceivingCallback;
- public void Setup(Func<string, Stream> streamReceivingCallback, Func<string, string> finalPathReceivingCallback)
- {
- if (streamReceivingCallback == null)
- {
- throw new Exception("Callback parameter is missing.");
- }
- _streamReceivingCallback = streamReceivingCallback;
- _finalPathReceivingCallback = finalPathReceivingCallback;
- }
- public override Stream Map(AssetLoaderContext assetLoaderContext, string originalFilename, out string finalPath)
- {
- finalPath = _finalPathReceivingCallback != null ? _finalPathReceivingCallback(originalFilename) : originalFilename;
- return _streamReceivingCallback(originalFilename);
- }
- }
-
-
-
- public class SimpleTextureMapper : TextureMapper
- {
- private Func<ITexture, Stream> _streamReceivingCallback;
- public void Setup(Func<ITexture, Stream> streamReceivingCallback)
- {
- if (streamReceivingCallback == null)
- {
- throw new Exception("Callback parameter is missing.");
- }
- _streamReceivingCallback = streamReceivingCallback;
- }
- public override void Map(TextureLoadingContext textureLoadingContext)
- {
- var stream = _streamReceivingCallback(textureLoadingContext.Texture);
- textureLoadingContext.Stream = stream;
- }
- }
-
-
-
- public class SimpleCustomAssetLoader
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static AssetLoaderContext LoadModelFromByteData(
- byte[] data,
- string modelExtension,
- Action<IContextualizedError> onError,
- Action<AssetLoaderContext, float> onProgress,
- Action<AssetLoaderContext> onModelFullyLoad,
- Func<string, Stream> customDataReceivingCallback,
- Func<string, string> customFilenameReceivingCallback,
- Func<ITexture, Stream> customTextureReceivingCallback,
- string modelFilename = null,
- GameObject wrapperGameObject = null,
- AssetLoaderOptions assetLoaderOptions = null,
- object customData = null)
- {
- if (data == null || data.Length == 0)
- {
- throw new Exception("Missing model file byte data.");
- }
- return LoadModelFromStream(new MemoryStream(data), modelExtension, onError, onProgress, onModelFullyLoad, customDataReceivingCallback, customFilenameReceivingCallback, customTextureReceivingCallback, modelFilename, wrapperGameObject, assetLoaderOptions, customData);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static AssetLoaderContext LoadModelFromStream(
- Stream stream,
- string modelExtension,
- Action<IContextualizedError> onError,
- Action<AssetLoaderContext, float> onProgress,
- Action<AssetLoaderContext> onModelFullyLoad,
- Func<string, Stream> customDataReceivingCallback,
- Func<string, string> customFilenameReceivingCallback,
- Func<ITexture, Stream> customTextureReceivingCallback,
- string modelFilename = null,
- GameObject wrapperGameObject = null,
- AssetLoaderOptions assetLoaderOptions = null,
- object customData = null)
- {
- if (stream == null)
- {
- throw new Exception("Missing model file byte data.");
- }
- if (string.IsNullOrWhiteSpace(modelExtension) && !string.IsNullOrWhiteSpace(modelFilename))
- {
- modelExtension = FileUtils.GetFileExtension(modelFilename);
- }
- if (string.IsNullOrWhiteSpace(modelExtension))
- {
- throw new Exception("Missing model extension parameter");
- }
- var simpleExternalDataMapper = ScriptableObject.CreateInstance<SimpleExternalDataMapper>();
- simpleExternalDataMapper.Setup(customDataReceivingCallback, customFilenameReceivingCallback);
- var simpleTextureMapper = ScriptableObject.CreateInstance<SimpleTextureMapper>();
- simpleTextureMapper.Setup(customTextureReceivingCallback);
- if (assetLoaderOptions == null)
- {
- assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
- }
- assetLoaderOptions.ExternalDataMapper = simpleExternalDataMapper;
- assetLoaderOptions.TextureMappers = new TextureMapper[] { simpleTextureMapper};
- return AssetLoader.LoadModelFromStream(stream, modelFilename, modelExtension, null, onModelFullyLoad, onProgress, onError, wrapperGameObject, assetLoaderOptions, customData);
- }
- }
- }
|