Net.Like.Xue.Tokyo/Packages-Local/Net.BITKit.Bounds.Unity/RandomizeTreeScale.cs

52 lines
1.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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} 棵树的大小!");
}
}