71 lines
2.8 KiB
C#
71 lines
2.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using UnityEditor;
|
|
using UnityEditor.ProjectWindowCallback;
|
|
using UnityEngine;
|
|
public class CreateHtml
|
|
{
|
|
|
|
[MenuItem("Assets/Create/创建WordAssets文件/Webgl Index Html", false, 60)]
|
|
public static void CreateIndex()
|
|
{
|
|
//参数为传递给CreateEventCSScriptAsset类action方法的参数
|
|
|
|
var endNameEditAction = ScriptableObject.CreateInstance<CreateEventCSScriptAsset>();
|
|
|
|
var icon = EditorGUIUtility.FindTexture("html Script Icon");
|
|
|
|
var resourceFile = "Assets/StreamingAssets/Template/" + "Index.html.txt";
|
|
|
|
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, endNameEditAction, "index.html", icon, resourceFile);
|
|
}
|
|
}
|
|
public class CreateEventCSScriptAsset : EndNameEditAction
|
|
{
|
|
public override void Action(int instanceId, string pathName, string resourceFile)
|
|
{
|
|
//创建资源
|
|
UnityEngine.Object obj = CreateScriptAssetFromTemplate(pathName, resourceFile);
|
|
ProjectWindowUtil.ShowCreatedAsset(obj);//高亮显示资源
|
|
}
|
|
|
|
private static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile)
|
|
{
|
|
//获取要创建资源的绝对路径
|
|
string fullPath = Path.GetFullPath(pathName);
|
|
|
|
//读取本地的模板文件
|
|
StreamReader streamReader = new StreamReader(resourceFile);
|
|
string text = streamReader.ReadToEnd();
|
|
streamReader.Close();
|
|
|
|
//获取文件名,不含扩展名
|
|
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName);
|
|
text = Regex.Replace(text, "#ScriptName#", fileNameWithoutExtension);
|
|
text = Regex.Replace(text, "#Author#", WordConfig.Instance.Author);
|
|
text = Regex.Replace(text, "#ProgramName#", PlayerSettings.productName);
|
|
text = Regex.Replace(text, "#NowTime#", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
text = Regex.Replace(text, "#ReleasePathName#", WordConfig.Instance.PackPathName);
|
|
|
|
//写入配置文件
|
|
bool encoderShouldEmitUTF8Identifier = true; //参数指定是否提供 Unicode 字节顺序标记
|
|
bool throwOnInvalidBytes = false;//是否在检测到无效的编码时引发异常
|
|
bool append = false;
|
|
UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes);
|
|
StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding);
|
|
streamWriter.Write(text);
|
|
streamWriter.Close();
|
|
|
|
//刷新资源管理器
|
|
AssetDatabase.ImportAsset(pathName);
|
|
AssetDatabase.Refresh();
|
|
return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
|
|
}
|
|
}
|
|
|
|
|