Log in or register to post comments

Load 3d Objects from SD Card

November 26, 2012 - 5:01am #1

Hello everyone,

I'm trying to load a 3d object from the SD card on my tablet. I use Asset Bundles, as in this tutorial: http://docs.unity3d.com/Documentation/Manual/AssetBundlesIntro.html . For desktop applications this works fine, but not for AR app on my tablet. Maybe it's something wrong with path, I use this path:  file :/ / / sdcard/3DObjekat/auto.unity3d.


Can someone help me?


Thanks,


Djordje

Load 3d Objects from SD Card

November 28, 2012 - 5:17am #7

About the path, try
"file:///mnt/sdcard/.." instead of "file:///sdcard/.."

Load 3d Objects from SD Card

November 28, 2012 - 5:14am #6

Hi, the code to generate the asset bundle looks fine (and the BuildTarget.Android  is included, which is perfect).

There might be two problems left:

1- the asset bundle is loaded but you don't see it in your view

2- the file path is incorrect

To check if file path is correct, you need to check your Logs; the code that loads the asset bundle will throw an exception if it does not find the asset bunlde car.unity3d in the sdcard; such exception will be printed out in the logs.

To check the logs, you will need to run the application and then use adb logcat from a command line (then you will be able to see if there ia anything like a file not found exception or similar I/O exception thrown).

 

he other potential problem is that the asset bundle is loaded but it is not visible;

I see in your code you call "Instantiate" and pass a set of transformation parameters; this is a good practice in general, however this will probably locate your asset bundle in World coordinates;

if your goal is to show the model attached to an Image Target, then that approach may not work; what you would need to do is to parent the asset bundle to the ImageTarget game object (i.e. to be a child of Image target) and then set the local position and local scale and rotation;

this is shown in the code snippet that I posted in my previous message; maybe give it a try (note: you might need to adjust the scale to something reasonable)

 

 

Load 3d Objects from SD Card

November 28, 2012 - 4:57am #5

Thank you very much, I appreciate your help. This is the function that I'm using in my script to create Asset Bundle:

  static void ExportResource () {
        // Bring up save panel
        string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
        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.Android);
            Selection.objects = selection;
        }
    }

This asset bundle is placed in the folder 3DObject on my tablet. For path I'm using "file:///sdcard/3DObject/car.unity3d", maybe the path is wrong?


Thanks,

Djordje

Load 3d Objects from SD Card

November 27, 2012 - 6:55am #4

Hi manilov88,

when loading assetbundles from sdcard on Android, you need to be sure that the assetbundle has been built for the Android target,

so if you built it from unity by yourself, make sure you have added the BuildTarget.Android parameter to the BuildAssetBundle function.

If you did not build it by yourself (i.e. you downloaded it from the net), make sure it is compatible with Android.

If you want to have an example of how to build an Android-compatible assetbundle from Unity (and then copying it to sdcard), let me know.

 

Other than that, make sure that the file path does not contain blank spaces and remember that in Android the filepath is case sensitive (for instance "Cube.unity3d" is not the same as "cube.unity3d", same for directory names).

 

If that is all in order, and you still don't get it, you may want to try out this code snippet, which is basically the same as yours,

excpet that it shows a re-parenting technique (which allows for instance to attach the assetbundle directly under an ImageTarget game object):


public class CachingLoader : MonoBehaviour {

public string AssetName;

public int version;

private Transform mTransform;

void Start() {

mTransform = GetComponent<Transform>();

 

StartCoroutine (DownloadAndCache());

}

 

IEnumerator DownloadAndCache (){

// Wait for the Caching system to be ready

while (!Caching.ready)

yield return null;

 

//For android (assuming there is an asset bundle called Cube.unity3d under /sdcard/

string bundleUrl = "file:///mnt/sdcard/Cube.unity3d";

 

// Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache

using(WWW www = WWW.LoadFromCacheOrDownload (bundleUrl, version)){

yield return www;

if (www.error != null)

throw new Exception("WWW download had an error:" + www.error);

AssetBundle bundle = www.assetBundle;

 

GameObject go = null;

if (AssetName == "")

go = (GameObject)Instantiate(bundle.mainAsset);

else

go = (GameObject)Instantiate(bundle.Load(AssetName));

 

//optional code to re-parent the assetBundle

//and set the local positioning

if (go != null) {

go.transform.parent = mTransform;

go.transform.localPosition = new Vector3(0, 0, 0);//set your desired local position here

go.transform.localRotation = Quaternion.identity;//set your desired local rotation here

go.transform.localScale = new Vector3(1,1,1);//set your desired local scale here

}

 

// Unload the AssetBundles compressed contents to conserve memory

            bundle.Unload(false);

}

}

}

 

Load 3d Objects from SD Card

November 27, 2012 - 2:34am #3

Thanks for reply. OS is Android 4.0.3. Here's the script that I use:

 

using System;
using UnityEngine;
using System.Collections;

public class CachingLoadExample : MonoBehaviour {
    public string BundleURL;
    public string AssetName;
    public int version;
    private GameObject obj;
    void Start() {
        StartCoroutine (DownloadAndCache());
    }

    IEnumerator DownloadAndCache (){
        // Wait for the Caching system to be ready
        while (!Caching.ready)
            yield return null;

        // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
        using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){
            yield return www;
            if (www.error != null)
                throw new Exception("WWW download had an error:" + www.error);
            AssetBundle bundle = www.assetBundle;
           
            if (AssetName == "")
            {   
                Vector3 position = new Vector3((float)0.007622499,(float)0.1420865,(float)0.0504825);
                Quaternion rotation = Quaternion.Euler((float)3.312042,(float)97.28548,(float)-172.7145);
                Instantiate(bundle.mainAsset, position, rotation);
            }
            else
            {
                Vector3 position = new Vector3((float).007622499,(float).1420865,(float)0.0504825);
                Quaternion rotation = Quaternion.Euler((float)3.312042,(float)97.28548,(float)-172.7145);
                Instantiate(bundle.Load(AssetName), position, rotation);
            }
                    // Unload the AssetBundles compressed contents to conserve memory
            bundle.Unload(false);
        }
    }
}

Load 3d Objects from SD Card

November 26, 2012 - 7:04am #2

Which OS version is your tablet running? Also see the method described here - http://answers.unity3d.com/questions/282471/how-to-load-an-assetbundle-from-sdcard.html

Log in or register to post comments