unity3d 在Unity C#中从一组指定位置中选择的随机位置示例化预制件

fzsnzjdm  于 2023-02-13  发布在  C#
关注(0)|答案(1)|浏览(174)

PrefabsLocations嗨,我想知道,我如何示例化一个预置(从一个数组/列表)下的一个随机的红色方块!请帮助我

fjnneemd

fjnneemd1#

抄这个:

// move this 3 lines to top of your class
using System.Collections.Generic;
using Random = UnityEngine.Random;
using Unity.Mathematics;

// put this inside your class
public List<GameObject> prefabsList = new List<GameObject>();
public List<Transform> redSquaresList = new List<Transform>();

private void Start()
{
    var randomPrefab = Random.Range(0, prefabsList.Count);
    var randomSquare = Random.Range(0, redSquaresList.Count);
    Instantiate(prefabsList[randomPrefab], redSquaresList[randomSquare].position, quaternion.identity);
}

然后根据需要在编辑器中填充这些列表

相关问题