How can I convert UnityEngine.Object[] to Texture2D[] ?
I am using this script :
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);
}
Conversion of Object[] to Texture2D[] is not working. I got this script from the stackoverflow post -> Download AssetBundle in hard disk
If request.allAssets returns object[] which you know are Texture2D , then you can try:
object[] aObj = request.allAssets;
Texture2D[] aT2D = Array.ConvertAll(aObj, x => (Texture2D) x);