Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Asset bundles loading inside editor and WebPlayer but not in exe and mobile

Discussion in 'Scripting' started by vahi2008, May 15, 2012.

  1. vahi2008

    vahi2008

    Joined:
    Oct 1, 2010
    Posts:
    321
    My simple code is::

    Hello,

    My asset bundles are loading successfully in my editor and Web Player. But they are not loading in the exe and mobile. Please help me, below is my code to download the assetbundles -

    Code (csharp):
    1.    yo ="http://cookasite.com/14j/unitylogin/meh/inventory.unity3d";
    2.  
    3.     download = new WWW (yo);
    4.  
    5. //      if (url.IndexOf ("file://") == 0 || url.IndexOf ("http://") == 0)
    6.  
    7. //          download = new WWW (yo);
    8.  
    9. //      if (Application.platform == RuntimePlatform.OSXWebPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer)
    10.  
    11. //          //download = new WWW ("../AssetBundles/" + url);
    12.  
    13. //          download = new WWW (yo);
    14.  
    15. //      else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.WindowsEditor)
    16.  
    17. //          //download = new WWW ("file://" + Application.dataPath + "/AssetBundles/" + url);
    18.  
    19. //          download = new WWW (yo);
    20.  
    21.        
    22.  
    23.     yield download;
    24.  
    25.        
    26.  
    27.     assetBundle = download.assetBundle;
    28.  
    29.     download.Dispose();
    30.  
    31.     downlaod = null;
     
  2. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Have you used the correct platform target when building the assetbundle? See the table here.

    See how a webplayer bundle will not work with Android.

    Look at the docs for BuildPipeline.BuildAssetBundle and notice the targetPlatform parameter.
     
  3. vahi2008

    vahi2008

    Joined:
    Oct 1, 2010
    Posts:
    321
    hiii was waiting for for ur help..

    "here" - link is not valid
     
  4. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Last edited: May 16, 2012
  5. vahi2008

    vahi2008

    Joined:
    Oct 1, 2010
    Posts:
    321
    thank you. Its written there

    "For example, a bundle created while the Webplayer build target was active would be compatible with the editor and with standalone builds. However, it would not be compatible with apps built for the iOS or Android platforms."


    http://i.imgur.com/CV6YP.jpg

    Now in this condition, i changed it to web player and closed it and then when i build asset bundles, it stopped even coming in editor and webplayer

    I tested same with standalone, its not loading anywhere now
    I am still checking and update you on this
     
    Last edited: May 16, 2012
  6. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    When I call BuildPipeline.BuildAssetBundle I use BuildTarget.iPhone for the targetPlatform parameter.

    In Build Settings the Platform is set to iOS.

    This builds an AssetBundle which loads on the iOS device and in the editor.
     
  7. vahi2008

    vahi2008

    Joined:
    Oct 1, 2010
    Posts:
    321
    Strange::

    Exe loaded the asset bundles, then i closed it. After say 30 minutes i again opened the exe and this time its not loading the asset bundles

    [i am calling the same asset bundles i made with web player settings,i am able to see them inside editor and web player]

    The table says if assetbundle is made with webplayer, its compatible to standalone, but it just came once and again its not coming in exe.
     
    Last edited: May 16, 2012
  8. vahi2008

    vahi2008

    Joined:
    Oct 1, 2010
    Posts:
    321
    Now i changed the code like this -
    Now issue is , i made the asset bundles from standalone, but still its not loading in exe {inventory-standalone.unity3d}


    Code (csharp):
    1. function loadAssetBundle()
    2. {
    3.  
    4.     yo ="http://cookasite.com/14j/unitylogin/meh/inventory.unity3d";
    5.     yo2 ="http://cookasite.com/14j/unitylogin/meh/inventory-standalone.unity3d";
    6.     //download = new WWW (yo);
    7. //      if (url.IndexOf ("file://") == 0 || url.IndexOf ("http://") == 0)
    8. //          download = new WWW (yo);
    9.         if (Application.platform == RuntimePlatform.OSXWebPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer)
    10. //          //download = new WWW ("../AssetBundles/" + url);
    11.             download = new WWW (yo);
    12.          if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.WindowsEditor)
    13. //          //download = new WWW ("file://" + Application.dataPath + "/AssetBundles/" + url);
    14.             download = new WWW (yo2);
    15.        
    16.     yield download;
    17.        
    18.     assetBundle = download.assetBundle;
    19.     download.Dispose();
    20.     downlaod = null;
    21.        
    22.     var objects = assetBundle.LoadAll(GameObject);
    23.     for(var i=0; i < objects.length; i++)
    24.     {
    25.         inventory.Add(objects[i].name);
    26.     }
    27.  
    28.     inventoryLength = inventory.ToArray().length;
    29.     start = 15 + inventoryLength * 25;
    30. }
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    can you show the editor script that builds the bundle?

    Chances are high that you only build 1 bundle while you need to build 1 bundle for desktop, 1 bundle for web (at worst, normally web bundles work on desktop too but not needfully vice versa), 1 bundle for iOS and 1 bundle for android
     
  10. vahi2008

    vahi2008

    Joined:
    Oct 1, 2010
    Posts:
    321
    Code (csharp):
    1. @MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")
    2. static function ExportResource () {
    3.     // Bring up save panel
    4.     var path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
    5.     if (path.Length != 0)
    6.     {
    7.         // Build the resource file from the active selection.
    8.         var selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
    9.        
    10.         BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets); 
    11.         Selection.objects = selection;
    12.     }
    13. }
    14.  
    15. @MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")
    16. static function ExportResourceNoTrack () {
    17.     // Bring up save panel
    18.     var path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
    19.     if (path.Length != 0)
    20.     {
    21.         // Build the resource file from the active selection.
    22.         BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
    23.     }
    24. }

    and i am using Track dependencies option
     
    Last edited: May 17, 2012
  11. vahi2008

    vahi2008

    Joined:
    Oct 1, 2010
    Posts:
    321
    ! more thing i want to get into the notice is-

    I selected "PC and Mac Standalone" and then made the assetbundles, now what i see is, when i check my player settings, they are back to "Web Player" . Now here 2 strange things i have notice with asset bundles

    1. Asset bundles due to some reason, is currently not coming in the exe, mobile i still did not check.
    2. Why the player settings change after building the assetbundles ?

    I'll update more and more till i find a solution. Nobody is coming to my rescue :(
     
  12. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    1. Not coming means they don't load? They are not transfered as part of it of the exe build etc for example.
    also is there any information in the log of the standalone? Potentially it mentions and error that helps you fixing this, as would likely do a remote connected debugger too when you set a breakpoint to the start of your function

    2. Thats not strange, thats a hell annoying missbehavior I already reported on 3.0 betas which has not been addressed and might not even be considered a bug. You will need your editor script to check what platforms its on prior building and switch back. With asset server or better the cache server, such switches luckily are trivial
     
  13. vahi2008

    vahi2008

    Joined:
    Oct 1, 2010
    Posts:
    321
  14. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    OK I've been looking into this problem for vahi2008 and the problem appears to be that even when you set the BuildAssetBundle's BuildTarget the resulting asset that is exported has a header file that begins UnityWeb regardless if the Target is a StandaloneWindows or a WebPlayer build.

    And therefore I think this is the reason why it will not load the assetBundle in a Standalone Windows player?
     
  15. paulrahme

    paulrahme

    Joined:
    Apr 25, 2012
    Posts:
    17
    I'm also having the AssetBundle problem - building from UnityEditor using the following script:
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. public class ExportAssetBundles
    6. {
    7.     [MenuItem("Assets/Build AssetBundle From Selection")]
    8.     static void ExportResource()
    9.     {
    10.         // Bring up save panel
    11.         string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");
    12.         if (path.Length != 0)
    13.         {
    14.             // Build the resource file from the active selection.
    15.             Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
    16.             BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows, BuildOptions.UncompressedAssetBundle);
    17.             Selection.objects = selection;
    18.  
    19.             foreach(Object obj in selection)
    20.             {
    21.                 MonoBehaviour.print("Exported object: "+obj.name);
    22.             }
    23.         }
    24.     }
    25. }
    26.  
    It loads fine in the Unity Editor (Windows XP), but on a build version (.exe) it does not load.

    Looking at the generated ..._Data/output_log.txt, the error given is as follows:

    The asset bundle 'AssetBundles/Common Icons.unity3d' can't be loaded because it was not built with the right version or build target.

    (Filename: C:/BuildAgent/work/300357e52574df36/Runtime/Misc/AssetBundleUtility.cpp Line: 542)


    The actual .unity3d file header starts with "UnityRaw" (since i use UncompressedAssetBundle)

    I hope one of us can find the solution to this problem soon!
     
  16. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Try the Compressed version it will be smaller and lighter to download.
     
  17. paulrahme

    paulrahme

    Joined:
    Apr 25, 2012
    Posts:
    17
    I originally tried Compressed bundle but it refuses to open - says it got type UnityWeb, expected UnityRaw. If it read compressed, that would be great (but not that important since the AssetBundles are included with our app, just different combinations for different clients).

    The behaviour seems to change with different versions of Unity - I'm using 3.5.2f2, but it looks like compressed read ok on older versions.

    Highly frustrating, since we can currently only use this feature in Editor mode, but not take it to any clients demo it off a build version :(
     
  18. paulrahme

    paulrahme

    Joined:
    Apr 25, 2012
    Posts:
    17
    Found the problem I was having! This may help track down similar issues.

    The "not built with the right version or build target" error was coming up in build mode, because it could not open the file. Instead of reporting something like "file not found", Unity's AssetBundle.CreateFromFile() function seems to just assume the file opened ok, tries to read garbage, and panics about not knowing what to do with it.

    Using an absolute file path works ok, eg.
    Code (csharp):
    1. CreateFromFile("C:\\temp\\mybundle.unity3d")
    so use Application.dataPath (possibly in conjunction with Directory.GetParent) to get the absolute path to your asset bundle.
     
  19. Toack

    Toack

    Joined:
    Dec 18, 2006
    Posts:
    107
    Thank you so much paulrahme, it never occured to me to test an absolute path because relative paths should work as expected, but again, the Unity team somehow just don't care about minor "adjustments" like this one, that I have absolutely no idea why are there in the first place.
     
  20. stevesan

    stevesan

    Joined:
    Aug 14, 2011
    Posts:
    65
    Thank you for that fix. I just ran into this issue my self, and using Application.dataPath to get an abs path made it work. BUG! OSX does not require this, btw.
     
  21. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264