Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GetAssetPreview on prefab loaded by AssetBundle

Discussion in 'Getting Started' started by powereborn, Nov 6, 2021.

  1. powereborn


    Dec 22, 2018

    I recently tried the AssetBundle, I managed to save my asset bundle on my web server and then load it from Unity.
    I have a Map Editor working with gizmos and I was trying to load prefabs from the asset bundle so I can place the prefabs on the scene.
    However, after going from resources directory to Asset bundle mode, it seems some functions don't work any more.

    Here is what I did:

    Code (CSharp):
    1. var request = UnityWebRequestAssetBundle.GetAssetBundle(url, hashDecoded, 0);
    2.         yield return request.SendWebRequest();
    4. Bundle bundle = DownloadHandlerAssetBundle.GetContent(request);
    6. GameObject tile = bundle.LoadAsset<GameObject>("myobject"); // The object is well loaded
    8. //Instantiate(tile) only this one works
    10. Texture2D img = AssetPreview.GetAssetPreview(tile); // Now Fails
    12. GameObject preview = PrefabUtility.InstantiatePrefab(tile) as GameObject; // Fails as well
    I cannot get an asset preview from the gameobject loaded from Asset bundle and cannot use InstantiatePrefab any more.

    It was working well with Resources directory, the problem is that I cannot keep all my game prefabs in this directory since it would fall into the Build as soon as I build the project.

    Could you guide me ?