263 lines
10 KiB
C#
263 lines
10 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.IO;
|
||
|
|
using UnityEditor;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
#if WEBGL_WX
|
||
|
|
using WeChatWASM;
|
||
|
|
#endif
|
||
|
|
|
||
|
|
namespace HC.Scripts.Editor
|
||
|
|
{
|
||
|
|
public class HCToolEditor
|
||
|
|
{
|
||
|
|
[MenuItem("HCMiniSDK/Clear Datas")]
|
||
|
|
public static void ClearDatas()
|
||
|
|
{
|
||
|
|
PlayerPrefs.DeleteAll();
|
||
|
|
}
|
||
|
|
|
||
|
|
[MenuItem("HCMiniSDK/Douyin Game Build")]
|
||
|
|
public static void DouyinGameBuild()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
#if WEBGL_WX
|
||
|
|
[MenuItem("HCMiniSDK/WX Game Build")]
|
||
|
|
public static void WxGameBuild()
|
||
|
|
{
|
||
|
|
var startText = "// hcmini sdk";
|
||
|
|
|
||
|
|
var config = UnityUtil.GetEditorConf();
|
||
|
|
var mingGameRoot = config.ProjectConf.DST;
|
||
|
|
|
||
|
|
UpdateGameJsCode(mingGameRoot, startText);
|
||
|
|
CopyUmengSDK(mingGameRoot, startText);
|
||
|
|
UpdateWxJsCode(mingGameRoot, startText);
|
||
|
|
Debug.Log("修改微信小游戏工程成功.");
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
private static void CopyUmengSDK(string mingGameRoot, string startText)
|
||
|
|
{
|
||
|
|
var umaSDkJsPath = Path.Combine(mingGameRoot, "minigame", "utils", "uma.min.js");
|
||
|
|
var hachiminiSDkJsPath = Path.Combine(mingGameRoot, "minigame", "utils", "hachimini.js");
|
||
|
|
var tdanalyticsSDkJsPath = Path.Combine(mingGameRoot, "minigame", "utils", "tdanalytics.mg.wx.min.js");
|
||
|
|
|
||
|
|
if (File.Exists(umaSDkJsPath))
|
||
|
|
{
|
||
|
|
Debug.LogError($"uma sdk exit : {umaSDkJsPath}");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var umamin = FindFilePath("uma.min");
|
||
|
|
|
||
|
|
Debug.Log(umamin);
|
||
|
|
|
||
|
|
var folderPath = Path.GetDirectoryName(umaSDkJsPath);
|
||
|
|
if (folderPath != null && !Directory.Exists(folderPath))
|
||
|
|
{
|
||
|
|
Directory.CreateDirectory(folderPath);
|
||
|
|
Console.WriteLine("文件夹创建成功!");
|
||
|
|
}
|
||
|
|
|
||
|
|
File.Copy(umamin, umaSDkJsPath);
|
||
|
|
if (!(string.IsNullOrEmpty(HCStaticParams.TDAppId) || string.IsNullOrEmpty(HCStaticParams.TDServerUrl)))
|
||
|
|
{
|
||
|
|
File.Copy(FindFilePath("hachimini"), hachiminiSDkJsPath);
|
||
|
|
File.Copy(FindFilePath("tdanalytics.mg.wx.min"), tdanalyticsSDkJsPath);
|
||
|
|
Debug.Log("tdanalytics");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static string FindFilePath(string fileName)
|
||
|
|
{
|
||
|
|
var guids = AssetDatabase.FindAssets(fileName);
|
||
|
|
var jsFilePath = "";
|
||
|
|
|
||
|
|
if (guids.Length > 0)
|
||
|
|
{
|
||
|
|
jsFilePath = AssetDatabase.GUIDToAssetPath(guids[0]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return jsFilePath;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void UpdateWxJsCode(string mingGameRoot, string startText)
|
||
|
|
{
|
||
|
|
var codeJsPath = Path.Combine(mingGameRoot, "minigame", "game.js");
|
||
|
|
var readLines = File.ReadLines(codeJsPath);
|
||
|
|
var isFirstRow = true;
|
||
|
|
var isWrite = true;
|
||
|
|
var codeLines = new List<string>();
|
||
|
|
|
||
|
|
foreach (var line in readLines)
|
||
|
|
{
|
||
|
|
if (isFirstRow)
|
||
|
|
{
|
||
|
|
isFirstRow = false;
|
||
|
|
if (startText.Equals(line))
|
||
|
|
{
|
||
|
|
Debug.LogError("已经修改过了,不在修改了。");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
codeLines.Add(startText + "\n");
|
||
|
|
codeLines.Add(line);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
if (line.StartsWith("function") && isWrite)
|
||
|
|
{
|
||
|
|
isWrite = false;
|
||
|
|
codeLines.Add("\n");
|
||
|
|
codeLines.Add("import uma from './utils/uma.min.js'");
|
||
|
|
codeLines.Add("uma.init({");
|
||
|
|
codeLines.Add(" appKey: '" + HCStaticParams.UMENG_ID + "',");
|
||
|
|
codeLines.Add(" useOpenid: false, // default true");
|
||
|
|
codeLines.Add(" debug: false");
|
||
|
|
codeLines.Add("})");
|
||
|
|
codeLines.Add("");
|
||
|
|
codeLines.Add("wx.uma = uma;");
|
||
|
|
codeLines.Add("\n");
|
||
|
|
codeLines.Add("\n");
|
||
|
|
|
||
|
|
#if UNITY_WEBGL
|
||
|
|
if (!(string.IsNullOrEmpty(HCStaticParams.TDAppId) || string.IsNullOrEmpty(HCStaticParams.TDServerUrl)))
|
||
|
|
{
|
||
|
|
codeLines.Add($@"
|
||
|
|
import HaichiMini from './utils/hachimini';
|
||
|
|
HaichiMini.init('{HCStaticParams.TDAppId}','{HCStaticParams.TDServerUrl}');
|
||
|
|
wx.HaichiMini = HaichiMini;
|
||
|
|
");
|
||
|
|
}
|
||
|
|
|
||
|
|
codeLines.Add("var new_device = wx.getStorageSync('new_device');");
|
||
|
|
codeLines.Add("if(!new_device){");
|
||
|
|
codeLines.Add("var new_device_data = {};");
|
||
|
|
codeLines.Add("if(wx.HaichiMini){");
|
||
|
|
codeLines.Add("wx.HaichiMini.trackEvent('new_device', new_device_data);");
|
||
|
|
codeLines.Add("new_device_data['ss_distinct_id'] = wx.HaichiMini.getDistinctId();");
|
||
|
|
codeLines.Add("}");
|
||
|
|
codeLines.Add("if(wx.uma){");
|
||
|
|
codeLines.Add("wx.uma.trackEvent('new_device', new_device_data);");
|
||
|
|
codeLines.Add("}");
|
||
|
|
codeLines.Add("wx.setStorageSync('new_device', true);");
|
||
|
|
codeLines.Add("}");
|
||
|
|
|
||
|
|
|
||
|
|
codeLines.Add("\n");
|
||
|
|
codeLines.Add("wx.reportEvent('hcinit',{})");
|
||
|
|
codeLines.Add("\n");
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
string code = @"
|
||
|
|
const toukaType = e.type;
|
||
|
|
const toukaData = e.data;
|
||
|
|
const toukaCostTimeMs = toukaData.costTimeMs;
|
||
|
|
const toukaIsHighPerformance = toukaData.isHighPerformance;
|
||
|
|
const toukaIsVisible = toukaData.isVisible;
|
||
|
|
const toukaLoadDataPackageFromSubpackage = toukaData.loadDataPackageFromSubpackage;
|
||
|
|
const toukaNeedDownloadDataPackage = toukaData.needDownloadDataPackage;
|
||
|
|
const toukaRunTimeMs = toukaData.runTimeMs;
|
||
|
|
const toukaUseCodeSplit = toukaData.useCodeSplit;
|
||
|
|
|
||
|
|
const eventData = {
|
||
|
|
""type"": toukaType,
|
||
|
|
""costTimeMs"": toukaCostTimeMs,
|
||
|
|
""isHighPerformance"": toukaIsHighPerformance,
|
||
|
|
""isVisible"": toukaIsVisible,
|
||
|
|
""loadDataPackageFromSubpackage"": toukaLoadDataPackageFromSubpackage,
|
||
|
|
""needDownloadDataPackage"": toukaNeedDownloadDataPackage,
|
||
|
|
""runTimeMs"": toukaRunTimeMs,
|
||
|
|
""useCodeSplit"": toukaUseCodeSplit
|
||
|
|
};
|
||
|
|
|
||
|
|
eventData['wx_version'] = wx.getAccountInfoSync().miniProgram.version;
|
||
|
|
eventData['wx_model'] = wx.getSystemInfoSync().model;
|
||
|
|
eventData['wx_brand'] = wx.getSystemInfoSync().brand;
|
||
|
|
eventData['wx_language'] = wx.getSystemInfoSync().language;
|
||
|
|
eventData['wx_system'] = wx.getSystemInfoSync().system;
|
||
|
|
eventData['wx_platform'] = wx.getSystemInfoSync().platform;
|
||
|
|
eventData['wx_env_version'] = wx.getAccountInfoSync().miniProgram.envVersion;
|
||
|
|
|
||
|
|
if (e.type === launchEventType.launchPlugin) {
|
||
|
|
wx.uma.trackEvent(""launchEventType.launchPlugin"", eventData);
|
||
|
|
if(wx.HaichiMini) wx.HaichiMini.trackEvent(""launchEventType_launchPlugin"", eventData);
|
||
|
|
wx.reportEvent('launchEventType.launchPlugin'.toLowerCase().replace('.','_'),eventData);
|
||
|
|
}
|
||
|
|
if (e.type === launchEventType.loadWasm) {
|
||
|
|
wx.uma.trackEvent(""launchEventType.loadWasm"", eventData);
|
||
|
|
if(wx.HaichiMini) wx.HaichiMini.trackEvent(""launchEventType_loadWasm"", eventData);
|
||
|
|
wx.reportEvent('launchEventType.loadWasm'.toLowerCase().replace('.','_'),eventData);
|
||
|
|
}
|
||
|
|
if (e.type === launchEventType.compileWasm) {
|
||
|
|
wx.uma.trackEvent(""launchEventType.compileWasm"", eventData);
|
||
|
|
if(wx.HaichiMini) wx.HaichiMini.trackEvent(""launchEventType_compileWasm"", eventData);
|
||
|
|
wx.reportEvent('launchEventType.compileWasm'.toLowerCase().replace('.','_'),eventData);
|
||
|
|
}
|
||
|
|
if (e.type === launchEventType.loadAssets) {
|
||
|
|
wx.uma.trackEvent(""launchEventType.loadAssets"", eventData);
|
||
|
|
if(wx.HaichiMini) wx.HaichiMini.trackEvent(""launchEventType_loadAssets"", eventData);
|
||
|
|
wx.reportEvent('launchEventType.loadAssets'.toLowerCase().replace('.','_'),eventData);
|
||
|
|
}
|
||
|
|
if (e.type === launchEventType.readAssets) {
|
||
|
|
wx.uma.trackEvent(""launchEventType.readAssets"", eventData);
|
||
|
|
if(wx.HaichiMini) wx.HaichiMini.trackEvent(""launchEventType_readAssets"", eventData);
|
||
|
|
wx.reportEvent('launchEventType.readAssets'.toLowerCase().replace('.','_'),eventData);
|
||
|
|
}
|
||
|
|
if (e.type === launchEventType.prepareGame) {
|
||
|
|
wx.uma.trackEvent(""launchEventType.prepareGame"", eventData);
|
||
|
|
if(wx.HaichiMini) wx.HaichiMini.trackEvent(""launchEventType_prepareGame"", eventData);
|
||
|
|
wx.reportEvent('launchEventType.prepareGame'.toLowerCase().replace('.','_'),eventData);
|
||
|
|
}
|
||
|
|
";
|
||
|
|
codeLines.Add(line);
|
||
|
|
if (line.Contains("onLaunchProgress"))
|
||
|
|
{
|
||
|
|
codeLines.Add("\n");
|
||
|
|
codeLines.Add(code + "\n");
|
||
|
|
codeLines.Add("\n");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
File.WriteAllLines(codeJsPath, codeLines);
|
||
|
|
Debug.Log($"wx 代码写入成功 {codeJsPath}");
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void UpdateGameJsCode(string mingGameRoot, string startText)
|
||
|
|
{
|
||
|
|
var unityJsPath = Path.Combine(mingGameRoot, "minigame", "webgl.wasm.framework.unityweb.js");
|
||
|
|
|
||
|
|
// Read the existing text from the file
|
||
|
|
var existingText = File.ReadAllText(unityJsPath);
|
||
|
|
|
||
|
|
if (existingText.StartsWith(startText))
|
||
|
|
{
|
||
|
|
Debug.LogError("已经保存过了");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var guids = AssetDatabase.FindAssets("hcmini-sdk");
|
||
|
|
var jsFilePath = "";
|
||
|
|
|
||
|
|
if (guids.Length > 0)
|
||
|
|
{
|
||
|
|
jsFilePath = AssetDatabase.GUIDToAssetPath(guids[0]);
|
||
|
|
}
|
||
|
|
|
||
|
|
Debug.Log(jsFilePath);
|
||
|
|
|
||
|
|
|
||
|
|
// Combine the new text and existing text
|
||
|
|
var combinedText = startText + Environment.NewLine + File.ReadAllText(jsFilePath) + Environment.NewLine + existingText;
|
||
|
|
|
||
|
|
// Write the combined text back to the file
|
||
|
|
File.WriteAllText(unityJsPath, combinedText);
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
}
|