其他分享
首页 > 其他分享> > Get获取信息写入配置表,整合资源

Get获取信息写入配置表,整合资源

作者:互联网

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using LitJson;
using UnityEngine.UI;
using ZHCTOOLS;

/// <summary>
/// 添加自动step主脚本的依赖信息
/// </summary>
public class ZHCTools : EditorWindow
{
    #region 写入配置信息

    [MenuItem("ZHCTools/Tools")]
    public static void ConfigDialog()
    {
        GetWindow(typeof(ZHCTools));
    }

    public GameObject go;

    void OnGUI()
    {
        if (GUILayout.Button("写入配置信息"))
        {
            GetName();
        }
    }

    public bool CreatPrefab()
    {
        Transform[] selectionObj = Selection.transforms;
        List<string> PrefbaFilePath = new List<string>();
        PrefbaFilePath.Clear();
        Debug.Log(selectionObj.Length);
        if (selectionObj.Length == 0)
        {
            Debug.Log("没有生成预制体因为你没有选中");
            return false;
        }

        for (int i = 0; i < selectionObj.Length; i++)
        {
            string path = "Assets/实验升级文件/资源文件/" + selectionObj[i].name + ".prefab" + "";
            PrefabUtility.CreatePrefab(path, selectionObj[i].gameObject);
            PrefbaFilePath.Add(path);
            Debug.Log(path);
        }

        ResCopy(PrefbaFilePath);
        return true;
    }

    void ResCopy(List<string> prefabpath)
    {
        string[] path = new string[0];
        List<string> prefabName = new List<string>();

        for (int i = 0; i < prefabpath.Count; i++)
        {
            path = AssetDatabase.GetDependencies(prefabpath[i]);
            Debug.Log(path);
        }

        for (int j = 0; j < path.Length; j++)
        {
            string[] TempName = path[j].Split('/');
            string objname = TempName[TempName.Length - 1];
            for (int i = 0; i < zhcCofigScript.suffix.Count; i++)
            {
                if (objname.Contains(zhcCofigScript.suffix[i]))
                {
                    if (!objname.Contains(".cs") && !objname.Contains(".txt") && !objname.Contains(".dll"))
                    {
                        AssetDatabase.CopyAsset(path[j],
                            "Assets/实验升级文件/资源文件/依赖文件/" + zhcCofigScript.suffix[i] + "/" + objname);
                        EditorUtility.DisplayProgressBar("正在努力的生成中", "数据生成中。。。" + objname,
                            (float) j / path.Length);
                    }
                }
            }
        }
    }

    [MenuItem("ZHCTools/test")]
    static void ss()
    {
        string[] str = AssetDatabase.GetDependencies("Assets/Sctipts/core/StepMethodControl/StepOparationMethod.cs");
        for (int i = 0; i < str.Length; i++)
        {
            Debug.Log(str[i]);
        }
    }

    private ZHCCofigScript zhcCofigScript;

    public void GetName()
    {
        Debug.Log(File.Exists(Application.dataPath + "/ZHCTOOLS/"));


        if (File.Exists(Application.dataPath + "/ZHCTOOLS/MainConfig.asset"))
        {
            zhcCofigScript = AssetDatabase.LoadAssetAtPath<ZHCCofigScript>("Assets/ZHCTOOLS/MainConfig.asset");
            if (zhcCofigScript.suffix.Count == 0)
            {
                zhcCofigScript.suffix.Add("png");
                zhcCofigScript.suffix.Add("jpg");
                zhcCofigScript.suffix.Add("prefab");
                zhcCofigScript.suffix.Add("fbx");
                zhcCofigScript.suffix.Add("shader");
                zhcCofigScript.suffix.Add("mat");
            }

            try
            {
                CreatFile();
                GetAnglePosRot(zhcCofigScript.config);
                GetTechItemArray(zhcCofigScript.config);
                GetStep(zhcCofigScript.config);
                CreateJson(zhcCofigScript.config);
                CopAudio(zhcCofigScript.StreamingAssetsAudioFilepath, zhcCofigScript.NewAudioFilePath);
                if (zhcCofigScript.isbuilder)
                {
                    CreatPrefab();
                }

                EditorUtility.ClearProgressBar();
            }
            catch (Exception e)
            {
                EditorUtility.ClearProgressBar();
                throw new WarningException(e.Message);
            }
        }
        else
        {
            EditorUtility.DisplayDialog("警告",
                "请在Assets/ZHCTOOLS/目录下生成:MainConfig.asset配置文件,位置在:poroject面板--右键ZHCTOOLS文件--Create--ZHCConfigScript,用于可视化信息以及目录生成,最好用我默认的信息,你的路径我不管确保我的路径不要动",
                "OK");
        }
    }

    /// <summary>
    /// 创建文件夹
    /// </summary>
    void CreatFile()
    {
        AssetDatabase.Refresh();
        if (!Directory.Exists(Application.dataPath + "/实验升级文件"))
        {
            AssetDatabase.CreateFolder("Assets", "实验升级文件");
            AssetDatabase.CreateFolder("Assets/实验升级文件", "音频文件");
            AssetDatabase.CreateFolder("Assets/实验升级文件", "资源文件");
            AssetDatabase.CreateFolder("Assets/实验升级文件/资源文件", "依赖文件");

            for (int i = 0; i < zhcCofigScript.suffix.Count; i++)
            {
                AssetDatabase.CreateFolder("Assets/实验升级文件/资源文件/依赖文件", zhcCofigScript.suffix[i]);
            }

            Directory.CreateDirectory(Application.dataPath + zhcCofigScript.MudiPath);

            EditorUtility.DisplayDialog("", "请确保你大哥我生成的文件夹没给我删除了,我很多位置可没写路径判定,删除了自动配置是配置不了的", "OK");
        }
    }

    /// <summary>
    /// 生成一些视角的json
    /// </summary>
    /// <param name="config"></param>
    void GetAnglePosRot(ZHCCofigScript.Config config)
    {
        Transform[] go = new Transform[zhcCofigScript.FindAnglePos.Length];
        for (int i = 0; i < zhcCofigScript.FindAnglePos.Length; i++)
        {
            go[i] = GameObject.Find(zhcCofigScript.FindAnglePos[i]).transform;
        }

        Transform[] AutoAnglePos = FindObjectOfType<AngleControl>().cameraPoss;

        config.AutoAnglePos = new ZHCCofigScript.SetOBJData[AutoAnglePos.Length];
        config.AnglePos = new ZHCCofigScript.SetOBJData[go.Length];


        GetAngle(config.AnglePos, go);
        GetAngle(config.AutoAnglePos, AutoAnglePos);

        MoveAndLook moveAndLook = FindObjectOfType<MoveAndLook>();
        config.ManYouName = new String[moveAndLook.wuti.Length];
        for (int i = 0; i < moveAndLook.wuti.Length; i++)
        {
            config.ManYouName[i] = moveAndLook.wuti[i].name;
        }
    }

    public void GetAngle(ZHCCofigScript.SetOBJData[] getDatas, Transform[] go)
    {
        for (int i = 0; i < go.Length; i++)
        {
            getDatas[i] = new ZHCCofigScript.SetOBJData();
            getDatas[i].name = go[i].name;
            getDatas[i].m_pos.x = go[i].transform.localPosition.x.ToString();
            getDatas[i].m_pos.y = go[i].transform.localPosition.y.ToString();
            getDatas[i].m_pos.z = go[i].transform.localPosition.z.ToString();

            getDatas[i].m_rot.x = go[i].transform.localRotation.eulerAngles.x.ToString();
            getDatas[i].m_rot.y = go[i].transform.localRotation.eulerAngles.y.ToString();
            getDatas[i].m_rot.z = go[i].transform.localRotation.eulerAngles.z.ToString();
        }
    }

    /// <summary>
    /// 生成目的原理图json和自动拷贝目的原理资源文件
    /// </summary>
    /// <param name="config"></param>
    void GetTechItemArray(ZHCCofigScript.Config config)
    {
        List<string> spriteName = new List<string>();
        try
        {
            List<TeachItemClass> teachItemArray = FindObjectOfType<TeachSet>().teachItemArray;
            config.teachList = new TeachArray();
            for (int i = 0; i < teachItemArray.Count; i++)
            {
                config.teachList.teachArray.Add(new TeachInfo());
                config.teachList.teachArray[i].teachname = teachItemArray[i].Name;
                for (int j = 0; j < teachItemArray[i].infoArray.Count; j++)
                {
                    config.teachList.teachArray[i].teachInfoImgs.Add(new TeachInfoImg());
                    config.teachList.teachArray[i].teachInfoImgs[j].teachTabName = teachItemArray[i].infoArray[j].Name;
                    for (int k = 0; k < teachItemArray[i].infoArray[j].Textures.Count; k++)
                    {
                        EditorUtility.DisplayProgressBar("生成配置", "请稍等...",
                            (float) i / teachItemArray[i].infoArray[j].Textures.Count);
                        config.teachList.teachArray[i].teachInfoImgs[j].imgNames.Add("");
                        config.teachList.teachArray[i].teachInfoImgs[j].imgNames[k] =
                            teachItemArray[i].infoArray[j].Textures[k].name + ".png";
                        string SpritePath =
                            AssetDatabase.GetAssetOrScenePath(teachItemArray[i].infoArray[j].Textures[k]);
                        string SpriteName = teachItemArray[i].infoArray[j].Textures[k].name + ".png";
                        //改赋值会产生依赖,根据需求定
                        // AssetDatabase.CopyAsset(SpritePath, "Assets/实验升级文件/目的原理/" + SpriteName);
                        CopyFolder(Application.dataPath + SpritePath.Remove(0, 6),
                            Application.dataPath + @"\实验升级文件\目的原理\", SpriteName);
                        spriteName.Add(SpriteName);
                    }
                }
            }

            AssetDatabase.Refresh();
            for (int i = 0; i < spriteName.Count; i++)
            {
                SetSprite(spriteName[i]);
            }
        }
        catch (Exception e)
        {
            Debug.Log(e);
            EditorUtility.ClearProgressBar();
            throw;
        }

        EditorUtility.ClearProgressBar();
    }

    void CopyFolder(string sourcePath, string targetpath, string fileName)
    {
        string destFile = Path.Combine(targetpath, fileName);
        if (!File.Exists(destFile))
        {
            File.Copy(sourcePath, destFile, true);
            Debug.Log("没有图片");
        }
    }

    void CopAudio(string oldfile, string newfile)
    {
        string newpath = "Assets/实验升级文件/";
        string[] dirs = Directory.GetFileSystemEntries(oldfile);
        int number = (dirs.Length / 2) + 1;
        bool isoptishi = true;
        for (int i = 1; i < number; i++)
        {
            if (Directory.GetFileSystemEntries(newfile).Length / 2 != number - 1)
            {
                AssetDatabase.CopyAsset(oldfile + i + ".wav", newfile + i + ".wav");
                EditorUtility.DisplayProgressBar("复制音频中", "请稍等...    " + i + "只羊",
                    (float) i / number);
                isoptishi = false;
            }
            else
            {
                isoptishi = true;
            }
        }

        if (isoptishi)
        {
            EditorUtility.DisplayDialog("", "没有可以拷贝的音频文件", "确定");
        }

        AssetDatabase.Refresh();
    }

    void SetSprite(string fileName)
    {
        TextureImporter texture = AssetImporter.GetAtPath(@"Assets\实验升级文件\目的原理\" + fileName) as TextureImporter;
        texture.textureType = TextureImporterType.Sprite;
        texture.spritePixelsPerUnit = 1;
        texture.filterMode = FilterMode.Trilinear;
        texture.mipmapEnabled = false;
        texture.textureFormat = TextureImporterFormat.AutomaticTruecolor;
        AssetDatabase.ImportAsset(@"Assets\实验升级文件\目的原理\" + fileName);
    }

    void GetStep(ZHCCofigScript.Config config)
    {
        LoadStepTxt loadStepTxt = FindObjectOfType<LoadStepTxt>();
        TishiControl tishiTxtText = FindObjectOfType<TishiControl>();
        try
        {
            if (loadStepTxt.bigtxt && loadStepTxt.smalltxt && loadStepTxt.stepvoiceText && tishiTxtText.tishiTxt)
            {
                config.m_BigTxt = loadStepTxt.bigtxt.text;
                config.m_Smalltxt = loadStepTxt.smalltxt.text;
                config.m_StepvoiceTxt = loadStepTxt.stepvoiceText.text;
                config.m_TishiTxt = tishiTxtText.tishiTxt.text;
            }
            else
            {
                Debug.LogError("loadsteptxt,是否挂载场景中并且是否有3大小步骤和音频txt文档");
            }
        }
        catch (Exception e)
        {
            throw new WarningException(e.Message);
        }
    }


    /// <summary>
    /// 创建配置文件
    /// </summary>
    void CreateJson(ZHCCofigScript.Config config)
    {
        try
        {
            string json = JsonMapper.ToJson(config);
            string questionInfo = OtherFormatToUTF8.UnicodeToString(json);
            File.WriteAllText(Application.dataPath + zhcCofigScript.ConfiPath, questionInfo, Encoding.UTF8);
            AssetDatabase.Refresh();
            Debug.Log("写入完成");
        }
        catch (Exception e)
        {
            Console.WriteLine("重写失败" + e);
            throw;
        }
    }

    #endregion

}

标签:获取信息,string,Get,int,写入,Length,AssetDatabase,zhcCofigScript,config
来源: https://blog.csdn.net/qq_40558087/article/details/120146383