Search Unity

Remote AssetBundle Question

Discussion in 'Scripting' started by viniverso, Jun 18, 2018.

  1. viniverso

    viniverso

    Joined:
    Sep 11, 2015
    Posts:
    20
    Greetings,

    First of all I am using Unity3d 2017.3.1p1, and I looked over some hours to build a structure about AssetBundle and how it's works.

    After some research, I did the basic Export Asset function.
    It's works with Local access, but now I need to make the function to work with remote access.

    Actually I got a little confused because the structure for local file access is different that I found for remote access.
    Here goes some example I found:
    Download from Local File Access
    Download from Remote Access

    The main focus is about the example/infos I found:
    Why In Local File, via AssetBundle, my access could be made by folder, and on remote access, on the example, my access has to be made by file (".unity3D")?

    Someone has some tip about this?

    Best wishes,
    Vinícius
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    It doesn't actually matter what the file extension is. It's just a bag of bytes.
     
  3. viniverso

    viniverso

    Joined:
    Sep 11, 2015
    Posts:
    20
    Hi Kurt-Dekker,

    But in the actually structure, the Asset Bundle create 2 types of files: 1 Asset Bundle file and 1 related .manifest.

    For Local access (via StreamingPath/AssetBundle folder), I just set the folder to access and search for the specific asset.

    But my question is on Remote access, in the examples used to to get a file, not a folder. Or am I wrong?
    And if I need to create this file, how I can create it?

    Thanks by now,
    Vinicius
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I'm not sure what you are asking exactly. We use the asset bundle browser for our new projects to set up our bundles and then build them. It generates two files. The assetbundle without a file extension and the .manifest file.

    We upload the assetbundle to a CDN where we download it from. Again, no file extension on it.

    But, as @Kurt-Dekker mentioned, you could put an extension on it if you want and it shouldn't matter any.

    Local assetbundles in streamingassets and downloading from the web behave exactly the same. You don't have to add a file extension to local bundles either.
     
  5. viniverso

    viniverso

    Joined:
    Sep 11, 2015
    Posts:
    20
    Hi Brathnann,

    Thanks for your attention and explained how you works with it.
    But I stil have some questions about it. (It's my firt time working with AssetBundle and remote access).
    1 - How do you read that asset bundle from remote server?
    2 - What is a CDN?
    3 - Has some good tutorial with a overview about AssetBundle Browser? (The keynote I found, the remote access example not worked =/ )

    Below is my code to import the asset bundle, and some info about it.
    Code (CSharp):
    1. void LoadLocalFile(){
    2.         urlToload = Path.Combine(Application.streamingAssetsPath, "AssetBundle");
    3.         Debug.Log("URL " + urlToload);
    4.  
    5.         urlToload = Path.Combine(urlToload, "obj01");
    6.         Debug.Log("URL " + urlToload);
    7.  
    8.         var myAssets = AssetBundle.LoadFromFile(urlToload);
    9.  
    10.         if (myAssets == null)
    11.         {
    12.             Debug.Log("Falha ao carregar os assets");
    13.         }
    14.  
    15.         var objLoaded = myAssets.LoadAsset<GameObject>("Cube");
    16.         Instantiate(objLoaded);
    17.  
    18.         myAssets.Unload(false);
    19.     }
    And that's my code for Remote Access:
    Code (CSharp):
    1. IEnumerator LoadFromServer()
    2.     {
    3. //Access to a test folder
    4.         using(UnityWebRequest _req = UnityWebRequest.GetAssetBundle("https://www.hiperverso.com.br/AssetBundle"))
    5.         {
    6.             yield return _req.SendWebRequest();
    7.  
    8.             if(_req.isNetworkError || _req.isHttpError)
    9.             {
    10.                 Debug.Log("Connection Error " + _req.error);
    11.             }
    12.             else{
    13.                 AssetBundle _bundle = DownloadHandlerAssetBundle.GetContent(_req);
    14.                 var objLoaded = _bundle.LoadAsset<GameObject>("Cube");
    15.                 Instantiate(objLoaded);
    16.  
    17.                 _bundle.Unload(false);
    18.             }
    19.         }
    20.     }
    The "Remote Access" code returns a "HTTP Generic error" and don't import nothing into the project.

    On the Remote URL, has a folder with 4 files: 2 ("AssetBundle" and my bundle, called "obj1") without extension and yours respective .Manifest files.

    Thanks again,
    Vinícius
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    1. I'm not sure if your path is correct. Your AssetBundle is named "AssetBundle" or named "obj1"? Your code doesn't appear wrong if the path is correct.

    2. Content delivery network. They are great for storing large amounts of data really cheap and getting them out so that people can get access quickly to that data.

    3. I don't. I started bundles before the browser was created and just took knowledge from that and moved it along.
     
  7. viniverso

    viniverso

    Joined:
    Sep 11, 2015
    Posts:
    20
    1) So, I am respecting the same structure for local access.
    The path is to AssetBundle folder and that is the 4 files over there:
    Screen Shot 2018-06-19 at 3.36.54 PM.png
    I need to take the objects from "obj1".
    May be I am miss something, right?

    2) I am not sure if I know something about it. Could you mention CDN service?

    Thanks for your attention, it's been really useful. =)
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    If obj01 is your bundle with your asset in it, that is what you need to download.
    It looks like you're trying to grab AssetBundle. AssetBundle may just be the autogenerated bundle that contains build target info (I think).

    https://www.hiperverso.com.br/obj01

    Should be the valid link for you.

    Make sure you can actually download from your server. Try a text file and see if you can successfully grab that. (if you haven't already)

    We use keycdn. Found the price to be one of the less expensive cdn. Though Google and Amazon both have a service also.
     
    viniverso likes this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    The obj01 file is your content. That is what Unity considers "an AssetBundle."

    The obj01.manifest file is a text file that you can parse to see what is in there, if you care.

    The AssetBundle file (it will be named the same as the target folder you choose) is an AssetBundleManifest. You can use it to see what was created when you made the files in this directory. Docs here:

    https://docs.unity3d.com/ScriptReference/AssetBundleManifest.html

    You can open that AssetBundleManifest to ask what all is in this particular suite of AssetBundles, the ones you just built. You can do this (for example) by iterating the . GetAllAssetBundles(), which returns a string array.
     
  10. viniverso

    viniverso

    Joined:
    Sep 11, 2015
    Posts:
    20
    Wow,

    Pretty nice info, guys. Thanks a lot.
    That helped me to get the project working on Editor, but as my project is for mobile, I am testing there too but the return is not so good.
    The app identify platform, makes a successful connection but not load the object as expected.

    Do you guys has some tips about it?

    That is how I made the AssetBundle:
    Code (CSharp):
    1. public class AssetBundle : Editor
    2. {
    3.     [MenuItem("Assets/Create PC Asset Bundle")]
    4.     static void CreatePCBundle()
    5.     {
    6.         string bundlePath = "Assets/StreamingAssets/AssetBundle";
    7.  
    8.         if (!Directory.Exists(bundlePath))
    9.             Directory.CreateDirectory(bundlePath);
    10.  
    11.         BuildPipeline.BuildAssetBundles(bundlePath, BuildAssetBundleOptions.None, BuildTarget.StandaloneOSX);
    12.     }
    13.  
    14.     [MenuItem("Assets/Create Android Asset Bundle")]
    15.     static void CreateAndroidBundle()
    16.     {
    17.         string bundlePath = "Assets/StreamingAssets/AssetBundle/Android";
    18.  
    19.         if (!Directory.Exists(bundlePath))
    20.             Directory.CreateDirectory(bundlePath);
    21.  
    22.         BuildPipeline.BuildAssetBundles(bundlePath, BuildAssetBundleOptions.None, BuildTarget.Android);
    23.     }
    24.  
    25.     [MenuItem("Assets/Create IOS Asset Bundle")]
    26.     static void CreateIOSBundle()
    27.     {
    28.         string bundlePath = "Assets/StreamingAssets/AssetBundle/IOS";
    29.  
    30.         if (!Directory.Exists(bundlePath))
    31.             Directory.CreateDirectory(bundlePath);
    32.  
    33.         BuildPipeline.BuildAssetBundles(bundlePath, BuildAssetBundleOptions.None, BuildTarget.iOS);
    34.     }
    35. }
    Thanks,
    Vinicius
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I just use the asset bundle browser to build bundles, so I haven't coded it myself. Just make sure you are grabbing the correct bundle for your platform. Android bundles for android, iOS for iOS, etc.

    Otherwise, use something that lets you have a runtime console on the device so you can look for errors.
     
    viniverso likes this.
  12. viniverso

    viniverso

    Joined:
    Sep 11, 2015
    Posts:
    20
    Hey Brathnann,
    So, as my code show on the previous reply, I really make for specific platforms.
    But on my test on Android device the app makes the connection but nothing happens. Not loaded any object.

    I set some permissions (INTERNET = "Require", Write Permission = "Prefer External") but I don't know if has another permissions to set.

    Thanks again.
     
  13. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Only thing I can tell you is use a runtime console. You can get several on the asset store for free. We use log viewer, but you'll have to remove the webplayer stuff. Otherwise, there are others. This will let you view the console on the device and look for errors.

    Or, use Logcat to track device activities.
     
    viniverso likes this.
  14. viniverso

    viniverso

    Joined:
    Sep 11, 2015
    Posts:
    20
    Thanks for the suggestion, but I use the Loggly when it's necessarily.

    So, now I made a different test on Editor to make sure that is working nicely. Here is the steps I did:
    1 - Updated the asset on Remote Access (same name and structure);
    2 - Tried Connect again;
    And now is returning 2 errors that I don't understand why is happening if is the same code.
    Screen Shot 2018-06-20 at 4.28.46 PM.png

    Do you know what this errors means?
     
  15. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I don't know what loggly is. (just looked it up, I prefer a runtime console and logcat myself)

    The top error seems like an error I got when I tried to get an assetbundle that wasn't compatible with my version. Either it was the wrong bundle (iOS trying to get an android bundle) or it's a bundle created with the newer version of unity and trying to use it on an older version of Unity.

    Granted, there could be other reasons, the two I listed are the only ones I remember seeing give me that error. The second error I don't think I've seen.
     
    viniverso likes this.
  16. viniverso

    viniverso

    Joined:
    Sep 11, 2015
    Posts:
    20
    Wow, fixed this issue.
    Now the AssetBundle is working on both, Editor and Android. Now I need to test on IOS. And then I will update you guys.
    The problem is the transfer type to remote server. On Filezilla was configured to 'Ascii' but the correctly is 'Binary'.
    Here goes the link that helps me: https://answers.unity.com/questions/1140569/error-failed-to-decompress-data-for-the-assetbundl.html

    A question what I didn't understand at all on my search:
    AsseBundle, after the download was made on mobile, the content stay on cache and get the file automatically everytime that is called or has something to work with cache?
     
  17. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Ah, yep. I remember having to change Filezilla to binary. It's one of those steps I did so long ago that I don't even think about it.

    There are ways to version your assetbundle, but from what I can recall on the cache, bundles get stored there for a bit and can be loaded so they don't get downloaded again, however as this gets filled, lower used bundles might be removed. It's been a long time since I've looked into these docs about the cache, so I'm pretty sure stuff has changed.
     
    viniverso likes this.
  18. viniverso

    viniverso

    Joined:
    Sep 11, 2015
    Posts:
    20
    Ok, Thanks for your experience and information Brathnann.

    So, now I am doing the IOS version. But it's like the XCode is blocking the access. Screen Shot 2018-06-20 at 6.52.26 PM.png
    That's my configuration.

    On the app is returning a error message: Received no data in response.

    The bundle was build for IOS.

    It's missing to add something or some permission I didn't included?

    By the way, below is the message returned from XCode Console when it's on Debugging: Screen Shot 2018-06-20 at 7.02.01 PM.png
     
  19. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I'm not the iOS guy at work, so my knowledge on it is pretty limited. I can do builds on it and basic debugging, but that is where my knowledge of it ends.

    Saying this, I've never changed anything myself nor has the iOS guy ever come to me saying something doesn't work.

    The error you're getting mentions you can add an exception to info.plist for downloading over http vs https. Is your server that you're downloading from using ssl?
     
  20. viniverso

    viniverso

    Joined:
    Sep 11, 2015
    Posts:
    20
    Hi Brathnann,
    Yes, It's using SSL.

    I searched about it, and found some advices about put exception on my domain, but that is a risk and neither worked.
    I would like to learn about the mostly "correct" version as possible to make the project works on the right way.

    Thank you again Brathnann for your patience, if you discover something about it, please let us know.
    Anyone has some tips about IOS Permission?