最近,我一直在Unity制作FPS游戏。我需要能够从我的预制文件夹中访问某些枪支。这是代码:
public class SwitchGuns : MonoBehaviour
{
public GameObject[] guns;// Holds all the gun prefabs to spawn when switching weapons
public GameObject gunParent;// The parent object of the gun
// Use this for initialization
void Start ()
{
guns = Resources.LoadAll("Prefabs/Guns/Guns");// Should load all guns into "guns" array
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Alpha1))// Switches gun to the first weapon in the "guns" array
{
spawnGun(0);
}
}
// Spawns a gun from the "guns" array
void spawnGun(int index)
{
GameObject gun = Instantiate(guns[index], gunParent.transform.position, gunParent.transform.rotation) as GameObject;// Creates a bullet at position of gun
}
}
此外,如果有人想看到路径,这里是一个 picture