52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
using UnityEngine;
|
||
using UnityEditor;
|
||
|
||
public class RandomizeTreeScale : EditorWindow
|
||
{
|
||
Terrain terrain;
|
||
float minScale = 0.8f;
|
||
float maxScale = 1.2f;
|
||
|
||
[MenuItem("Tools/Terrain/随机调整树大小")]
|
||
static void Init()
|
||
{
|
||
GetWindow<RandomizeTreeScale>("随机树大小");
|
||
}
|
||
|
||
void OnGUI()
|
||
{
|
||
GUILayout.Label("🌲 调整 Terrain 上树的大小", EditorStyles.boldLabel);
|
||
|
||
terrain = (Terrain)EditorGUILayout.ObjectField("目标 Terrain", terrain, typeof(Terrain), true);
|
||
minScale = EditorGUILayout.FloatField("最小缩放", minScale);
|
||
maxScale = EditorGUILayout.FloatField("最大缩放", maxScale);
|
||
|
||
if (GUILayout.Button("✅ 执行随机缩放"))
|
||
{
|
||
if (terrain == null)
|
||
{
|
||
Debug.LogWarning("请指定 Terrain!");
|
||
return;
|
||
}
|
||
|
||
ApplyRandomScale();
|
||
}
|
||
}
|
||
|
||
void ApplyRandomScale()
|
||
{
|
||
var data = terrain.terrainData;
|
||
var trees = data.treeInstances;
|
||
|
||
for (int i = 0; i < trees.Length; i++)
|
||
{
|
||
float scale = Random.Range(minScale, maxScale);
|
||
trees[i].widthScale = scale;
|
||
trees[i].heightScale = scale;
|
||
}
|
||
|
||
data.treeInstances = trees;
|
||
|
||
Debug.Log($"🌲 成功调整了 {trees.Length} 棵树的大小!");
|
||
}
|
||
} |