549 lines
20 KiB
C#
549 lines
20 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEditor;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using GameEditor.Core;
|
||
using GameEditor.GameEditor.PlayerBuilder;
|
||
using GameLogic;
|
||
using System.Diagnostics;
|
||
using ResUpdate;
|
||
using GameCore;
|
||
using System.Net;
|
||
|
||
namespace GameEditor.FrameTool {
|
||
public class BuildWindow : EditorWindow
|
||
{
|
||
/// <summary>
|
||
/// 版本文件名
|
||
/// </summary>
|
||
const string VersionsFile = "version";
|
||
|
||
Version version;
|
||
string defaultVersion;
|
||
string subChannel;
|
||
string serverUrl;
|
||
string resUrl;
|
||
|
||
/// <summary>
|
||
/// 导出APK/XCODE工程
|
||
/// </summary>
|
||
bool isBuildPlayer;
|
||
/// <summary>
|
||
/// 是否拷贝AB包到StreamingAssets
|
||
/// </summary>
|
||
bool isCopyABSToStreamingAssets;
|
||
// Add menu named "My Window" to the Window menu
|
||
[MenuItem("Build/BuildWindow")]
|
||
static void Init()
|
||
{
|
||
|
||
// Get existing open window or if none, make a new one:
|
||
BuildWindow window = (BuildWindow)EditorWindow.GetWindow(typeof(BuildWindow));
|
||
window.Show();
|
||
window.InitWindow();
|
||
|
||
}
|
||
|
||
void InitWindow()
|
||
{
|
||
InitSize();
|
||
InitGames();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化大小
|
||
/// </summary>
|
||
void InitSize()
|
||
{
|
||
minSize = new Vector2(300, 400);
|
||
maxSize = new Vector2(300, 650);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 初始化游戏
|
||
/// </summary>
|
||
void InitGames()
|
||
{
|
||
//version = new Version(Resources.Load<TextAsset>(VersionsFile).text);
|
||
//defaultVersion = version.GetInfo("version");
|
||
//subChannel = version.GetInfo("subChannel");
|
||
//serverUrl = version.GetInfo("serverUrl");
|
||
//resUrl = version.GetInfo("resUrl");
|
||
}
|
||
|
||
void OnGUI()
|
||
{
|
||
EditorGUILayout.BeginVertical();
|
||
EditorGUILayout.Space();
|
||
EditorGUILayout.LabelField(string.Format("当前平台:{0}", EditorUserBuildSettings.activeBuildTarget.ToString()));
|
||
EditorGUILayout.Space();
|
||
EditorGUILayout.EndVertical();
|
||
|
||
isCopyABSToStreamingAssets = EditorGUILayout.BeginToggleGroup("拷贝AssetBundle到流媒体目录(打包到App包体内)", isCopyABSToStreamingAssets);
|
||
//if (isCopyABSToStreamingAssets)
|
||
//{
|
||
// EditorGUILayout.BeginVertical();
|
||
// defaultVersion = EditorGUILayout.TextField("Version", defaultVersion);
|
||
// subChannel = EditorGUILayout.TextField("SubChannel", subChannel);
|
||
// serverUrl = EditorGUILayout.TextField("ServerUrl", serverUrl);
|
||
// resUrl = EditorGUILayout.TextField("ResUrl", resUrl);
|
||
// EditorGUILayout.EndVertical();
|
||
//}
|
||
EditorGUILayout.EndToggleGroup();
|
||
EditorGUILayout.Space();
|
||
|
||
isBuildPlayer = EditorGUILayout.BeginToggleGroup(GetBuildTitle(), isBuildPlayer);
|
||
if (isBuildPlayer)
|
||
{
|
||
var gameSet = GameObject.FindObjectOfType<GameSettings>();
|
||
gameSet.settingInfo.isUpdate = true;
|
||
gameSet.settingInfo.bundleMode = true;
|
||
gameSet.settingInfo.isDebug = false;
|
||
gameSet.settingInfo.luaBundleMode = true;
|
||
}
|
||
EditorGUILayout.EndToggleGroup();
|
||
EditorGUILayout.Space();
|
||
|
||
if (GUILayout.Button("整体打包", GUILayout.Height(40f)))
|
||
{
|
||
if (EditorUtility.DisplayDialog("打包提示", "打包将持续一段时间,确定打包?", "是", "否")) //显示对话框
|
||
{
|
||
BuildGame();
|
||
}
|
||
}
|
||
if (GUILayout.Button("单打lua资源", GUILayout.Height(40f)))
|
||
{
|
||
if (EditorUtility.DisplayDialog("打包提示", "单打lua资源将持续一段时间,确定打包?", "是", "否")) //显示对话框
|
||
{
|
||
if (FrameTool.BuildLuaAssetBundles())
|
||
{
|
||
//拷贝AssetBundle到流媒体目录
|
||
if (isCopyABSToStreamingAssets)
|
||
{
|
||
string exportPath = AssetBundle.AssetBundleConfig.GetExportPath(EditorUserBuildSettings.activeBuildTarget);
|
||
string targetPath = FrameTool.GetStreamingAssetPath(EditorUserBuildSettings.activeBuildTarget);
|
||
File.Copy(exportPath + "/lzma/luabytes.unity3d", targetPath + "/lzma/luabytes.unity3d", true);
|
||
File.Copy(exportPath + "/lzma/resconfigs.unity3d", targetPath + "/lzma/resconfigs.unity3d", true);
|
||
File.Copy(exportPath + "/files.unity3d", targetPath + "/files.unity3d", true);
|
||
//SaveVersionFile();
|
||
}
|
||
Close();
|
||
}
|
||
}
|
||
}
|
||
if (GUILayout.Button("资源合法性检查", GUILayout.Height(40f)))
|
||
{
|
||
FrameTool.CheckAssetBundleStates();
|
||
}
|
||
if (GUILayout.Button("查看多依赖资源", GUILayout.Height(40f)))
|
||
{
|
||
FrameTool.AssetsDuplicatedInMultBundlesCache(1);
|
||
}
|
||
if (GUILayout.Button("处理多依赖资源", GUILayout.Height(40f)))
|
||
{
|
||
FrameTool.AssetsDuplicatedInMultBundlesCache(2);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打包游戏
|
||
/// </summary>
|
||
void BuildGame()
|
||
{
|
||
//打包游戏
|
||
FrameTool.BuildGameAssetBundles();
|
||
|
||
//拷贝AssetBundle到流媒体目录
|
||
if (isCopyABSToStreamingAssets)
|
||
{
|
||
FrameTool.CopyAssetBundleToStreamingAssets();
|
||
//SaveVersionFile();
|
||
}
|
||
//是否BuildPlayer
|
||
if (isBuildPlayer)
|
||
{
|
||
switch (EditorUserBuildSettings.activeBuildTarget)
|
||
{
|
||
case BuildTarget.Android:
|
||
ExportApk();
|
||
break;
|
||
case BuildTarget.iOS:
|
||
ExportXcode();
|
||
break;
|
||
}
|
||
}
|
||
Close();
|
||
}
|
||
|
||
void SaveVersionFile()
|
||
{
|
||
var VersionsFilePath = Application.dataPath + "/Resources/" + VersionsFile + ".txt";
|
||
var VersionsFilePath2 = AppConst.PersistentDataPath + VersionsFile + ".txt";
|
||
|
||
version.SetInfo("version", defaultVersion);
|
||
version.SetInfo("subChannel", subChannel);
|
||
version.SetInfo("serverUrl", serverUrl);
|
||
version.SetInfo("resUrl", resUrl);
|
||
|
||
string directoryPath = Path.GetDirectoryName(VersionsFilePath);
|
||
if (!Directory.Exists(directoryPath))
|
||
Directory.CreateDirectory(directoryPath);
|
||
File.WriteAllText(VersionsFilePath, version.ToJson(), System.Text.Encoding.UTF8);
|
||
|
||
string directoryPath2 = Path.GetDirectoryName(VersionsFilePath2);
|
||
if (!Directory.Exists(directoryPath2))
|
||
Directory.CreateDirectory(directoryPath2);
|
||
File.WriteAllText(VersionsFilePath2, version.ToJson(), System.Text.Encoding.UTF8);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取是否打包
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
string GetBuildTitle()
|
||
{
|
||
switch (EditorUserBuildSettings.activeBuildTarget)
|
||
{
|
||
case BuildTarget.Android:
|
||
return "导出APK";
|
||
case BuildTarget.iOS:
|
||
return "导出XCODE工程";
|
||
}
|
||
return "请切换到Android/IOS平台";
|
||
}
|
||
|
||
/// <summary>
|
||
/// 导出XCODE工程
|
||
/// </summary>
|
||
void ExportXcode()
|
||
{
|
||
PlayerBuilder.BuildXCode();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否导出APK
|
||
/// </summary>
|
||
void ExportApk()
|
||
{
|
||
PlayerBuilder.BuildApk();
|
||
}
|
||
}
|
||
|
||
public class VersionTxt
|
||
{
|
||
public string subChannel;
|
||
public string buglyId;
|
||
public string channel;
|
||
public string resUrl;
|
||
public string packageVersion;
|
||
public string version;
|
||
public string serverUrl;
|
||
}
|
||
|
||
|
||
public class VersionWindow : EditorWindow
|
||
{
|
||
static string versionPath = Application.dataPath + "/../Version";
|
||
static string editorVersion = Application.dataPath + "/../AssetBundles/version.txt";
|
||
static string persistVersion = Application.dataPath + "/Resources/version.txt";
|
||
static string streamVersion = AppConst.StreamPath + "/Resources/version.txt";
|
||
|
||
static VersionTxt m_VersionTxt; // 热更版本
|
||
|
||
|
||
|
||
static string[] m_Files;
|
||
static bool[] m_Choose;
|
||
static string m_Ver;
|
||
|
||
|
||
|
||
|
||
[MenuItem("Build/Version")]
|
||
static void Init()
|
||
{
|
||
LoadVersion(editorVersion);
|
||
LoadDic();
|
||
// Get existing open window or if none, make a new one:
|
||
VersionWindow window = (VersionWindow)EditorWindow.GetWindow(typeof(VersionWindow));
|
||
window.Show();
|
||
|
||
}
|
||
// 加载version文件
|
||
private static void LoadVersion(string path)
|
||
{
|
||
if (!string.IsNullOrEmpty(path) && File.Exists(path))
|
||
{
|
||
string json = File.ReadAllText(path);
|
||
m_VersionTxt = JsonUtility.FromJson<VersionTxt>(json);
|
||
}
|
||
if (!path.Equals(editorVersion))
|
||
{
|
||
SaveToEditorPath();
|
||
}
|
||
}
|
||
|
||
// 保存到编辑器路径
|
||
private static void SaveToEditorPath()
|
||
{
|
||
string json = JsonUtility.ToJson(m_VersionTxt);
|
||
SaveToVersion(editorVersion, json);
|
||
}
|
||
// 保存到打包路径
|
||
private static void SaveToBuildPath()
|
||
{
|
||
string json = JsonUtility.ToJson(m_VersionTxt);
|
||
SaveToVersion(persistVersion, json);
|
||
SaveToVersion(streamVersion, json);
|
||
}
|
||
// 保存到version
|
||
private static void SaveToVersion(string path, string json)
|
||
{
|
||
if (!string.IsNullOrEmpty(path) && File.Exists(path))
|
||
{
|
||
File.WriteAllText(path, json);
|
||
}else
|
||
{
|
||
UnityEngine.Debug.Log("未找到目标路径:"+path);
|
||
}
|
||
}
|
||
|
||
// 加载version目录
|
||
private static void LoadDic()
|
||
{
|
||
if (!string.IsNullOrEmpty(versionPath) && Directory.Exists(versionPath))
|
||
{
|
||
m_Files = Directory.GetFiles(versionPath, "*", SearchOption.AllDirectories);
|
||
m_Choose = new bool[m_Files.Length];
|
||
}
|
||
}
|
||
void OnGUI()
|
||
{
|
||
|
||
EditorGUILayout.BeginVertical();
|
||
EditorGUILayout.LabelField("version文件配置:");
|
||
EditorGUILayout.Space();
|
||
EditorGUILayout.LabelField("登录服地址:");
|
||
m_VersionTxt.serverUrl = EditorGUILayout.TextField("", m_VersionTxt.serverUrl);
|
||
EditorGUILayout.LabelField("热更CDN地址:");
|
||
m_VersionTxt.resUrl = EditorGUILayout.TextField("", m_VersionTxt.resUrl);
|
||
EditorGUILayout.LabelField("渠道:");
|
||
m_VersionTxt.channel = EditorGUILayout.TextField("", m_VersionTxt.channel);
|
||
EditorGUILayout.LabelField("子渠道:");
|
||
m_VersionTxt.subChannel = EditorGUILayout.TextField("", m_VersionTxt.subChannel);
|
||
EditorGUILayout.LabelField("包版本号:");
|
||
m_VersionTxt.packageVersion = EditorGUILayout.TextField("", m_VersionTxt.packageVersion);
|
||
EditorGUILayout.LabelField("热更版本号:");
|
||
m_VersionTxt.version = EditorGUILayout.TextField("", m_VersionTxt.version);
|
||
EditorGUILayout.EndVertical();
|
||
|
||
if (GUILayout.Button("应用", GUILayout.Height(40f)))
|
||
{
|
||
SaveToEditorPath();
|
||
UnityEngine.Debug.Log("应用成功");
|
||
}
|
||
EditorGUILayout.BeginVertical();
|
||
EditorGUILayout.Space();
|
||
EditorGUILayout.LabelField("请选择要使用的version文件:");
|
||
if (m_Files != null && m_Files.Length != 0)
|
||
{
|
||
for (int i = 0; i < m_Files.Length; i++)
|
||
{
|
||
m_Choose[i] = EditorGUILayout.ToggleLeft(Path.GetFileNameWithoutExtension(m_Files[i]), m_Choose[i]);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
EditorGUILayout.LabelField("未找到分支");
|
||
}
|
||
EditorGUILayout.EndVertical();
|
||
|
||
if (GUILayout.Button("使用选中的version文件", GUILayout.Height(40f)))
|
||
{
|
||
for (int i = 0; i < m_Files.Length; i++)
|
||
{
|
||
if (m_Choose[i])
|
||
{
|
||
LoadVersion(m_Files[i]);
|
||
UnityEngine.Debug.Log("使用成功");
|
||
return;
|
||
}
|
||
}
|
||
UnityEngine.Debug.Log("未找到可用的version");
|
||
}
|
||
|
||
|
||
if (GUILayout.Button("保存到打包路径", GUILayout.Height(40f)))
|
||
{
|
||
SaveToBuildPath();
|
||
UnityEngine.Debug.Log("保存成功");
|
||
}
|
||
}
|
||
}
|
||
|
||
//看热更大小
|
||
public class HotfixWindow : EditorWindow
|
||
{
|
||
static VersionTxt m_VersionTxt; // 热更版本
|
||
static string fixVersion;
|
||
static string fPath;
|
||
static List<ResourceFile> exDataList;
|
||
static List<ResourceFile> inDataList;
|
||
static DirectoryInfo pathInfo;
|
||
static decimal Size = 0;
|
||
static Dictionary<string, ResourceFile> exDataDic = new Dictionary<string, ResourceFile>();
|
||
|
||
[MenuItem("Build/HotfixSize")]
|
||
static void Init()
|
||
{
|
||
fixVersion = Application.dataPath + "/../AssetBundles/version.txt";
|
||
pathInfo = new DirectoryInfo(Application.dataPath);
|
||
GetExternalFile(fixVersion);
|
||
GetInternalData();
|
||
|
||
// Get existing open window or if none, make a new one:
|
||
HotfixWindow window = (HotfixWindow)EditorWindow.GetWindow(typeof(HotfixWindow));
|
||
window.titleContent = new GUIContent("热更大小");
|
||
window.Show();
|
||
}
|
||
//下载外部.unity3d文件
|
||
private static void GetExternalFile(string path)
|
||
{
|
||
//获取下载文件链接
|
||
string json = File.ReadAllText(path);
|
||
m_VersionTxt = JsonUtility.FromJson<VersionTxt>(json);
|
||
string url = m_VersionTxt.resUrl + "Android/files.unity3d";
|
||
//获取文件存放路径
|
||
fPath = pathInfo.Parent.FullName + @"\Temp";
|
||
if (File.Exists(fPath + @"\files.unity3d"))
|
||
{
|
||
File.Delete(fPath + @"\files.unity3d");
|
||
}
|
||
// 设置参数
|
||
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
|
||
//发送请求并获取相应回应数据
|
||
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
|
||
//直到request.GetResponse()程序才开始向目标网页发送Post请求
|
||
Stream responseStream = response.GetResponseStream();
|
||
//创建本地文件写入流
|
||
Stream stream = new FileStream(fPath + @"\files.unity3d", FileMode.Create, FileAccess.Write);
|
||
byte[] bArr = new byte[1024];
|
||
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
|
||
while (size > 0)
|
||
{
|
||
stream.Write(bArr, 0, size);
|
||
size = responseStream.Read(bArr, 0, (int)bArr.Length);
|
||
}
|
||
stream.Close();
|
||
responseStream.Close();
|
||
}
|
||
|
||
//重新下载外部.unity3d文件
|
||
private static void RegetExternalFile()
|
||
{
|
||
//获取下载文件链接
|
||
string url = m_VersionTxt.resUrl + "Android/files.unity3d";
|
||
//获取文件存放路径
|
||
fPath = pathInfo.Parent.FullName + @"\Temp";
|
||
if (File.Exists(fPath + @"\files.unity3d"))
|
||
{
|
||
File.Delete(fPath + @"\files.unity3d");
|
||
}
|
||
// 设置参数
|
||
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
|
||
//发送请求并获取相应回应数据
|
||
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
|
||
//直到request.GetResponse()程序才开始向目标网页发送Post请求
|
||
Stream responseStream = response.GetResponseStream();
|
||
//创建本地文件写入流
|
||
Stream stream = new FileStream(fPath + @"\files.unity3d", FileMode.Create, FileAccess.Write);
|
||
byte[] bArr = new byte[1024];
|
||
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
|
||
while (size > 0)
|
||
{
|
||
stream.Write(bArr, 0, size);
|
||
size = responseStream.Read(bArr, 0, (int)bArr.Length);
|
||
}
|
||
stream.Close();
|
||
responseStream.Close();
|
||
}
|
||
|
||
//遍历外部unity3d数据
|
||
private static void GetExternalData()
|
||
{
|
||
UnityEngine.AssetBundle bundle = UnityEngine.AssetBundle.LoadFromFile(fPath + "\\" + UpdateConfigs.FILES);
|
||
ResourceFiles files = bundle.LoadAsset<ResourceFiles>("game");
|
||
exDataList = files.files;
|
||
bundle.Unload(true);
|
||
exDataList.Sort(delegate (ResourceFile a, ResourceFile b) { return a.crc.CompareTo(b.crc); });
|
||
for (int i = 0; i < exDataList.Count; i++)
|
||
{
|
||
if (!exDataDic.ContainsKey(exDataList[i].fileName))
|
||
{
|
||
exDataDic.Add(exDataList[i].fileName, exDataList[i]);
|
||
}else
|
||
{
|
||
exDataDic[exDataList[i].fileName] = exDataList[i];
|
||
}
|
||
}
|
||
}
|
||
|
||
//遍历内部unity3d数据
|
||
private static void GetInternalData()
|
||
{
|
||
UnityEngine.AssetBundle bundle = UnityEngine.AssetBundle.LoadFromFile(pathInfo.Parent.FullName + @"\BuildABs\Android\" + UpdateConfigs.FILES);
|
||
ResourceFiles files = bundle.LoadAsset<ResourceFiles>("game");
|
||
inDataList = files.files;
|
||
bundle.Unload(true);
|
||
}
|
||
|
||
//对比数据
|
||
private static void CompareData()
|
||
{
|
||
for (int i = 0; i < inDataList.Count; i++)
|
||
{
|
||
if (!exDataDic.ContainsKey (inDataList[i].fileName))
|
||
{
|
||
UnityEngine.Debug.Log("NewFile: "+inDataList[i].fileName);
|
||
Size = inDataList[i].size + Size;
|
||
}else
|
||
{
|
||
if(!inDataList[i].crc.Equals(exDataDic[inDataList[i].fileName].crc))
|
||
{
|
||
UnityEngine.Debug.Log("ModifiedFile: " + inDataList[i].fileName);
|
||
Size = inDataList[i].size + Size;
|
||
}
|
||
}
|
||
}
|
||
Size = Size / 1048576;
|
||
UnityEngine.Debug.Log(Size + "M");
|
||
}
|
||
|
||
void OnGUI()
|
||
{
|
||
EditorGUILayout.BeginVertical();
|
||
EditorGUILayout.Space();
|
||
EditorGUILayout.LabelField("线上位置:");
|
||
m_VersionTxt.resUrl = EditorGUILayout.TextField(m_VersionTxt.resUrl);
|
||
EditorGUILayout.EndVertical();
|
||
if (GUILayout.Button("重新获取files", GUILayout.Height(40f)))
|
||
{
|
||
UnityEngine.Debug.Log("URL:"+ m_VersionTxt.resUrl);
|
||
RegetExternalFile();
|
||
}
|
||
if (GUILayout.Button("对比大小", GUILayout.Height(40f)))
|
||
{
|
||
Size = 0;
|
||
GetExternalData();
|
||
CompareData();
|
||
}
|
||
|
||
string resUrl = Size.ToString("0.00") + "M";
|
||
resUrl = EditorGUILayout.TextField(resUrl);
|
||
}
|
||
}
|
||
}
|