创建一些特定的目录结构

如果做固定开发,经常会遇到一些固定模式,例如开发某个功能,要创建对应的文件夹,名字都是有套路的,此时就可以使用工具来一键生成,节省时间。

/*--------------------------------------------------------------------

  • Author Name: DXL
  • Creation Time: 2019.1.15
  • File Describe: 创建场景的结构路径,用于新场景开发
  • ------------------------------------------------------------------*/

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using LogPrint;
using System.Text;

public class CreateScenePath
{
static EditorWindow m_singleSceneWindow;

//开发新场景时直接创建对应结构目录    
public static void CreateSceneFile(string scene)
{
    //------------------------都在Assets文件夹下-------------

    //1、先检测AnimationFile的XXX_Ani文件夹
    string animaAni = Application.dataPath + "/AnimationFile" + "/" + scene + "_Ani";
    if (!Directory.Exists(animaAni))
    {
        //创建一个文件夹
        Directory.CreateDirectory(animaAni);
    }

    //2、创建Prefab下的XXX_Prefab文件夹 
    string scenePrefab = Application.dataPath + "/Prefab" + "/" + scene + "_Prefab";
    if (!Directory.Exists(scenePrefab))
    {
        //创建一个文件夹
        Directory.CreateDirectory(scenePrefab);
    }

    //3、创建Sound下的XXX_Sound文件夹 
    string sceneSound = Application.dataPath + "/Sound" + "/" + scene + "_Sound";
    if (!Directory.Exists(sceneSound))
    {
        //创建一个文件夹
        Directory.CreateDirectory(sceneSound);
    }

    //4、创建Texture下的XXX_Texture文件夹 
    string sceneTexture = Application.dataPath + "/Texture" + "/" + scene + "_Texture";
    if (!Directory.Exists(sceneTexture))
    {
        //创建一个文件夹
        Directory.CreateDirectory(sceneTexture);
    }
    //Texture文件夹中的BackGround文件夹
    string textureBG = sceneTexture + "/BackGround";
    if (!Directory.Exists(textureBG))
    {
        //创建一个文件夹
        Directory.CreateDirectory(textureBG);
    }
    //Texture文件夹中的UI文件夹
    string textureUI = sceneTexture + "/UI";
    if (!Directory.Exists(textureUI))
    {
        //创建一个文件夹
        Directory.CreateDirectory(textureUI);
    }

    //7、Script文件夹 \Scripts\SceneLogic\ XXX
    string scriptFloder = Application.dataPath + "/Scripts/SceneLogic/" + scene;
    if (!Directory.Exists(scriptFloder))
    {
        //创建一个文件夹
        Directory.CreateDirectory(scriptFloder);
    }

    //5、ConfigXlsx文件夹下的XXXModel.xlsx文件和XXXScene.xlsx文件,就手动创建吧,因为里面包含特殊字段
    //6、Resources/TestScene文件夹下对应的XXX_Prefab也手动创建,这个要等运行时手动拖动
    m_singleSceneWindow.Close();
    AssetDatabase.Refresh();
    Debug.Log(scene + " Struct Path create success!");

}


//打开界面
[MenuItem("HideUIRaycast/CreateSceneStructurePath")]
private static void ShowWindow()
{
    m_singleSceneWindow = EditorWindow.GetWindowWithRect(typeof(CreateSceneWindow), new Rect(0, 0, 400, 600), true, "SingleSceneWindow");
}


//打开测试界面    
public static void ShowSceneInfoWindow()
{
    if (m_singleSceneWindow) m_singleSceneWindow.Close();
    m_singleSceneWindow = EditorWindow.GetWindowWithRect(typeof(SceneInfoWindow), new Rect(0, 0, 400, 600), true, "SingleSceneWindow");
}

//Spine资源地址
static string modelSourcePath = Application.dataPath + "/AnimationFile" + "/" + "FourElves" + "_Ani";
//Audio资源地址
static string audioSourcePath = Application.dataPath + "/Sound" + "/" + "FourElves" + "_Sound";
//测试场景ModelExcel地址
static string testModelExcelPath = Application.dataPath + "/ConfigXlsx/Model/" + "LittlePoliceModel.xlsx";
//测试场景SceneExcel地址
static string testSceneExcelPath = Application.dataPath + "/ConfigXlsx/Scene/" + "LittlePoliceScene.xlsx";
//练习场景ModelExcel地址
static string practiceModelExcelPath = Application.dataPath + "/ConfigXlsx/Model/" + "TakenModel.xlsx";
//练习场景SceneExcel地址
static string practiceSceneExcelPath = Application.dataPath + "/ConfigXlsx/Scene/" + "TakenScene.xlsx";
//KeepBg预制体的地址
static string keepBgPath = Application.dataPath + "/Prefab" + "/" + "FourElves" + "_Prefab/KeepBg.prefab";
static int createFileNum;              //创建文件的数量

//传递3个参数,场景名字,要创建的模型名字,是否是测试场景(如果不是,就是练习场景)
public static void CreateSceneFile2(string scene, List<string> modelList, bool isTestScene)
{
    //------------------------都在Assets文件夹下-------------
    createFileNum = 0;

    //1、先检测AnimationFile的XXX_Ani文件夹
    string animaAni = Application.dataPath + "/AnimationFile" + "/" + scene + "_Ani";
    if (!Directory.Exists(animaAni))
    {
        //创建一个文件夹
        Directory.CreateDirectory(animaAni);
        createFileNum++;
    }

    //1.2创建AnimationFile下的模型资源       
    for (int i = 0; i < modelList.Count; ++i)
    {
        if (!Directory.Exists(animaAni + "/" + modelList[i]))
        {
            Directory.CreateDirectory(animaAni + "/" + modelList[i]);
            createFileNum++;
        }

        CopyAniFile(scene, modelList[i]);
    }

    //2、创建Prefab下的XXX_Prefab文件夹 
    string scenePrefab = Application.dataPath + "/Prefab" + "/" + scene + "_Prefab";
    if (!Directory.Exists(scenePrefab))
    {
        //创建一个文件夹
        Directory.CreateDirectory(scenePrefab);
        createFileNum++;
    }

    //3、创建Sound下的XXX_Sound文件夹 
    string sceneSound = Application.dataPath + "/Sound" + "/" + scene + "_Sound";
    if (!Directory.Exists(sceneSound))
    {
        //创建一个文件夹
        Directory.CreateDirectory(sceneSound);
        createFileNum++;
    }
    //3.2创建下面对应的子文件夹
    for (int i = 0; i < modelList.Count; ++i)
    {
        if (!Directory.Exists(sceneSound + "/" + modelList[i]))
        {
            //创建一个文件夹
            Directory.CreateDirectory(sceneSound + "/" + modelList[i]);
            createFileNum++;
        }
    }
    //3.3 将一些经常用到的音效文件,例如ClickEffect.mp3这样音效复制进来
    if (!Directory.Exists(sceneSound + "/BasicAudio"))
    {
        //创建一个文件夹
        Directory.CreateDirectory(sceneSound + "/BasicAudio");
        createFileNum++;
    }
    CopyAudioFile(scene);


    //4、创建Texture下的XXX_Texture文件夹 
    string sceneTexture = Application.dataPath + "/Texture" + "/" + scene + "_Texture";
    if (!Directory.Exists(sceneTexture))
    {
        //创建一个文件夹
        Directory.CreateDirectory(sceneTexture);
        createFileNum++;
    }
    //Texture文件夹中的BackGround文件夹
    string textureBG = sceneTexture + "/BackGround";
    if (!Directory.Exists(textureBG))
    {
        //创建一个文件夹
        Directory.CreateDirectory(textureBG);
        createFileNum++;
    }
    //Texture文件夹中的UI文件夹
    string textureUI = sceneTexture + "/UI";
    if (!Directory.Exists(textureUI))
    {
        //创建一个文件夹
        Directory.CreateDirectory(textureUI);
        createFileNum++;
    }

    //7、Script文件夹 \Scripts\SceneLogic\ XXX
    string scriptFloder = Application.dataPath + "/Scripts/SceneLogic/" + scene;
    if (!Directory.Exists(scriptFloder))
    {
        //创建一个文件夹
        Directory.CreateDirectory(scriptFloder);
        createFileNum++;
    }
    //7.2创建对应 的cs脚本文件
    CreateScriptFile(scene, scriptFloder);

    //5、创建对应的Excel文件
    CreateExcelFile(scene, isTestScene);

    //6、创建经常用的 Prefab预制体
    CreatePrefab(scene);

    if (createFileNum > 0)
    {
        Debug.Log(scene + " Struct Path create " + createFileNum + " filse success!");
    }
    else
    {
        Debug.Log(scene + " had already been created,so not create new file");
    }

    m_singleSceneWindow.Close();
    AssetDatabase.Refresh();

    //这里可以创建对应的Spine预制体和打图集 todo
}

//复制Spine的资源
static void CopyAniFile(string sceneName, string targetDirName)
{
    //获取文件夹下所有文件
    string[] files = Directory.GetFiles(modelSourcePath + "/" + targetDirName);
    for (int j = 0; j < files.Length; ++j)
    {
        if (files[j].Contains(".meta"))
        {
            continue;
        }

        string target = files[j].Replace("FourElves_Ani", sceneName + "_Ani");
        if (files[j].Contains(".atlas.txt"))
        {
            //这里要检测是否已经存在当前文件了,正常情况下是没有的
            if (!File.Exists(target))
            {
                File.Copy(files[j], target);
                createFileNum++;
            }
        }
        else if (files[j].Contains(".json"))
        {
            if (!File.Exists(target))
            {
                File.Copy(files[j], target);
                createFileNum++;
            }
        }
        else if (files[j].Contains(".png"))
        {
            if (!File.Exists(target))
            {
                File.Copy(files[j], target);
                createFileNum++;
            }
        }
        else if (files[j].Contains(".controller"))
        {
            if (!File.Exists(target))
            {
                File.Copy(files[j], target);
                createFileNum++;
            }
        }
    }
}

//复制mp3文件到目录中
static void CopyAudioFile(string sceneName)
{
    string[] files = Directory.GetFiles(audioSourcePath);

    for (int i = 0; i < files.Length; ++i)
    {
        //这里仅仅针对mp3资源,ogg音效暂时不考虑
        if (files[i].Contains("meta") || !files[i].Contains("mp3"))
        {
            continue;
        }

        string target = files[i].Replace("FourElves_Sound", sceneName + "_Sound/BasicAudio");
        if (!File.Exists(target))
        {
            File.Copy(files[i], target);
            createFileNum++;
        }
    }

}

//创建一个cs脚本文件
static void CreateScriptFile(string sceneName, string scriptFolderPath)
{
    if (!File.Exists(scriptFolderPath + "/" + sceneName + "Scene.cs"))
    {
        using (FileStream fs = new FileStream(scriptFolderPath + "/" + sceneName + "Scene.cs", FileMode.Create, FileAccess.Write))
        {
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("UTF-8"));

            sw.WriteLine("using System.Collections;");
            sw.WriteLine("using System.Collections.Generic;");
            sw.WriteLine("using UnityEngine;");
            sw.WriteLine("");
            sw.WriteLine("public class " + sceneName + "Scene : MonoBehaviour");
            sw.WriteLine("{");
            sw.WriteLine("");
            sw.WriteLine("\t#region variable");
            sw.WriteLine("");
            sw.WriteLine("\t#endregion");
            sw.WriteLine("");
            sw.WriteLine("\t#region ui");
            sw.WriteLine("");
            sw.WriteLine("\tvoid Start ()");
            sw.WriteLine("\t{");
            sw.WriteLine("");
            sw.WriteLine("\t}");
            sw.WriteLine("");
            sw.WriteLine("\tvoid Update ()");
            sw.WriteLine("\t{");
            sw.WriteLine("");
            sw.WriteLine("\t}");
            sw.WriteLine("");
            sw.WriteLine("\t#endregion");
            sw.WriteLine("}");

            sw.Close();

        }
        createFileNum++;
    }
}

//创建2个Excel
static void CreateExcelFile(string sceneName, bool isTest)
{
    if (isTest)
    {
        //是测试场景,有学习卡

        string targetModel = testModelExcelPath.Replace("LittlePolice", sceneName);
        if (!File.Exists(targetModel))
        {
            File.Copy(testModelExcelPath, targetModel);
            createFileNum++;
        }

        string targetScene = testSceneExcelPath.Replace("LittlePolice", sceneName);
        if (!File.Exists(targetScene))
        {
            File.Copy(testSceneExcelPath, targetScene);
            createFileNum++;
        }

    }
    else
    {
        //是练习场景,有碎片的

        string targetModel = practiceModelExcelPath.Replace("Taken", sceneName);
        if (!File.Exists(targetModel))
        {
            File.Copy(practiceModelExcelPath, targetModel);
            createFileNum++;
        }

        string targetScene = practiceSceneExcelPath.Replace("Taken", sceneName);
        if (!File.Exists(targetScene))
        {
            File.Copy(practiceSceneExcelPath, targetScene);
            createFileNum++;
        }
    }
}

//创建一些经常用的预制体
static void CreatePrefab(string sceneName)
{
    //创建一个KeepBg预制体
    string target = keepBgPath.Replace("FourElves", sceneName);
    if(!File.Exists(target))
    {
        File.Copy(keepBgPath, target);
        createFileNum++;
    }
}

//打开界面
//[MenuItem("HideUIRaycast/TestEditorPath")]
static void TestEditorAction()
{
    //CreateScriptFile("MagicTest2", @"E:\SVN2\yxxj_optimize\Assets\Scripts\SceneLogic\MagicMask");
    //AssetDatabase.Refresh();
}

}

public class CreateSceneWindow : EditorWindow
{
static bool[] ShowArray = new bool[400];

Vector2 m_scrollPos = Vector2.zero;

string[] m_scenes;
int toggleIndex = -1;            //当前选中的场景id

void OnGUI()
{
    if (GUILayout.Button("Create", GUILayout.Width(100), GUILayout.Height(50)))
    {
        //打包
        if (toggleIndex < 0)
        {
            LogSystem.Log("Please choose scene!");
            return;
        }

        //CreateScenePath.CreateSceneFile(m_scenes[toggleIndex]);

        SceneInfoWindow.sceneName = m_scenes[toggleIndex];
        CreateScenePath.ShowSceneInfoWindow();

    }

    m_scrollPos = GUILayout.BeginScrollView(m_scrollPos);
    GUILayout.BeginVertical();
    m_scenes = System.Enum.GetNames(typeof(SceneType));


    for (int i = m_scenes.Length - 1; i >= 0; --i)
    {
        if (m_scenes[i].Contains("MainScene") || m_scenes[i].Contains("PipiHome"))
        {
            continue;
        }

        //ShowArray[i] = EditorGUILayout.ToggleLeft(m_scenes[i], ShowArray[i]);
        ShowArray[i] = EditorGUILayout.ToggleLeft(m_scenes[i], toggleIndex == i);
        if (ShowArray[i])
        {
            toggleIndex = i;
        }

    }

    GUILayout.EndVertical();

    GUILayout.EndScrollView();
}


private void OnDisable()
{
    Clean();
}

public static void Clean()
{
    //SceneNameList.Clear();

    for (int i = 0; i < ShowArray.Length; ++i)
    {
        ShowArray[i] = false;
    }
}

}

//场景信息设置面板
public class SceneInfoWindow : EditorWindow
{
public static string sceneName = “”; //当前要创建的场景名字
List modelList = new List(); //要创建的模型名单

Vector2 m_scrollPos = Vector2.zero;

//--------------------------
bool haveMeiMei;                 //是否创建美美这个模型
bool haveNiuZhenZhang;
bool haveTiger;
bool haveElves;                 //是否创建小精灵这个模型,包含小黄,小红。。。
bool haveJingJing;
bool haveWolf;
bool haveBabu;
bool haveMiaoDa;
bool haveElephant;
bool haveElephantFight;
bool haveSmoke;

bool isTestScene;               //是否是测试场景,不是测试场景,就是练习场景
//----------------------------


void OnGUI()
{
    m_scrollPos = GUILayout.BeginScrollView(m_scrollPos);
    GUILayout.BeginVertical();

    EditorGUILayout.Space();
    EditorGUILayout.Space();
    EditorGUILayout.LabelField("                       请以下选择创建的模型", GUILayout.Height(20));
    EditorGUILayout.LabelField("--------------------------------------------------------------");
    EditorGUILayout.Space();
    haveMeiMei = EditorGUILayout.ToggleLeft("美美", haveMeiMei);
    haveNiuZhenZhang = EditorGUILayout.ToggleLeft("牛镇长", haveNiuZhenZhang);
    haveTiger = EditorGUILayout.ToggleLeft("小虎", haveTiger);
    haveElves = EditorGUILayout.ToggleLeft("小精灵", haveElves);
    haveJingJing = EditorGUILayout.ToggleLeft("静静", haveJingJing);
    haveWolf = EditorGUILayout.ToggleLeft("灰狼", haveWolf);
    haveBabu = EditorGUILayout.ToggleLeft("巴布", haveBabu);
    haveMiaoDa = EditorGUILayout.ToggleLeft("喵达", haveMiaoDa);
    haveElephant = EditorGUILayout.ToggleLeft("大象警察", haveElephant);
    haveElephantFight = EditorGUILayout.ToggleLeft("大象与老狼搏斗", haveElephantFight);
    haveSmoke = EditorGUILayout.ToggleLeft("烟雾", haveSmoke);

    EditorGUILayout.Space();
    EditorGUILayout.Space();
    EditorGUILayout.LabelField("--------------------------------------------------------------");
    EditorGUILayout.Space();
    isTestScene = EditorGUILayout.ToggleLeft("是否是测试场景", isTestScene);
    EditorGUILayout.Space();
    EditorGUILayout.Space();
    EditorGUILayout.Space();
    EditorGUILayout.Space();



    EditorGUILayout.LabelField("     当前创建场景: " + sceneName);
    EditorGUILayout.LabelField("--------------------------------------------------------------");
    EditorGUILayout.Space();
    EditorGUILayout.Space();
    if (GUILayout.Button("Create", GUILayout.Width(100), GUILayout.Height(50)))
    {
        //开始创建对应的场景结构路径 todo
        if (haveMeiMei) modelList.Add("MeiMei");
        if (haveNiuZhenZhang) modelList.Add("NiuZhenZhang");
        if (haveTiger) modelList.Add("Tiger");
        if (haveElves) modelList.Add("Elves");
        if (haveJingJing) modelList.Add("JingJing");
        if (haveWolf) modelList.Add("Wolf");
        if (haveBabu) modelList.Add("Babu");
        if (haveMiaoDa) modelList.Add("MiaoDa");
        if (haveElephant) modelList.Add("Elephant");
        if (haveElephantFight) modelList.Add("ElephantFight");
        if (haveSmoke) modelList.Add("Smoke");

        CreateScenePath.CreateSceneFile2(sceneName, modelList, isTestScene);
    }

    GUILayout.EndVertical();
    GUILayout.EndScrollView();
}

void OnDisable()
{
    sceneName = "";
    modelList.Clear();
    isTestScene = false;
}

}

这是个编辑器界面功能,有缺陷,但是可以使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值