Home > Software design >  How can I create 3D Object during runtime in Unity by using code or by programmatically?
How can I create 3D Object during runtime in Unity by using code or by programmatically?

Time:04-04

I am new to unity 3d world. I am making an AR app for android and iOS. I want to use unity to show 3d object in real world which is obviously via mobile camera. I am fetching data from server in the form of JSON via webservice/API. But that's not the problem, the problem is how can I create 3d object during runtime based on the JSON data. In the data, it will be define coordinates of x, y and z axis where on those I have to show 3D objects and this will change dynamically which is based on the JSON data. I need idea or hint how can I put those via coding with C#. Thank you.

CodePudding user response:

From my point of view you want hint for creating a 3D object using c# during runtime. For that you can use GameObject. code snippet:

GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); //In place of Cube you can use any 3d object like Sphere, Cylinder...
        cube.transform.position = new Vector3(0, 1.9f, 0);

the above code is in start() method.

CodePudding user response:

You could also use Instantiate() where you feed in the prefab which you want to create runtime.

// Assign this from the inspector
public GameObject ObjectToCreate;

public void GenerateObject(){
    GameObject.Instantiate(ObjectToCreate, positionFromJsonInVector3, Quaternion.identity);
}
  • Related