简体   繁体   English

如何在Unity C#代码的GameObject.CreatePrimitive中添加.fbx(人类3D对象)?

[英]How to add .fbx (Human 3D object) in GameObject.CreatePrimitive in Unity C# code?

Previously I asked question and got proper solution for adding and destruction 3D object at run time. 之前我问过一个问题,并获得了在运行时添加和销毁3D对象的正确解决方案。

By using GameObject.CreatePrimitive(PrimitiveType.Capsule); 通过使用GameObject.CreatePrimitive(PrimitiveType.Capsule); I can add only limited 3D object like Cube , Capsules and other 我只能添加有限的3D对象,例如Cube,Capsules和其他

Now the problem is, I want add 3D human body .fxb object. 现在的问题是,我要添加3D人体.fxb对象。 Can I add .fbx object in below code? 我可以在以下代码中添加.fbx对象吗?

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

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

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 . GameObject.CreatePrimitive仅创建图元,例如立方体圆柱体等。如果要在运行时实例化预制件,建议您看一下Instantiate

You can do this instead: 您可以改为:

GameObject yourGameObject=whatever;
private void createObject()
{
    caps = Instantiate(yourGameObject);
    Capsules.Enqueue(caps);
}

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 . 加载预制件后,可以使用Instantiate进行复制。

GameObject character = Object.Instantiate(myPrefab);
// Set location, rotation, ...
Capsules.Enqueue(character);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM