Log in or register to post comments

Unity help - how to create 3d Objects at run time?

August 8, 2012 - 12:44am #1

Hello,

I am trying to learn Unity as its part of my project.

I have a map downloaded from "My Trackables" and i need to show information on that map using AR. To be more specific, i parse a JSON object and i get some information for places on the map (such as lat & lon) and then i have to show a 3D object above each place, dynamically.

I 've read the sample projects and watched some videos but i am a little confused on how to start.

First of all, i created a c# script to download and store the data. Now that i have the information, i want to create 3D objects and show them on the map at specific points..

So, how do i create those objects at run time?  Number of places is about 200.. Should i create so many 3D objects?

Is there a limit on the number of virtual buttons that an AR app can have?

The project consists of an AR camera, an ImageTarget and a Directional light.
I added the first script as part of the camera in the inspector. Is this correct? Where should i add the script that creates the 3D objects?

any advice would be really helpful!

Unity help - how to create 3d Objects at run time?

November 12, 2013 - 3:48am #8

Hello!

I'm loocking same solution, could some body send me an example please?

Unity help - how to create 3d Objects at run time?

July 24, 2013 - 4:45pm #7

Yes, you'll need to use the AddComponent() functionality to add an Animation component to your game object in the scene. 

 

gameObject.AddComponent("Animation");

 

Unity help - how to create 3d Objects at run time?

July 24, 2013 - 2:46pm #6

That's great ,, i downloaded the GameObject from the server through assets Bundle and augmented it ,,, but now i want to download animation script and attach it to GameObject at RunTime ,,, is that possible ??

Unity help - how to create 3d Objects at run time?

August 9, 2012 - 10:19am #5

Great! Thank you for your time Smile

Unity help - how to create 3d Objects at run time?

August 9, 2012 - 7:26am #4

Yes.  If you're creating these at runtime you can set the parent immediately after instantiating the prefab like this (assuming your instantiate call is stored as variable t):

t.parent = gameObject;

gameObject can be any transform (like your ImageTarget), so you could store this in a variable at the start and then set all of your instantiated objects as children of this master "parent" object. 

Just set the parent property of each instance equal to your image target directly after instantiation and you should be good to go.

Unity help - how to create 3d Objects at run time?

August 8, 2012 - 11:27pm #3

Thanks for your answer! I will try to start that way

The prefab should be the child of my imagetarget in the heirarchy view?

Unity help - how to create 3d Objects at run time?

August 8, 2012 - 8:41am #2

Create a prefab of the 3D content you would like to display on your image target.  Then, use the Instantiate function to create the prefab at the location of your choosing:

C#


Transform t = (Transform) Instantiate(prefab, transform.position, transform.rotation);
  • t is the variable name you'd like to use for your 3D content
  • prefab is the name of the prefab in the Project view
  • transform.position & transform.rotation are the position at which you'd like to spawn the object, and the rotation (if any) that you'd like to apply (Quaternion.identity is a safe bet here)
  • NOTE: Be sure to cast the instantiated object as a Transform using the (Transform) type

In summary - build a prefab (create the 3D content and object heirarchy in the Heirarchy view and then drag the whole object into the Project view to create an automatic prefab), Instantiate the prefab at runtime, passing in position and rotation information in the Instantiate calls. 

 

Log in or register to post comments