How to add .fbx (Human 3D object) in GameObject.Cr

2019-08-09 06:20发布

Previously I asked question and got proper solution for adding and destruction 3D object at run time.

By using GameObject.CreatePrimitive(PrimitiveType.Capsule); I can add only limited 3D object like Cube , Capsules and other

Now the problem is, I want add 3D human body .fxb object. Can I add .fbx object in below code?

Queue<GameObject> Capsules;
void Start()
{
    Capsules = new Queue<GameObject>();
}

public GameObject caps;
private void createObject()
{
    caps = GameObject.CreatePrimitive(PrimitiveType.Capsule);
    Capsules.Enqueue(caps);
}

2条回答
ゆ 、 Hurt°
2楼-- · 2019-08-09 06:50

You can do this by using prefabs. After you created your prefab in the editor you can use it in scripts using the following code:

using UnityEditor;

// Loading the prefab this way only works in the editor.
GameObject myPrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefabs/Character.prefab");

// Use this otherwise:
// using UnityEngine;
// GameObject myPrefab = Resources.Load<GameObject>("Prefabs/Character");
// Note: The Prefabs folder has to be placed in a folder named Resources.

after you have your prefab loaded you can make copies using Instantiate.

GameObject character = Object.Instantiate(myPrefab);
// Set location, rotation, ...
Capsules.Enqueue(character);
查看更多
唯我独甜
3楼-- · 2019-08-09 07:00

Long story short: YOU CAN'T!

GameObject.CreatePrimitive only creates primitives, like cubes cylinders etc. If you want to instantiate your prefab at runtime, I suggest you go look at Instantiate.

You can do this instead:

GameObject yourGameObject=whatever;
private void createObject()
{
    caps = Instantiate(yourGameObject);
    Capsules.Enqueue(caps);
}
查看更多
登录 后发表回答