Unity3D 自定义类的数组初始化
实现功能:
1. 自定义类,用于保存数据等
2. 初始化数组
代码:
public class tree_elem { // 位置 public int x, y; // 大小【相对于原始大小的比例】最后随机分配 public float size; // 树的类型,最后随机分配 public tree_kind kind; public tree_elem(int x, int y) { this.x = x; this.y = y; this.size = UnityEngine.Random.Range(0.5f, 2.0f); this.kind = (tree_kind)(UnityEngine.Random.Range(0, 100) % 3); } } public class MapCreator : MonoBehaviour { [Header("生成的面积尺寸")] public Vector2 create_map_size = new Vector2(100, 100); private tree_elem[] tree_elements; // Start is called before the first frame update void Start() { Debug.Log("生成模式:normal,利用random随机数,纯随机"); int length = create_map_size.x; int heigth = create_map_size.y; int max_length = length * heigth; tree_elements = new tree_elem[max_length]; for (int i = 0; i < max_length; i++) { // 赋值数据 tree_elements[i] = new tree_elem(i / length, i % heigth); } } }