/TinyAssetLoader

Asset Loader for Unity Tiny

Primary LanguageC#MIT LicenseMIT

Tiny Asset Loader

This package intends to allow loading any file from an external location into the Unity Tiny runtime.

Getting Started

Dependencies

  • Requires Unity 2020.1.17f1 and will automatically import Tiny package 0.32.0-preview.54

Installing

  • Go to package manager and use Add package from git url to add this package. Alternatively clone this repo and use Add package from Disk.

Using this package

  • 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 the AssetState until it is on a Loaded state. Then we can read the actual data from AssetBuffer 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();
    }
  }

Authors

Contributors names and contact info

Gilberto Catarino Linkedin LinkedIn

Version History

  • 0.1
    • Initial Release

Notes

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 XMLHttpRequestin the underlying plugin.