参考链接: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();
}
}
}
}