如何转换UnityEngine.对象[]包含Texture2D到Texture2D[]



如何转换UnityEngine。Object[] to Texture2D[] ?

我正在使用这个脚本:

void Start()
{
string dataFileName = "skins";
string tempPath = Path.Combine(Application.persistentDataPath, "AssetData");
tempPath = Path.Combine(tempPath, dataFileName + ".unity3d");
StartCoroutine(LoadObject(tempPath).GetEnumerator());

}
IEnumerable LoadObject(string path)
{
Debug.Log("Loading...");
AssetBundleCreateRequest bundle = AssetBundle.LoadFromFileAsync(path);
yield return bundle;
AssetBundle myLoadedAssetBundle = bundle.assetBundle;
if (myLoadedAssetBundle == null)
{
Debug.Log("Failed to load AssetBundle!");
yield break;
}
AssetBundleRequest request = myLoadedAssetBundle.LoadAllAssetsAsync();
yield return request;

Texture2D[] obj = request.allAssets as Texture2D[]; 
var path2 = Application.persistentDataPath + "/Item Images/";
if (!Directory.Exists(Path.GetDirectoryName(path2)))
{
Directory.CreateDirectory(Path.GetDirectoryName(path2));
}
foreach (Texture2D s in obj)
{
byte[] itemBGBytes = s.EncodeToPNG();
File.WriteAllBytes(Application.persistentDataPath + "/Item Images/" + s.name + ".png", itemBGBytes);
}

myLoadedAssetBundle.Unload(false);
}

Object[]到Texture2D[]的转换不工作。我从stackoverflow post得到了这个脚本->下载硬盘中的AssetBundle

如果请求。allAssets返回对象[],你知道它是Texture2D,然后你可以尝试:

object[] aObj = request.allAssets;
Texture2D[] aT2D = Array.ConvertAll(aObj, x => (Texture2D) x);

最新更新