[Unity热更新]更新lua脚本 (一)

参考链接:http://www.manew.com/thread-39343-1-1.html?_dsign=334770ed


一、创建lua脚本模板

在\Editor\Data\Resources\ScriptTemplates目录下可以看到各种的模板文件,模仿命名格式"数字-右键菜单项的名字-默认文件名字.txt",创建文件,然后重启unity,就可以像平时新建脚本一样,新建自定义的模板。


二、后缀格式转换(.lua和.txt的转换)

using UnityEditor;
using System.IO;

public class ChangeExtension {

    static void ExtensionConvert(string newExtension)
    {
        if (Selection.objects.Length > 0)
        {
            for (int i = 0; i < Selection.objects.Length; i++)
            {
                string path = AssetDatabase.GetAssetPath(Selection.objects[i]);
                string newPath = Path.ChangeExtension(path, newExtension);
                if (File.Exists(newPath)) File.Delete(newPath);
                File.Move(path, newPath);
            }
            AssetDatabase.Refresh();
        }
    }

    [MenuItem("Assets/ChangeExtension/ToTxt")]
    static void ToTxt()
    {
        ExtensionConvert("txt");
    }

    [MenuItem("Assets/ChangeExtension/ToLua")]
    static void ToLua()
    {
        ExtensionConvert("lua");
    }

}

三、压缩解压工具(对lua脚本资源进行压缩解压处理)

这里本人使用的是DotNetZip,地址为:http://dotnetzip.codeplex.com/。DotNetZip有很多东西,而一般情况下只需使用Ionic.Zip.dll这个东西就可以了,这里给出下载地址:http://pan.baidu.com/s/1cDxp0

using System.Collections.Generic;
using System.IO;
using Ionic.Zip;

public class ZipTool {

    public static void CreateZip(List<string> filePaths, string outputDir, string zipName, bool deleteSourceFile)
    {
        string zipPath = outputDir + "/" + zipName + ".zip";
        //Debug.Log(zipPath);
        using (ZipFile zip = new ZipFile())
        {
            for (int i = 0; i < filePaths.Count; i++)
            {
                zip.AddFile(filePaths[i]);
            }
            zip.Save(zipPath);
        }

        if (!deleteSourceFile) return;
        for (int i = 0; i < filePaths.Count; i++)
        {
            File.Delete(filePaths[i]);
        }
    }

    public static void UnZip(string zipPath, string outputDir, bool deleteSourceFile)
    {
        using (ZipInputStream zis = new ZipInputStream(File.OpenRead(zipPath)))
        {
            ZipEntry ze;
            while ((ze = zis.GetNextEntry()) != null)
            {
                string targetPath = outputDir + ze.FileName.Substring(ze.FileName.LastIndexOf("/"));
                //Debug.Log(ze.FileName);
                //Debug.Log(targetPath);
                if (File.Exists(targetPath)) File.Delete(targetPath);
                using (FileStream fs = File.Create(targetPath))
                {
                    byte[] buffer = new byte[2048];
                    while (true)
                    {
                        int size = zis.Read(buffer, 0, buffer.Length);
                        if (size > 0)  fs.Write(buffer, 0, size);
                        else break;
                    }
                }
            }
        }
        if (deleteSourceFile) File.Delete(zipPath);
    }

}

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

public class ZipConvert : EditorWindow {

    string zipName;
    string compressFileDir;
    string decompressFileDir;

    [MenuItem("Window/ZipConvert")]
    static void Init()
    {
        EditorWindow.GetWindow(typeof(ZipConvert));
    }

    void OnGUI()
    {
        GUILayout.Label("压缩文件(请选中要压缩的文件)");
        zipName = EditorGUILayout.TextField("压缩文件名(不需要后缀)", zipName);
        if (Selection.objects.Length > 0)
        {
            compressFileDir = Directory.GetParent(AssetDatabase.GetAssetPath(Selection.objects[0])).ToString();
            compressFileDir = EditorGUILayout.TextField("输出目录", compressFileDir);

            if (GUILayout.Button("压缩"))
            {            
                List<string> filePaths = new List<string>();
                for (int i = 0; i < Selection.objects.Length; i++)
                {
                    string s = AssetDatabase.GetAssetPath(Selection.objects[i]);
                    if (Directory.Exists(s))//是目录
                    {
                        string[] ss = Directory.GetFiles(s);
                        foreach (var v in ss)
                        {
                            if (!v.EndsWith(".meta"))
                            {
                                filePaths.Add(v);
                            }
                        }
                    }
                    else//是单个文件 
                    {
                        filePaths.Add(s);
                    }
                }
                foreach (var v in filePaths) Debug.Log(v);
                ZipTool.CreateZip(filePaths, compressFileDir, zipName, true);
                AssetDatabase.Refresh();
            }
        }

        GUILayout.Space(10);
        GUILayout.Label("------------------------------------------------------------");
        GUILayout.Space(10);

        GUILayout.Label("解压文件(请选中要解压的文件)");
        if (Selection.objects.Length > 0)
        {
            string zipPath = AssetDatabase.GetAssetPath(Selection.objects[0]);
            decompressFileDir = Directory.GetParent(zipPath).ToString();
            decompressFileDir = EditorGUILayout.TextField("输出目录", decompressFileDir);
            if (GUILayout.Button("解压"))
            {
                ZipTool.UnZip(zipPath, decompressFileDir, true);
                AssetDatabase.Refresh();
            } 
        }     
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值