Search Unity

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

Error Loading Bundles

Discussion in 'Windows' started by denali95, May 16, 2018.

  1. denali95

    denali95

    Joined:
    Nov 6, 2016
    Posts:
    78
    I'm saving an asset bundle like this:
    Code (CSharp):
    1.         BuildPipeline.BuildAssetBundles("Assets/AssetBundles/", BuildAssetBundleOptions.None, BuildTarget.WSAPlayer);
    2.         AssetDatabase.Refresh();
    Then opening it like this:

    Code (CSharp):
    1. IEnumerator getObject()
    2.     {
    3.         WWW www = WWW.LoadFromCacheOrDownload(Application.dataPath + "/AssetBundles/primitives", 1);
    4.         yield return www;
    5.  
    6.         AssetBundle bundle = www.assetBundle;
    7.         AssetBundleRequest request = bundle.LoadAssetAsync<GameObject>("Sphere");
    8.  
    9.         yield return request;
    10.  
    11.         GameObject sphere = request.asset as GameObject;
    12.         Instantiate<GameObject>(sphere);
    13.         print("successful sphere");
    14.  
    15.     }
    I have my assetbundle inside of assets/AssetBundles/primitives. Loading works perfect in the editor, but always returns null when I deploy in uwp. What am I doing wrong?
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,626
    What Unity version are you on? There was a bug in Unity 2017.3 that made WWW requests to fail, but it was fixed in 2017.4.2f1.
     
  3. denali95

    denali95

    Joined:
    Nov 6, 2016
    Posts:
    78
    I'm using 2017.1.0f3 Personal
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,626
  5. denali95

    denali95

    Joined:
    Nov 6, 2016
    Posts:
    78
    I managed to make this work by using the persistentDataPath. If I was using StreamingAssets, would I still be able to import my asset with scripts on it that could be executed at run-time?
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,626
    Are you packaging scripts themselves into the asset bundle, or just objects that have scripts attached?
     
  7. denali95

    denali95

    Joined:
    Nov 6, 2016
    Posts:
    78
    I suppose I could do either, but preferably objects with scripts attached.
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,626
    The latter is easy and streaming assets will definitely work. Packing scripts themselves into a bundle is much more problematic :).
     
  9. denali95

    denali95

    Joined:
    Nov 6, 2016
    Posts:
    78
    Thanks! Is it possible to keep a data structure filled with scripts that I have preloaded, and then when I want to bring in the desired asset, attach them? I seem to be having trouble adding when trying to do this with AddComponent. The context here is that I'm allowing a user to set characteristics in a config file, and then have those be applied to the bundled assets at runtime.
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,626
    I'm having a hard time understanding what you're trying to do. Could you post a code sample?
     
  11. denali95

    denali95

    Joined:
    Nov 6, 2016
    Posts:
    78
    Basically all I'm doing is trying to trying to add a particular script when I load the object.
    Code (CSharp):
    1.     IEnumerator getObject()
    2.     {
    3.         WWW www = WWW.LoadFromCacheOrDownload(Application.persistentDataPath + "/primitives", 1);
    4.         yield return www;
    5.  
    6.         AssetBundle bundle = www.assetBundle;
    7.         AssetBundleRequest request = bundle.LoadAssetAsync<GameObject>("Sphere");
    8.  
    9.         yield return request;
    10.  
    11.         GameObject sphere = request.asset as GameObject;
    12.         ObstacleAttributes attrib = PreProcessor.obstacles["testing"];
    13.         GameObject test = Instantiate<GameObject>(sphere);
    14.         test.AddComponent(attrib);
    15.         Instantiate<GameObject>(sphere, new Vector3(3, 3, 3), Quaternion.identity, g.transform);
    16.         print("successful sphere");
    17.  
    18.     }

    The Script I'm trying to add to my new game object is a list of values assigned elsewhere:
    Code (CSharp):
    1. public class ObstacleAttributes : MonoBehaviour{
    2.  
    3.     public string obj;
    4.     public double origin_lat;
    5.     public double origin_lon;
    6.     public double appear_dist;
    7.     public List<double> checkpoint_lat = new List<double>();
    8.     public List<double> checkpoint_long = new List<double>();
    9.     public double obstacle_speed;
    10. }
     
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,626
    That should work, however, you're spawning 3 copies of the object:

    1. In variable "sphere";
    2. In variable "test" with your added component
    3. In the last call to Instantiate, even though you never store it anywhere.
     
  13. denali95

    denali95

    Joined:
    Nov 6, 2016
    Posts:
    78
    Yeah, I was just trying to do some testing. I'm getting an error where it says ObstacleAttributes must be of type System.type.
     
  14. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,626
    Right, you can't attach existing objects as new components. What you should do is this:

    Code (csharp):
    1. var attributes = test.AddComponent<ObstacleAttributes>();
    2. attributes.obj = attrib.obj;
    3. attributes.origin_lat = attrib.origin_lat;
    4. ....
     
    denali95 likes this.
  15. denali95

    denali95

    Joined:
    Nov 6, 2016
    Posts:
    78
    I had this working quite well with just primitive objects (I tried with a sphere), but when I try to apply the same code to a bundle I've constructed from a model with a script and collider attached, I get this error:

    The AssetBundle could not be loaded because it references scripts that are not compatible with the currently loaded ones. Rebuild the AssetBundle to fix this error.

    Do I need to do something special to make models with scripts work with bundling?
     
  16. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,626
    Did you build them in the same Unity project where the game was built?
     
  17. denali95

    denali95

    Joined:
    Nov 6, 2016
    Posts:
    78
    No, is that a requirement?
     
  18. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,626