53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
using UnityEditor;
|
|
#endif
|
|
using UnityEngine;
|
|
|
|
[ExecuteInEditMode]
|
|
public class BackBroadEditor : MonoBehaviour
|
|
{
|
|
#if UNITY_EDITOR
|
|
public List<GameObject> prefabs; // 棋盘格子Prefab
|
|
public int width = 10; // 棋盘宽度
|
|
public int height = 5; // 棋盘高度
|
|
public float spacing = 1f; // 格子之间的间距
|
|
|
|
public void GenerateBoard()
|
|
{
|
|
// 清除现有的子对象
|
|
foreach (Transform child in transform)
|
|
{
|
|
DestroyImmediate(child.gameObject);
|
|
}
|
|
|
|
// 生成新的棋盘
|
|
for (int x = 0; x < width; x++)
|
|
{
|
|
for (int y = 0; y < height; y++)
|
|
{
|
|
Vector3 position = new Vector3(x * spacing, y * spacing,0 );
|
|
GameObject obj = GetRandomSelection(prefabs);
|
|
GameObject broad = PrefabUtility.InstantiatePrefab(obj) as GameObject;
|
|
broad.transform.SetParent(this.transform);
|
|
broad.transform.localPosition = position;
|
|
}
|
|
}
|
|
}
|
|
|
|
private GameObject GetRandomSelection(List<GameObject> list)
|
|
{
|
|
System.Random random = new System.Random();
|
|
int n = list.Count;
|
|
int randomNumber = random.Next(0, n);
|
|
return list[randomNumber];
|
|
}
|
|
#endif
|
|
}
|