using UnityEditor; using UnityEngine; public class MapSpawnTool : EditorWindow { MapMonsterCfg mapMonsterCfg; MapMonsterCfg tempMapMonsterCfg; MapSpawnShow mMapSpawnShow; bool bInit = false; Vector2 scrollPos; private void OnEnable() { GameObject tr = GameObject.Find("SpawnTools"); if (tr) DestroyImmediate(tr); GameObject go = new GameObject(); go.name = "SpawnTools"; go.transform.position = Vector3.zero; mMapSpawnShow = go.AddComponent(); } private void OnDisable() { GameObject tr = GameObject.Find("SpawnTools"); if (tr) DestroyImmediate(tr); } [MenuItem("地图/刷怪点编辑")] private static void ShowWindow() { //第一种 EditorWindow window = CreateWindow(); window.Show(); window.Focus(); } private void OnGUI() { mMapSpawnShow.mapMonsterCfg = mapMonsterCfg; if (GUILayout.Button("新建")) { //按下按钮后执行的方法 mapMonsterCfg = new MapMonsterCfg() { MapID = 1}; tempMapMonsterCfg = null; bInit = true; } tempMapMonsterCfg = EditorGUILayout.ObjectField(tempMapMonsterCfg, typeof(MapMonsterCfg), true) as MapMonsterCfg; if (GUILayout.Button("载入")) { if (tempMapMonsterCfg == null) { Debug.LogError("无选中对象"); return; } mapMonsterCfg = tempMapMonsterCfg; bInit = true; } if (!bInit) return; mapMonsterCfg.MapID = EditorGUILayout.IntField("地图ID:", mapMonsterCfg.MapID); if (GUILayout.Button("保存")) { if (!tempMapMonsterCfg) { string path = $"Assets/GameAssets/ScriptableObjectCfg/SpawnPoint/{mapMonsterCfg.MapID}.asset"; Debug.Log("以保存到文件"+ path); AssetDatabase.CreateAsset(mapMonsterCfg, path); } else { tempMapMonsterCfg = mapMonsterCfg; Debug.Log("直接编辑Asset不需要保存?"); } } EditorGUILayout.LabelField("本地图Step数量:" + mapMonsterCfg.StepList.Count); if (GUILayout.Button($" 新增Step")) { MapMonsterCfg_Step newsStep = new MapMonsterCfg_Step(); if (mapMonsterCfg.StepList != null && mapMonsterCfg.StepList.Count > 0) { var laststep = mapMonsterCfg.StepList[mapMonsterCfg.StepList.Count - 1]; newsStep.StepID = laststep.StepID + 1; } else { newsStep.StepID = 1; } mapMonsterCfg.StepList.Add(newsStep); return; } (Color, Color) bcStep = (GUI.backgroundColor, GUI.color); GUI.backgroundColor = Color.blue; GUI.color = Color.blue; EditorGUILayout.BeginVertical(); scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Height(800)); foreach (var step in mapMonsterCfg.StepList) { EditorGUILayout.LabelField($"-- Step {step.StepID} Start --"); step.StepID = EditorGUILayout.IntField("----StepID:", step.StepID); EditorGUILayout.LabelField($"---- Step {step.StepID} 数量:" + step.SpawnPoints.Count); //GUI.color = Color.white; if (GUILayout.Button($"删除Step:{step.StepID}")) { mapMonsterCfg.StepList.Remove(step); return; } (Color, Color) bcPoint = (GUI.backgroundColor, GUI.color); GUI.backgroundColor = Color.green; GUI.color = Color.green; if (GUILayout.Button($"在Step{step.StepID} 中创建一个刷怪点")) { MapMonsterCfg_Spawn newspawn = new MapMonsterCfg_Spawn(); if (step.SpawnPoints != null && step.SpawnPoints.Count > 0) { var lastspawn = step.SpawnPoints[step.SpawnPoints.Count - 1]; newspawn.SpawnPointID = lastspawn.SpawnPointID + 1; newspawn.MonsterCfgID = lastspawn.MonsterCfgID; newspawn.Pos = lastspawn.Pos + new Vector3(1f, 0, 0f); } else { newspawn.SpawnPointID = 1; newspawn.Pos = new Vector3(0, 1f, 0); } newspawn.MonsterCount = 1; step.SpawnPoints.Add(newspawn); } foreach (var point in step.SpawnPoints) { EditorGUILayout.LabelField($"Point {point.SpawnPointID} Start"); //GUI.color = Color.green; if (GUILayout.Button($"删除Point:{point.SpawnPointID}")) { step.SpawnPoints.Remove(point); return; } point.SpawnPointID = EditorGUILayout.IntField($"Point {point.SpawnPointID}->SpawnPointID:", point.SpawnPointID); point.MonsterCfgID = EditorGUILayout.IntField($"Point {point.SpawnPointID}->MonsterCfgID:", point.MonsterCfgID); point.MonsterCount = EditorGUILayout.IntField($"Point {point.SpawnPointID}->MonsterCount:", point.MonsterCount); point.Pos = EditorGUILayout.Vector3Field("Point {point.SpawnPointID}:->Pos", point.Pos); EditorGUILayout.LabelField($"Point {point.SpawnPointID} End"); } GUI.backgroundColor = bcPoint.Item1; GUI.color = bcPoint.Item2; //GUI.color = Color.white; EditorGUILayout.LabelField("-- Step 1 End--"); } GUI.backgroundColor = bcStep.Item1; GUI.color = bcStep.Item2; EditorGUILayout.EndScrollView(); EditorGUILayout.EndVertical(); } void OpenAssetPath() { string absPath = EditorUtility.OpenFilePanel("Select Inventory Item List", "", ""); if (absPath.StartsWith(Application.dataPath)) { string relPath = absPath.Substring(Application.dataPath.Length - "Assets".Length); mapMonsterCfg = AssetDatabase.LoadAssetAtPath(relPath, typeof(MapMonsterCfg)) as MapMonsterCfg; if (mapMonsterCfg) bInit = true; } } }