using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEditor.Animations; using System.IO; using Game; using Articy.Touhou; public class LevelGenerate : MonoBehaviour { static string LevelVoxPath = "/Resources/SceneAsset/Model_vox/"; static string LevelGroundPrefabPath = "/Resources/Prefab/LevelAssets/levelground/"; static string LevelPrefabPath = "/Resources/Prefab/Level/"; static string VoxTemplateName = "levelground0000.vox"; static string LevelGroundPrefabTemplateName = "levelground0000.prefab"; static string LevelPrefabTemplateName = "level0000.prefab"; [MenuItem("Tools/关卡资源/生成文件")] static void LevelGenerateAll() { var levelInfos = Util.GetLevelConfigDataAll(); foreach (var levelInfo in levelInfos) { Generate(levelInfo); } AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); AssetDatabase.SaveAssets(); Resources.UnloadUnusedAssets(); foreach (var levelInfo in levelInfos) { Adjust(levelInfo); } AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); AssetDatabase.SaveAssets(); Resources.UnloadUnusedAssets(); Debug.Log("LevelGenerateAll Done"); } static void Generate(Level level) { var levelInfo = level.GetFeatureLevel(); var levelAssetsName = levelInfo.Prefab; if (!levelAssetsName.StartsWith("level")) { Debug.LogError("关卡资源名字不正确:" + levelAssetsName); return; } var levelNumberStr = levelAssetsName.Substring(5); var levelVoxName = "levelground" + levelNumberStr + ".vox"; var levelGroundPrefabName = "levelground" + levelNumberStr + ".prefab"; var levelPrefabName = "level" + levelNumberStr + ".prefab"; //生成vox文件 var voxPath = Application.dataPath + LevelVoxPath + levelVoxName; if (!File.Exists(voxPath)) { var templateVoxPath = Application.dataPath + LevelVoxPath + VoxTemplateName; File.Copy(templateVoxPath, voxPath); Debug.Log("生成vox文件:" + voxPath); } //生成levelground文件 var levelGroundPrefabPath = Application.dataPath + LevelGroundPrefabPath + levelGroundPrefabName; if (!File.Exists(levelGroundPrefabPath)) { var templateLevelGroundPrefabPath = Application.dataPath + LevelGroundPrefabPath + LevelGroundPrefabTemplateName; File.Copy(templateLevelGroundPrefabPath, levelGroundPrefabPath); Debug.Log("生成levelground文件:" + levelGroundPrefabPath); } //生成level文件 var levelPrefabPath = Application.dataPath + LevelPrefabPath + levelPrefabName; if (!File.Exists(levelPrefabPath)) { var templateLevelPrefabPath = Application.dataPath + LevelPrefabPath + LevelPrefabTemplateName; File.Copy(templateLevelPrefabPath, levelPrefabPath); Debug.Log("生成level文件:" + levelPrefabPath); } } static void Adjust(Level level) { var levelInfo = level.GetFeatureLevel(); var levelAssetsName = levelInfo.Prefab; if (!levelAssetsName.StartsWith("level")) { Debug.LogError("关卡资源名字不正确:" + levelAssetsName); return; } var levelNumberStr = levelAssetsName.Substring(5); var levelVoxName = "levelground" + levelNumberStr + ".vox"; var levelGroundPrefabName = "levelground" + levelNumberStr + ".prefab"; var levelPrefabName = "level" + levelNumberStr + ".prefab"; //检查vox文件 var voxPath = Application.dataPath + LevelVoxPath + levelVoxName; if (!File.Exists(voxPath)) { Debug.LogError("vox文件不存在:" + voxPath); return; } //检查levelground文件 var levelGroundPrefabPath = Application.dataPath + LevelGroundPrefabPath + levelGroundPrefabName; if (!File.Exists(levelGroundPrefabPath)) { Debug.LogError("levelground文件不存在:" + levelGroundPrefabPath); return; } //检查level文件 var levelPrefabPath = Application.dataPath + LevelPrefabPath + levelPrefabName; if (!File.Exists(levelPrefabPath)) { Debug.LogError("level文件不存在:" + levelPrefabPath); return; } // 调整 var levelP = AssetDatabase.LoadAssetAtPath("Assets" + LevelPrefabPath + levelPrefabName); if (levelP == null) { Debug.LogError("level文件不存在:" + levelPrefabPath); return; } var levelPrefab = GameObject.Instantiate(levelP); // 调整关卡大小 var groundBlock = levelPrefab.transform.Find("Obj/_GroundBlock"); groundBlock.localScale = new Vector3(levelInfo.LevelSizeX, 1, levelInfo.LevelSizeY); // 调整空气墙大小 var airWallView = levelPrefab.transform.Find("Obj/_AirWallView"); airWallView.localScale = new Vector3(levelInfo.LevelSizeX + 0.05f, 1, levelInfo.LevelSizeY + 0.05f); // 调整关卡地面引用 var groundView = levelPrefab.transform.Find("Obj/_GroundView"); foreach (Transform child in groundView) { GameObject.DestroyImmediate(child.gameObject); } var levelGroundPrefab = AssetDatabase.LoadAssetAtPath("Assets" + LevelGroundPrefabPath + levelGroundPrefabName); var levelGround = PrefabUtility.InstantiatePrefab(levelGroundPrefab, groundView); // 调整关卡连接 var levelConnect = levelPrefab.transform.Find("Entity/_DefaultDoor"); levelConnect.localScale = new Vector3(levelInfo.LevelSizeX, 1, levelInfo.LevelSizeY); var levelConnectRoot = levelPrefab.transform.Find("Entity/_DefaultDoor/Root"); foreach (Transform child in levelConnectRoot) { switch (child.name) { case "LT": child.gameObject.SetActive(levelInfo.LevelLink_LT != null); break; case "LM": child.gameObject.SetActive(levelInfo.LevelLink_LM != null); break; case "LD": child.gameObject.SetActive(levelInfo.LevelLink_LD != null); break; case "RT": child.gameObject.SetActive(levelInfo.LevelLink_RT != null); break; case "RM": child.gameObject.SetActive(levelInfo.LevelLink_RM != null); break; case "RD": child.gameObject.SetActive(levelInfo.LevelLink_RD != null); break; case "TL": child.gameObject.SetActive(levelInfo.LevelLink_TL != null); break; case "TM": child.gameObject.SetActive(levelInfo.LevelLink_TM != null); break; case "TR": child.gameObject.SetActive(levelInfo.LevelLink_TR != null); break; case "DL": child.gameObject.SetActive(levelInfo.LevelLink_DL != null); break; case "DM": child.gameObject.SetActive(levelInfo.LevelLink_DM != null); break; case "DR": child.gameObject.SetActive(levelInfo.LevelLink_DR != null); break; default: Debug.LogError("门类型错误:" + child.name); break; } } PrefabUtility.SaveAsPrefabAsset(levelPrefab, levelPrefabPath); GameObject.DestroyImmediate(levelPrefab); } }