Yield WWW in C#

Hi,

I can’t load an assetbundle from the /Documents folder of my app on IOS.
I tried all the answers i can found on internet but it still not working.
My script for Android, Windows, Mac OS X works but not on IOS.
I work on IOS 5.1 and later.

here is my script
i set the UIFileSharingEnabled of the Plist.info to true.
I use this part of the script in a coroutine launch in the Start()

WWW temp_pathAssetBundleConcerne = new WWW(@listAsset[indexAssetBundle]);
//listAsset[indexAssetBundle] = @Application.persistentDataPath + "/Resources/" + "NameAssetBundle/AssetBundle.AssetBundle"
			
yield return temp_pathAssetBundleConcerne;
			
AssetBundle temp_ouvertureAssetBundleConcerne = new AssetBundle();

temp_ouvertureAssetBundleConcerne = temp_pathAssetBundleConcerne.assetBundle;

After the debug, it seems that the error come from the yield return,
in the Xcode console i have the error

Failed to load asset bundle
UnityEngine.WWW:get_assetBundle()

SomeOne can help me ? i’m on this problem for one week.

(Sorry for my bad english i’m still learning it)

Are you sure the file is placed at correct location?
Do you specify “file://” prefix/protocol when passing file location to the WWW class?

Hi Mantas,

Yes i’m sure that the file is placed at correct location, i used the file.Exist test to check this.

I tried whit the prefix “file://” but whit it, i have an error, the file doesn’t exist
Maybe it’s the problem ? the file without the prefix exist but with it, it doesn’t exist, that’s normal ?

I try the find on internet if there is an access problem, maybe i’m not autorhized to access /Documents, maybe i have to activate an option like
UIFileSharingEnabled ?

i will try to load a picture instead of an assetbundle

Please post here the final URL you are using with file:// prefix.

@“file://” + Application.persistentDataPath + “/Resources/” + “Cube/Cube.assetbundle”

I have a “/Resources” in my “/Documents” app.

I just saw that i have an “/” in “Cube/Cube.assetbundle” maybe i have to use an “\” ? but my WWW find the file and the file.exist
i will try it maybe it’s just that

Then maybe file is found, but the platform of Asset Bundle does not match platform you are deploying on. When exporting Asset Bundle have you specified iPhone as target platform?

yes, i use the BuildTarget.iPhone to create my assetBundle

could you please show whole Start function code?

i though the problem come from my script so i create a new project with only on script just to test if i can open an assetbundle

the main script

using UnityEngine;
using System.Collections;
using System.IO;

public class script_testLoadIOS : MonoBehaviour {

	// Use this for initialization
	IEnumerator Start () 
	{
		if(File.Exists(@Application.persistentDataPath + "/test.assetbundle"))
		{
			WWW test = new WWW(@Application.persistentDataPath + "/test.assetbundle");
			yield return test;
			AssetBundle testA = new AssetBundle();
			testA = test.assetBundle;
			Instantiate(testA.Load("Cube",typeof(GameObject)),Vector3.zero,Quaternion.identity);
			testA.Unload(true);
		}
	}
}

and here the script i use to create an assetbundle (it’s the code from unity scripting reference web site)

using UnityEngine;
using UnityEditor;
using System.Collections;

public class editor : Editor {

 [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
    static void ExportResource () {
        // Bring up save panel
        string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "assetbundle");
        if (path.Length != 0) {
            // Build the resource file from the active selection.
            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets,BuildTarget.iPhone);
            Selection.objects = selection;
        }
    }
}

i build then i run the app on Xcode with an ipad 5.1 simulator, it creates the documents folder then i paste the test.assetbundle in the /Documents folder of the simulator.

But again i have an error in Xcode,
Failed to load asset bundle
UnityEngine.WWW:get_assetBundle()
c__Iterator0:MoveNext()

I hope the mistake comes from my script.

Hi

First of all, As Mantas puida said, if you try to access a local file with the www class, you must prefix the path with “file://”.
Second for asset bundle you should use www.LoadFromCacheOrDownload to enable caching.
And Finally asset bundles have the extension .unity3d not .assetbundle

Hope this helps

Hi,

As i said in a reply, if i use “file://” in the path, my file don’t exist for unity, i think the problem comes from here,
i don’t think the extension of the file is necessary .unity3d, i use .assetbundle for my android and windows app ad it works.

i will try the LoadFromCacheOrDownload but my files are on the /Documents of my app on my ipad so it’s mean i have to cache my folder before i load it. I find that weird but i will try it.

Thanks for your help

The path you try to use is not correct, on no platform actually, assuming we talk about content you added in XCode or Unity for inclusion in the project, as the path you have there points to the folder where the application is in / the assets folder in the editor.

In none of these places, asset bundles can be (in the editor they could but you will get problems if you try to have them in the Assets folder directly)

For in editor it would likely want to go to the StreamingAssets folder.
For Android its quite a bit more complex (the documentation and Unity Answers have the path though)
For iOS you would need to go to the /Raw/ folder if it was StreamingAssets in Unity. If you have them in documents then you downloaded them before or copied them there without it being needed as you can access StreamingAssets directly. If you downloaded it from the web, you would best use LoadFromCacheOrDownload directly with the http address of the asset bundle though.

On iOS the WWW to load them from the streaming assets folder directly would look like

  WWW test = new WWW("file://"+Application.persistentDataPath + "/Raw/test.assetbundle");

or in a full code:

void Awake ()
{
  StartCoroutine(LoadBundle());
}

IEnumerator LoadBundle () {
  WWW test = new WWW("file://"+Application.persistentDataPath + "/Raw/test.assetbundle");
  yield return test;

  // do something with test

  test.Dispose ();
}

it’s

Application.streamingAssetsPath

which in fact gives on iOS path like

../Data/Raw

(at least in 4.x don’t know if it was present in pre 4.x)

When you talk about the /Raw folder, you talk about the /Raw folder contain in the app ? (“file://” + application.datapath +“/Data/Raw”)
Because “file://”+Application.persistentDataPath + “/Raw/test.assetbundle” point to /Documents/Raw/test.assetbundle not the in app /Raw folder

Hi all, i solved my problems

I changed the creation of the prefab
You can find the solution in this post

http://forum.unity3d.com/threads/77692-Asset-Bundles-failing-to-load-(on-iOS)

now i can load my assetbundle from the /Documents folder

Thanks for all guys, with your helped i learned a lot on unity IOS, hope someday it will my turn to help you