Log in or register to post comments

How to load 3d object on runtime in unity.

November 5, 2012 - 12:36am #1

I want to load 3d object on runtime when target detect. This 3d object(may be any ".fbx" file format like in ImageTarget sample "teapot.fbx") load from any saved folder or in iphone from shared directory . If someone know please help me on that, this will be great help for me. Thanks in advance.

 

How to load 3d object on runtime in unity.

November 14, 2012 - 10:33pm #10

Im creating asset bundles for android(with minimum API level as 2.0.1).

Tested it by running in HTC sensation (ICS), Samsung S2 (GB).

development machine : Windows 7

Unity version: UNity pro 3.5.6 , android pro ,iphone pro

 

How to load 3d object on runtime in unity.

November 14, 2012 - 4:10pm #9

Which OS versions have you tested this with? - and which Unity version are you using?

Sorry about the forum, we're looking into that as well.

How to load 3d object on runtime in unity.

November 13, 2012 - 11:44pm #8

Hello guys,

i have posted but it appears empty on the screen.If you click on "quote", for my previous posting, yu ll get to see what i have asked /said .

How to load 3d object on runtime in unity.

November 13, 2012 - 11:41pm #7

DavidBeard wrote:

Have you tried using the non-caching method?

using System;
using UnityEngine;
using System.Collections; class NonCachingLoadExample : MonoBehaviour {
   public string BundleURL;
   public string AssetName;
   IEnumerator Start() {
	   // Download the file from the URL. It will not be saved in the Cache
	   using (WWW www = new WWW(BundleURL)) {
		   yield return www;
		   if (www.error != null)
			   throw new Exception("WWW download had an error:" + www.error);
		   AssetBundle bundle = www.assetBundle;
		   if (AssetName == "")
			   Instantiate(bundle.mainAsset);
		   else
			   Instantiate(bundle.Load(AssetName));
                   // Unload the AssetBundles compressed contents to conserve memory
                   bundle.Unload(false);
	   }
   }
}


http://docs.unity3d.com/Documentation/Manual/DownloadingAssetBundles.html

 

Hi david ,

i did try this method too.I check for the existense of asset bundle.Then i load the asset bundle.I store the variable as static..

So that i could access the asset bundle from defaulttrackableEventHandler.cs script.

 

LoadAssetBundle.cs:

public static AssetBundle bundle;

void Start()

{

StartCoroutine("loadAssetB");

}

    IEnumerator loadAssetB()
    {
        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

           if(File.Exists ("/mnt/sdcard/assetbundles/testBundle.unity3d"))
            {

         using(WWW www = new WWW ("file:///mnt/sdcard/assetbundles/testBundle.unity3d")){   
            yield return www;

            if (www.error != null)
                throw new Exception("WWW download had an error:" + www.error);
            bundle = www.assetBundle;
            Debug.Log ("loading asset bundle:"+bundle.name);
        }

File.Exists returns true but bundle .name returns empty.

 

TrackableEventHandler.cs

    private void OnTrackingFound()
    {

        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();

        // Enable rendering:
        foreach (Renderer component in rendererComponents) {
            component.enabled = true;
        }
        if (mTrackableBehaviour.TrackableName=="trackable1"){
            Debug.Log ("trackable1fort found");
            if(!LoadDs.bundle)
            {
                ab=LoadDs.bundle.Load ("testObject",typeof(GameObject)) as GameObject;
                Debug.Log ("bundle not null");
            }
            else
                Debug.Log ("bundle is null");
            go= Instantiate (ab) as GameObject;
            Debug.Log ("instantiating game object");

 

When i scan the trackable i get the following error

11-14 06:30:42.495: I/Unity(11756): ArgumentException: The thing you want to instantiate is null.
11-14 06:30:42.495: I/Unity(11756):   at UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) [0x00000] in <filename unknown>:0
11-14 06:30:42.495: I/Unity(11756):   at UnityEngine.Object.Instantiate (UnityEngine.Object original) [0x00000] in <filename unknown>:0
11-14 06:30:42.495: I/Unity(11756):   at TrackableEventHandlerDylo.OnTrackingFound () [0x00000] in <filename unknown>:0
11-14 06:30:42.495: I/Unity(11756):   at TrackableEventHandlerDylo.OnTrackableStateChanged (Status previousStatus, Status newStatus) [0x00000] in <filename unknown>:0
11-14 06:30:42.495: I/Unity(11756):   at TrackableBehaviour.OnTrackerUpdate (Status newStatus) [0x00000] in <filename unknown>:0
11-14 06:30:42.495: I/Unity(11756):   at ImageTracker.UpdateTrackablePoses (UnityEngine.Camera arCamera, .TrackableData[] trackableDataArray, Int32 originTrackableID) [0x00000] in <filename unknown>:0

 

 

i DO NOT HAVE A CLUE ABOUT WHY THE RETURNED VALUE IS NULL.

How to load 3d object on runtime in unity.

November 13, 2012 - 11:40pm #6

DavidBeard wrote:

Have you tried using the non-caching method?

using System;
using UnityEngine;
using System.Collections; class NonCachingLoadExample : MonoBehaviour {
   public string BundleURL;
   public string AssetName;
   IEnumerator Start() {
	   // Download the file from the URL. It will not be saved in the Cache
	   using (WWW www = new WWW(BundleURL)) {
		   yield return www;
		   if (www.error != null)
			   throw new Exception("WWW download had an error:" + www.error);
		   AssetBundle bundle = www.assetBundle;
		   if (AssetName == "")
			   Instantiate(bundle.mainAsset);
		   else
			   Instantiate(bundle.Load(AssetName));
                   // Unload the AssetBundles compressed contents to conserve memory
                   bundle.Unload(false);
	   }
   }
}


http://docs.unity3d.com/Documentation/Manual/DownloadingAssetBundles.html

 

Hi david ,

i did try this method too.I check for the existense of asset bundle.Then i load the asset bundle.I store the variable as static..

So that i could access the asset bundle from defaulttrackableEventHandler.cs script.

 

LoadAssetBundle.cs:

public static AssetBundle bundle;

void Start()

{

StartCoroutine("loadAssetB");

}

    IEnumerator loadAssetB()
    {
        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

           if(File.Exists ("/mnt/sdcard/assetbundles/testBundle.unity3d"))
            {

         using(WWW www = new WWW ("file:///mnt/sdcard/assetbundles/testBundle.unity3d")){   
            yield return www;

            if (www.error != null)
                throw new Exception("WWW download had an error:" + www.error);
            bundle = www.assetBundle;
            Debug.Log ("loading asset bundle:"+bundle.name);
        }

File.Exists returns true but bundle .name returns empty.

 

TrackableEventHandler.cs

    private void OnTrackingFound()
    {

        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();

        // Enable rendering:
        foreach (Renderer component in rendererComponents) {
            component.enabled = true;
        }
        if (mTrackableBehaviour.TrackableName=="trackable1"){
            Debug.Log ("trackable1fort found");
            if(!LoadDs.bundle)
            {
                ab=LoadDs.bundle.Load ("testObject",typeof(GameObject)) as GameObject;
                Debug.Log ("bundle not null");
            }
            else
                Debug.Log ("bundle is null");
            go= Instantiate (ab) as GameObject;
            Debug.Log ("instantiating game object");

 

When i scan the trackable i get the following error

11-14 06:30:42.495: I/Unity(11756): ArgumentException: The thing you want to instantiate is null.
11-14 06:30:42.495: I/Unity(11756):   at UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) [0x00000] in <filename unknown>:0
11-14 06:30:42.495: I/Unity(11756):   at UnityEngine.Object.Instantiate (UnityEngine.Object original) [0x00000] in <filename unknown>:0
11-14 06:30:42.495: I/Unity(11756):   at TrackableEventHandlerDylo.OnTrackingFound () [0x00000] in <filename unknown>:0
11-14 06:30:42.495: I/Unity(11756):   at TrackableEventHandlerDylo.OnTrackableStateChanged (Status previousStatus, Status newStatus) [0x00000] in <filename unknown>:0
11-14 06:30:42.495: I/Unity(11756):   at TrackableBehaviour.OnTrackerUpdate (Status newStatus) [0x00000] in <filename unknown>:0
11-14 06:30:42.495: I/Unity(11756):   at ImageTracker.UpdateTrackablePoses (UnityEngine.Camera arCamera, .TrackableData[] trackableDataArray, Int32 originTrackableID) [0x00000] in <filename unknown>:0

 

 

i DO NOT HAVE A CLUE ABOUT WHY THE RETURNED VALUE IS NULL.

How to load 3d object on runtime in unity.

November 11, 2012 - 6:51am #5

thanks for the reply.Let me try it if it works..

 

How to load 3d object on runtime in unity.

November 9, 2012 - 4:25pm #4

Have you tried using the non-caching method?

using System;
using UnityEngine;
using System.Collections; class NonCachingLoadExample : MonoBehaviour {
   public string BundleURL;
   public string AssetName;
   IEnumerator Start() {
	   // Download the file from the URL. It will not be saved in the Cache
	   using (WWW www = new WWW(BundleURL)) {
		   yield return www;
		   if (www.error != null)
			   throw new Exception("WWW download had an error:" + www.error);
		   AssetBundle bundle = www.assetBundle;
		   if (AssetName == "")
			   Instantiate(bundle.mainAsset);
		   else
			   Instantiate(bundle.Load(AssetName));
                   // Unload the AssetBundles compressed contents to conserve memory
                   bundle.Unload(false);
	   }
   }
}


http://docs.unity3d.com/Documentation/Manual/DownloadingAssetBundles.html

How to load 3d object on runtime in unity.

November 9, 2012 - 2:06am #3

hi david,

i try loading the assets from SD card of the android device.

i also made sure the asset bundles are created particularly for android platform.

But im getting a null value when i access the asset bundle .

I use similar to what i have typed below(.cs)

WWW www= new WWW.LoadFromCacheOrDownload("file:///mnt/sdcard/testbundle/test.unity3d",1);
yield return www;
bundle =www.assetBundle;
is(!bundle)
{
print null;
}

is there anything else i should do to get asset bundles work in android devices?

How to load 3d object on runtime in unity.

November 5, 2012 - 2:45pm #2

So will the models always be loaded on the device - you're not downloading them to the device at runtime?

For local files the best option is to use a Resources folder in your project. Otherwise you'll need to use AssetBundles, which are a Pro-only features.

See: http://docs.unity3d.com/Documentation/Manual/LoadingResourcesatRuntime.html

 

Log in or register to post comments