using UnityEngine; using UnityEditor; using System.Collections.Generic; using UnityEditor.SceneManagement; [CustomEditor(typeof(Grass))] public class RandomizeGrassEditor : Editor { public override void OnInspectorGUI() { base.OnInspectorGUI(); Grass grassTarget = (Grass)target; Transform parent = grassTarget.transform; if (GUILayout.Button("Randomize Grass Wind Effect")) { Undo.RecordObject(grassTarget, "Randomize Grass Wind"); List<GrassWindData> newWindData = new List<GrassWindData>(); foreach (Transform child in parent) { var renderer = child.GetComponent<MeshRenderer>(); if (!renderer) continue; float windStrength = Random.Range(grassTarget.MinWindStrength, grassTarget.MaxWindStrength); float windSpeed = Random.Range(grassTarget.MinWindSpeed, grassTarget.MaxWindSpeed); GrassWindData data = new GrassWindData { childPath = GetChildPath(child, parent), windStrength = windStrength, windSpeed = windSpeed }; newWindData.Add(data); var block = new MaterialPropertyBlock(); block.SetFloat("_Wind_Strength", windStrength); block.SetFloat("_Wind_Speed", windSpeed); renderer.SetPropertyBlock(block); } grassTarget.SetWindData(newWindData); grassTarget.SaveWindSettings(newWindData); // Save to JSON EditorUtility.SetDirty(grassTarget); EditorSceneManager.MarkSceneDirty(grassTarget.gameObject.scene); } if (GUILayout.Button("Randomize Grass Children")) { Undo.RecordObject(parent, "Randomize Grass"); foreach (Transform child in parent) { // Random Y rotation float randomYRotation = Random.Range(0f, 360f); Vector3 newRotation = new Vector3(0f, randomYRotation, 0f); // Random uniform scale float randomScale = Random.Range(grassTarget.MinGrassSize, grassTarget.MaxGrassSize); Vector3 newScale = Vector3.one * randomScale; child.localRotation = Quaternion.Euler(newRotation); child.localScale = newScale; } // Debug.Log("Randomized " + parent.childCount + " grass objects."); } } private string GetChildPath(Transform child, Transform parent) { string path = child.name; while (child.parent != null && child.parent != parent) { child = child.parent; path = child.name + "/" + path; } return path; } }