This package intends to allow loading any file from an external location into the Unity Tiny runtime.
- Requires Unity 2020.1.17f1 and will automatically import Tiny package 0.32.0-preview.54
- Go to package manager and use
Add package from git url
to add this package. Alternatively clone this repo and useAdd package from Disk
.
- Create and entity and add component
AssetFilename
with the path to the file we want to load.
EntityManager.AddBufferFromString<AssetFilename>(EntityManager.CreateEntity(), "path/myfile.bin");
- How to use loaded data
After adding
AssetFilename
we must listen to theAssetState
until it is on aLoaded
state. Then we can read the actual data fromAssetBuffer
dynamic buffer.
Here is ans example system that will handle the loading of a text asset file:
public class LoadTextAssetSystem : SystemBase {
protected override void OnUpdate() {
Entities
.WithStructuralChanges()
.ForEach((Entity entity, DynamicBuffer<AssetBuffer> buffer, in AssetState state) => {
switch (state.Status) {
case AssetStatus.LoadError:
case AssetStatus.Invalid:
Debug.Log("Something wrong happendd while loading the asset");
break;
case AssetStatus.Loaded:
// Convert bytes to text
var textData = Encoding.UTF8.GetString(buffer.Reinterpret<byte>().AsArray());
Debug.Log($"Loaded text file with content size of {textData.Length} characters.");
// Do something with the text such as parsing JSON if it's an actual JSON string
break;
case AssetStatus.Loading:
return;
}
// Dispose of the entity and the buffer alongside with it
EntityManager.DestroyEntity(entity);
}).Run();
}
}
Contributors names and contact info
Gilberto Catarino LinkedIn
- 0.1
- Initial Release
This was tested on Windows, Mac and WebGL. iOS and Android might work but can't confirm right now.
External asset loading only loads local files in the desktop versions but in WebGL any remote file can be loaded because it uses XMLHttpRequest
in the underlying plugin.