首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将包含Texture2D的Texture2D转换为Texture2 D[]

如何将包含Texture2D的Texture2D转换为Texture2 D[]
EN

Stack Overflow用户
提问于 2021-08-11 18:40:48
回答 1查看 93关注 0票数 0

如何将UnityEngine.Object[]转换为Texture2 D[]?

我正在使用以下脚本:

代码语言:javascript
复制
    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的堆栈溢出帖子中获得了这个脚本

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-11 19:06:33

如果request.allAssets返回您知道是Texture2D的object[],那么您可以尝试:

代码语言:javascript
复制
object[] aObj = request.allAssets;
Texture2D[] aT2D = Array.ConvertAll(aObj, x => (Texture2D) x);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68747381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档