Log in or register to post comments

Select 3D Model at runtime from multiple models on one ImageTarget

April 25, 2013 - 1:17am #1

Hi, I have created an Android app using Unity 3D, following this tutorial.

I want to know is there any way to add multiple 3D models in the app and display only a selected one on ImageTarget, and also be able to change 3D model at runtime?

Does unity allow this?

Please Reply...

 

Select 3D Model at runtime from multiple models on one ImageTarg

May 19, 2013 - 8:58am #10

Hi, I'm not aware of a way of directlty accessing and modifying C# script variables from Java (Android),

however, if you want to be sure,  this is a question you may want to verify directly on the Unity website.

 

Select 3D Model at runtime from multiple models on one ImageTarg

May 19, 2013 - 3:52am #9

Hi,

I am able to select one model among different by modifying the DefaultTrackableEventHandler like this.

 

private void OnTrackingFound()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
        Collider[] colliderComponents = GetComponentsInChildren<Collider>();

        /* Enable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = true;
        }*/
		rendererComponents[0].enabled = true;
		rendererComponents[1].enabled = false;

        /* Enable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = true;
        }*/
		colliderComponents[0].enabled = true;
		colliderComponents[1].enabled = false;

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    }

Now I want to modify the value of 

rendererComponents[0].enabled = true;
rendererComponents[1].enabled = false;
colliderComponents[0].enabled = true;
colliderComponents[1].enabled = false;

i.e. true or false from java i.e. Android. Is there any way to access these values and modify them in Java (Android) Eclipse project?

 

Select 3D Model at runtime from multiple models on one ImageTarg

April 30, 2013 - 5:59am #8

Hi, this is due to the GetComponentsInChildren, which actually returns all the renderers for all the objects that are attached under the Target.

You need to adapt that sample code snippet to only take the renderer of a specific gameObject.

For example, if you have gameObject1 and gameObject2, you can retreieve the renderers of gameObject1 with something like:

gameObject1.GetComponentsInChildren<Renderer>();

 

Select 3D Model at runtime from multiple models on one ImageTarg

April 30, 2013 - 4:31am #7

When I set component.enabled = false; none of the models is displayed at ImageTarget may be

because of foreach loop???

Select 3D Model at runtime from multiple models on one ImageTarg

April 30, 2013 - 3:28am #6

Hi, the best is then (at runtime, i.e. via script) to only enable the rendering of one of those models, and disabling all the others;

this is simpler than attaching / detaching objects to/from the scenegraph at runtime (although that's also a viable technique).

That's why i was suggesting to use this code (to make objects visible and invisible):

 

Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
        Collider[] colliderComponents = GetComponentsInChildren<Collider>();
 
        // Disable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = false;
        }
 
        // Disable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = false;
        }
 

Otherwise you can also instantiate ojects from Prefabs at runtime and then attach them under the image target objects;

this is discussed in this Unity page;

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

 

 

 

 
 

Select 3D Model at runtime from multiple models on one ImageTarg

April 30, 2013 - 3:22am #5

What you have said it will only allow me to select a model at compile or coding time and what I want is to be able to select among different models at run time in android. I have added two models on ImageTarget in Hierrchy View and they appear in android app as shown in the attached image.

I want to display only one model at a time and be able to select among these models at runtime in android.

Is there any way?

Select 3D Model at runtime from multiple models on one ImageTarg

April 29, 2013 - 11:47pm #4

That would be just a matter of dragging the models (prefabs) from the project view into the image target node in the hierarchical scene view.

Select 3D Model at runtime from multiple models on one ImageTarg

April 29, 2013 - 11:45pm #3

Can you please explain how multiple 3D models be attached as children of the ImageTarget in the scene view?

Thanks in advance...

Select 3D Model at runtime from multiple models on one ImageTarg

April 25, 2013 - 2:28am #2

Hi,

yes, this is possible; 3D models can be attached as children of the ImageTarget in the scene view, or dynamically via script code; you can also then enabled/disable objects so to show only a selelected one and hide the others.

For example, the DefaultTrackableEventHandler in the ImageTargets sample shows how to make an object invisible by disabling its rendering components, like in this code:

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

        // Disable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = false;
        }

        // Disable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = false;
        }

In general, you have plenty of possiblities, but this is just about Unity programming.

 

Log in or register to post comments