本文介绍了如何实例化非公共预制件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过将预制件拖入场景中的变量来实例化它们.这是代码:

I instantiate prefabs by dragging them into a variable in the scene. Here's the code:

public GameObject player1;

void Start()
{
    Instantiate(player1, spawn.position, spawn.rotation)
}

我不想拖放.我该怎么做?

I don't want to drag and drop. How do I accomplish this?

推荐答案

将预制件放入Assets/Resources文件夹.

然后您可以找到预制件,然后实例化它.

Then you can find the prefab and then instantiate it.

private GameObject player1;

void Start()
{
    player1 = Instantiate(Resources.Load("Player1"), spawn.position, spawn.rotation) as GameObject;
}

"Player1"是预制件的名称.您可以随心所欲地对其进行命名.

"Player1" is the name of the prefab. You can call it what you want.

您可以使用任何预制件或其他数据(例如纹理)来执行此操作. Unity API 上有关Resources.Load的更多信息.

You can do this with any prefab or other data, such as textures. More information on Resources.Load on Unity API.

这篇关于如何实例化非公共预制件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 16:43