Skip to content

Instantly share code, notes, and snippets.

@sassembla
Created April 6, 2018 07:53
Show Gist options
  • Save sassembla/2a42d8e8bae0146e3d236fb8337f3830 to your computer and use it in GitHub Desktop.
Save sassembla/2a42d8e8bae0146e3d236fb8337f3830 to your computer and use it in GitHub Desktop.
ResourcesController for Autoya.
using System;
using System.Collections;
using AutoyaFramework;
using UnityEngine;
public class ResourcesController
{
public static void LoadAsset<T>(string assetPath, Action<string, T> succeeded, Action<string, int, string, object> failed) where T : UnityEngine.Object
{
var resRequest = Resources.LoadAsync(assetPath);
var cor = RequestCoroutine(assetPath, resRequest, succeeded, failed);
Autoya.Mainthread_Commit(cor);
}
private static IEnumerator RequestCoroutine<T>(string assetPath, ResourceRequest req, Action<string, T> succeeded, Action<string, int, string, object> failed) where T : UnityEngine.Object
{
while (!req.isDone)
{
yield return null;
}
if (req.asset == null)
{
failed(assetPath, -1, "failed to find asset.", new object());
yield break;
}
// req.asset is not null.
Debug.Log("req.asset:" + req.asset);
var casted = req.asset as T;
if (casted == null)
{
failed(assetPath, -2, "failed to cast asset to required type.", new object());
yield break;
}
succeeded(assetPath, casted);
}
}
@sassembla
Copy link
Author

usage

ResourcesController.LoadAsset<TextAsset>("Views/Default/UUebTags",
            (assetName, asset) =>
            {
                Debug.Log("成功 asset:" + asset);
            },
            (assetName, errorCode, reason, autoyaStatus) =>
            {
                Debug.Log("失敗 errorCode:" + errorCode + " reason:" + reason);
            }
        );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment