如何将UnityEngine.Object[]转换为Texture2 D[]?
我正在使用以下脚本:
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[]到Texture2 D[]的转换不起作用。我从-> Download AssetBundle in hard disk的堆栈溢出帖子中获得了这个脚本
发布于 2021-08-11 19:06:33
如果request.allAssets返回您知道是Texture2D的object[],那么您可以尝试:
object[] aObj = request.allAssets;
Texture2D[] aT2D = Array.ConvertAll(aObj, x => (Texture2D) x);https://stackoverflow.com/questions/68747381
复制相似问题