Tanoda
SimpleCustomAssetLoader.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using TriLibCore.Interfaces;
5using TriLibCore.Utils;
6using UnityEngine;
7
8namespace TriLibCore.Samples
9{
13 public class SimpleExternalDataMapper : ExternalDataMapper
14 {
15 private Func<string, Stream> _streamReceivingCallback;
16 private Func<string, string> _finalPathReceivingCallback;
17
18 public void Setup(Func<string, Stream> streamReceivingCallback, Func<string, string> finalPathReceivingCallback)
19 {
20 if (streamReceivingCallback == null) throw new Exception("Callback parameter is missing.");
21 _streamReceivingCallback = streamReceivingCallback;
22 _finalPathReceivingCallback = finalPathReceivingCallback;
23 }
24
25 public override Stream Map(AssetLoaderContext assetLoaderContext, string originalFilename, out string finalPath)
26 {
27 finalPath = _finalPathReceivingCallback != null
28 ? _finalPathReceivingCallback(originalFilename)
29 : originalFilename;
30 return _streamReceivingCallback(originalFilename);
31 }
32 }
33
37 public class SimpleTextureMapper : TextureMapper
38 {
39 private Func<ITexture, Stream> _streamReceivingCallback;
40
41 public void Setup(Func<ITexture, Stream> streamReceivingCallback)
42 {
43 if (streamReceivingCallback == null) throw new Exception("Callback parameter is missing.");
44 _streamReceivingCallback = streamReceivingCallback;
45 }
46
47 public override TextureLoadingContext Map(AssetLoaderContext assetLoaderContext, ITexture texture)
48 {
49 var stream = _streamReceivingCallback(texture);
50 return new TextureLoadingContext
51 {
52 Context = assetLoaderContext,
53 Stream = stream,
54 Texture = texture
55 };
56 }
57 }
58
63 {
86 public static AssetLoaderContext LoadModelFromByteData(
87 byte[] data,
88 string modelExtension,
89 Action<IContextualizedError> onError,
90 Action<AssetLoaderContext, float> onProgress,
91 Action<AssetLoaderContext> onModelFullyLoad,
92 Func<string, Stream> customDataReceivingCallback,
93 Func<string, string> customFilenameReceivingCallback,
94 Func<ITexture, Stream> customTextureReceivingCallback,
95 string modelFilename = null,
96 GameObject wrapperGameObject = null,
97 AssetLoaderOptions assetLoaderOptions = null,
98 object customData = null)
99 {
100 if (data == null || data.Length == 0) throw new Exception("Missing model file byte data.");
101 return LoadModelFromStream(new MemoryStream(data), modelExtension, onError, onProgress, onModelFullyLoad,
102 customDataReceivingCallback, customFilenameReceivingCallback, customTextureReceivingCallback,
103 modelFilename, wrapperGameObject, assetLoaderOptions, customData);
104 }
105
128 public static AssetLoaderContext LoadModelFromStream(
129 Stream stream,
130 string modelExtension,
131 Action<IContextualizedError> onError,
132 Action<AssetLoaderContext, float> onProgress,
133 Action<AssetLoaderContext> onModelFullyLoad,
134 Func<string, Stream> customDataReceivingCallback,
135 Func<string, string> customFilenameReceivingCallback,
136 Func<ITexture, Stream> customTextureReceivingCallback,
137 string modelFilename = null,
138 GameObject wrapperGameObject = null,
139 AssetLoaderOptions assetLoaderOptions = null,
140 object customData = null)
141 {
142 if (stream == null) throw new Exception("Missing model file byte data.");
143 if (string.IsNullOrWhiteSpace(modelExtension) && !string.IsNullOrWhiteSpace(modelFilename))
144 modelExtension = FileUtils.GetFileExtension(modelFilename);
145 if (string.IsNullOrWhiteSpace(modelExtension)) throw new Exception("Missing model extension parameter");
146 var simpleExternalDataMapper = ScriptableObject.CreateInstance<SimpleExternalDataMapper>();
147 simpleExternalDataMapper.Setup(customDataReceivingCallback, customFilenameReceivingCallback);
148 var simpleTextureMapper = ScriptableObject.CreateInstance<SimpleTextureMapper>();
149 simpleTextureMapper.Setup(customTextureReceivingCallback);
150 assetLoaderOptions = assetLoaderOptions ?? AssetLoader.CreateDefaultLoaderOptions();
151 assetLoaderOptions.ExternalDataMapper = simpleExternalDataMapper;
152 assetLoaderOptions.TextureMapper = simpleTextureMapper;
153 return AssetLoader.LoadModelFromStream(stream, modelFilename, modelExtension, null, onModelFullyLoad,
154 onProgress, onError, wrapperGameObject, assetLoaderOptions, customData);
155 }
156 }
157}
TriLibCore.AssetLoaderOptions AssetLoaderOptions
Definition: TriLibLoader.cs:9
Represents a class used to load models from Byte Arrays using callbacks to map External Data and Text...
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)
Loads a model from the given Byte Array data using the given callbacks to handle events/external data...
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)
Loads a model from the given Byte Array data using the given callbacks to handle events/external data...
Custom external data mapper which works with callbacks.
override Stream Map(AssetLoaderContext assetLoaderContext, string originalFilename, out string finalPath)
void Setup(Func< string, Stream > streamReceivingCallback, Func< string, string > finalPathReceivingCallback)
Custom texture mapper which works with callbacks.
override TextureLoadingContext Map(AssetLoaderContext assetLoaderContext, ITexture texture)
void Setup(Func< ITexture, Stream > streamReceivingCallback)