Unity : from Asset Bundles to On Demand Resources - unity3d

I'm currently looking to implement On Demand Resource on my project.
I already use Asset Bundle, but I'd want some of them to be On Demand Resource.
I found this page https://docs.unity3d.com/2019.4/Documentation/Manual/AppThinning.html
And there is things that I'm not sure to understand.
So I need to create a script BuildiOSAssetBundles to tell Unity which Asset Bundle I want to use as ODR.
But I'm confuse by this line :
new UnityEditor.iOS.Resource( "bundle", "Assets/Bundles/bundle.unity3d" ).AddOnDemandResourceTags("bundle"),
I didn't found the documentation of "Unity.iOS.Resource" object, and I'm a bit confused about the path I should give.
For exemple, I have a bundle named "foo/bar", which is composed of 3 scenes : "foo_level1", "foo_level2", and "foo_level3", let's say that theses scenes are located in a folder named "Assets/Foo/Scenes".
How do i do to tell Unity that this bundle should be a On Demand Resource ?
Plus, I have a lot of bundle similar to "foo/bar" (let's say 50). Do I have to register them one by one in that script ? Or is there a way to "mark" every bundle starting with "foo/" as ODR ? (Is there a GUI ?)
When I build my assets bundles from the GUI, the path I'm using is "AssetBundles/iOS". May be I should use that in the path ?

Related

Unity Asset Bundles - Which Files Do I Deploy?

I have created some asset bundles from my Unity assets using the directions given in the Unity documentation section on AssetBundle Workflow. After running the "Build AssetBundles" command, each asset bundle results in four files: myasset, myasset.meta, myasset.manifest, myasset.manifest.meta.
Now I am ready to deploy these bundles to a web server and implement downloading/caching in my Unity project. I have found numerous examples such as this that show the download URL to be a single file with a .unity3d extension. This is leading me to conclude that I am missing a step - I assume that all four of my files will be required by the app and that I have to do something to combine them into a .unity3d file first.
What file(s) do I need to deploy? Are there any additional steps that I need to take before my file(s) are ready to upload? Thanks in advance for any advice!
Just myasset will suffice.
Sometimes people optionally add .unity3d as a filename extension to their Asset Bundles. It is just a community convention, and is completely optional. Source (copied below)
Vincent-Zhang
Unity Technologies
Just a reminder, we don't have an official file extension ".unity3d" for asset bundle, it's not mandatory. You can use whatever file extension as you want, or without file extension.
But usually people use ".unity3d" as the file extension just because we used it in the official sample code at first time...
Unity creates the .meta files for all assets- you don't have to worry about those. In short, your myasset file is enough. I do not add file extensions to mine. Do note that if you use the strategy shown in the example that you shared that the client will re-download the bundle from the server every time. You need to additionally provide a version number if you want to take advantage of caching. You can see this in some of the method overloads here, the ones that have a Hash128 or uint "version" parameter. Caching is great because you can use the bundle that is already saved on the device next time instead of downloading from the server when no changes have occurred. The version/hash number you provide essentially gets mapped to the file name. Whenever a matching version is found, the file on disk is used. Update the version number to something else when the content changes to force the client to download anew.
You may want to reference the .manifest file for the CRC value listed there. You may have noticed a crc parameter in the link I shared as well. This can be used to ensure data integrity during transmission of the bundle data. You can make sure the downloaded content's CRC matches that of the bundle when you created it.

Load an asset from outside a 'resources' folder

I'd like to load "dynamic" assets (in particular, FBX files) that are located outside of a "Resources" folder so that the user can add/remove/modify files with no need to re-build the project.
To make those files easily accessible for the user in different kinds of devices (target platforms are desktop/android/ios), it seems reasonable to use either Application.persistentDataPath or Application.streamingAssetsPath.
Now the problem is: How do I load them (as proper Unity objects) ?
This obviously can't be done via Resources.Load() as they are outside a "Resources" folder.
Using WWW, retrieving the files should be easy and it could actually work for some types of files (e.g. textures, see http://docs.unity3d.com/ScriptReference/WWW-texture.html) but I cannot see a way to load something else.
The idea is that I'd like to be able to use those loaded objects like so:
(GameObject) Instantiate(loadedObject, new Vector3(0, 0, 0), Quaternion.identity);
I would really appreciate your help (preferrably with a solution that does not require a "pro license").
I know two options that you can try to use in this case:
Build an asset bundle for each dynamic asset you want to load, but this seems not really what you desire, because the user still have to use Unity3d to generate the asset bundle.
Use an custom 3d model loader, like described in this post, seems the best fit for your case.

Solution with dynamically assets in unity

I'm starting learn unity not long ago. Now, I have issues. When I build a game, I don't want build all assets in project. I want download some assets when Client after install game (Ex : first load Splashscreen). I tried to search and found WWW.LoadFromCacheOrDownload. I don't sure it correct!.
Can you give me solutions or keyword?
Thank you very much.
You are on correct road. But i think you need to google a bit deeply the keyword and you would have reached the "Asset Bundles".
Asset Bundle Defination
Scripting Asset bundles
Asset Bundle management
Asset Bundle detail description
I did asset bundle usage quite some time ago so i might miss something while explaining this.
First you need to build the resources which you will be required.(Each platform has different builds so you need to keep that also in mind)
You will need a script to download and cache the asset bundle from the net.
WWW.LoadFromCacheOrDownload
After caching comes the part of loading and unloading asset bundle from cache/resources. Also for every update we pushed we allowed application to check for any asset bundles were available so that it could fetch and use it.
I think there are some more helper scripts for the asset bundles and tutorials to go with on the net as I had used a script to generate asset bundles
to modify it.
Here is unity asset store Example and scripts for asset bundles
So basically it will be a long solution to implement but it is worth every time you spend.
Hope this helps.
Edit : Here is script I had used to create asset bundles. Unifycommunity Asset bundle builder Link

Errors loading scripts on prefabs from asset bundles

I'm currently trying to set up my project to allow users to download mini games post release. Todo this I am trying to use asset bundles to facilitate this, one containing the new games scene and one containing the assets and DLLs for the scripts required. The problem I am having is that when I load the scene or a prefab from the bundle all the attached scripts are still there but a warning says "the associated script can not be loaded. please fix any compile errors and assign a valid script". Is there something obvious that I am missing or do asset bundles not work this way and I'm going to have to rebuild my scene on load?
You can't have the scripts in the asset bundles assigned as references from GameObjects like you normally would. You can only access scripts from asset bundles by loading them with reflection. Then you can programmatically attach them to GameObjects, etc. from there (for example, load the assembly, find your Type, then use AddComponent() on a GameObject with your new loaded type).
See http://docs.unity3d.com/Manual/scriptsinassetbundles.html.
But even if you were to overcome these hurdles, it won't work on Windows Phone and it's against the terms of service of Apple's store, so ...

(iphone) How to store images in directory structure (vs flat documents directory) and related questions

I'm looking for a good way to manage many image files in my app.
When the number of images to maintain gets large, it seems vital to have a directory structure to hold them.(to organize images and to support duplicate image names)
Since normal way of storing images in documents directory(by adding images to xcode's resource folder) doesn't seem to support directory structure,
I probably need to use something called bundle.
Here are questions.
It's not clear to me what's the difference between "documents
directory" and "bundle". Are they
just names to specify directory path
inside an iphone application? Since
documents directory doesn't support
directory structure, I guess it's not
a regular file path in iOS? (I'm
looking for a definition or overview
description on those two terms enough
to help answering the following
questions)
How do I create a tree structure directory to store resources?
How do I add files to the created directory?
What is the step (probably in xcode) to add new files to the
directory as project grows? note:
question 3 deals with initial set up,
4 deals with update.
What happens to files under documents directory and bundle when
user updates the app? Since many
applications store user data
somewhere, there must be a way of
updating an app without wiping out the
saved user data. ie. How does
"documents directory" and "bundle"
behave in update situation?
So called "resource bundle" refers to the "bundle" used in above
questions?
Thank you.
Your app is a "bundle". It's represented by an NSBundle object. On iOS, this is the only bundle you are allowed to use; you can't create another bundle and import it into your app.
However, you can add a subdirectory to your app's bundle. In Xcode 4, select your project in the navigator and click on the Build Phases tab. Add a new build phase (bottom right), Copy Files. Move it up just below Copy Bundle Resources, and rename it something meaningful ("copy interface images", or something). You'll notice you've got a Subpath field there - that's your directory in your bundle. Drag the files you want in that subdirectory on to this build phase, and then you can access them through the normal methods in NSBundle, UIImage, NSData and so on.
Wish it was easier? Me too.