需求
使用Unity在Windows上注册自定义的后缀文件.tale,关联文件的图标、文件描述和默认打开方式,注册完毕后双击.tale文件打开默认关联程序,导入点击的.tale文件。
思路
注册自定义文件需要在Windows上对注册表进行操作,在Unity中引用Microsoft.Win32即可,需要注意的是必须要在管理员模式下启动程序才生效,Unity默认使用管理员模式打开功能这里不进行介绍。
点击注册后的文件导入到关联程序中对Unity来说是一个少见的功能,在C中有带参数的main方法,可以很简单的实现该功能,而Unity中没有与之类似的方法,测试过程中在Unity中写了
Main方法,发现居然也是可以跑得通的,但是方法中并不能带参数。
最终在 System.Environment中找到了GetCommandLineArgs方法可以获取到打开程序时Windows传给程序的参数。
实现
1.FileTypeRegInfo
构造注册文件函数,代码如下:
// ==============================================
// 功能描述:文件类型
// 创建时间:2020/12/04 11:13:20
// 作 者:Tale
// 版 本:v1.00
// ==============================================
public class FileTypeRegInfo
{
public FileTypeRegInfo() { }
public FileTypeRegInfo(string extendName){
this.ExtendName = extendName;
}
/// <summary>
/// 自定义文件扩展名
/// </summary>
public string ExtendName; //".vedu"
/// <summary>
///自定义文件说明
/// </summary>
public string Description; //"Vivision编辑器生成的文件"
/// <summary>
/// 自定义文件关联的图标
/// </summary>
public string IcoPath;
/// <summary>
/// 关联自定义文件的应用程序
/// </summary>
public string ExePath;
}
2.FileTypeRegister
注册文件方法,代码如下(示例):
// ==============================================
// 功能描述:注册自定义的文件类型
// 创建时间:2020/12/04 11:13:20
// 作 者:Tale
// 版 本:v1.00
// ==============================================
using Microsoft.Win32;
using UnityEngine;
public class FileTypeRegister : MonoBehaviour {
#region RegisterFileType 注册文件类型
/// <summary>
/// RegisterFileType 注册自定义文件类型,关联图标和默认打开程序。
/// </summary>
public static void RegisterFileType(FileTypeRegInfo regInfo)
{
string relationName = regInfo.ExtendName.Substring(1, regInfo.ExtendName.Length - 1).ToUpper() + "_FileType";
RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName);
fileTypeKey.SetValue("", relationName);
fileTypeKey.Close();
RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName);
relationKey.SetValue("", regInfo.Description);
RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon");
iconKey.SetValue("", regInfo.IcoPath);
RegistryKey shellKey = relationKey.CreateSubKey("Shell");
RegistryKey openKey = shellKey.CreateSubKey("Open");
RegistryKey commandKey = openKey.CreateSubKey("Command");
commandKey.SetValue("", regInfo.ExePath + " %1");
relationKey.Close();
}
/// <summary>
/// GetFileTypeRegInfo 获取自定义文件信息
/// </summary>
public static FileTypeRegInfo GetFileTypeRegInfo(string extendName)
{
FileTypeRegInfo regInfo = new FileTypeRegInfo(extendName);
string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType";
RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName);
regInfo.Description = relationKey.GetValue("").ToString();
RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon");
regInfo.IcoPath = iconKey.GetValue("").ToString();
RegistryKey shellKey = relationKey.OpenSubKey("Shell");
RegistryKey openKey = shellKey.OpenSubKey("Open");
RegistryKey commandKey = openKey.OpenSubKey("Command");
string temp = commandKey.GetValue("").ToString();
regInfo.ExePath = temp.Substring(0, temp.Length - 3);
return regInfo;
}
/// <summary>
/// UpdateFileTypeRegInfo 更新自定义文件信息
/// </summary>
public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo)
{
string extendName = regInfo.ExtendName;
string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType";
RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName, true);
relationKey.SetValue("", regInfo.Description);
RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon", true);
iconKey.SetValue("", regInfo.IcoPath);
RegistryKey shellKey = relationKey.OpenSubKey("Shell");
RegistryKey openKey = shellKey.OpenSubKey("Open");
RegistryKey commandKey = openKey.OpenSubKey("Command", true);
commandKey.SetValue("", regInfo.ExePath + " %1");
relationKey.Close();
return true;
}
/// <summary>
/// FileTypeRegistered 检测自定义图标是否被注册
/// </summary>
public static bool FileTypeRegistered(string extendName)
{
RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName);
if (softwareKey != null)
{
return true;
}
return false;
}
#endregion
}
3.RegistryController
使用方法,代码如下:
// ==============================================
// 功能描述:注册表操作
// 创建时间:2020/12/04 11:10:20
// 作 者:Tale
// 版 本:v1.00
// ==============================================
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RegistryController : MonoBehaviour {
private void Start()
{
#region 点击自定义文件关联的exe传入文件路径
//[0]是关联的exe路径[1]是文件路径 类似main(string[] args)
string[] args = System.Environment.GetCommandLineArgs();
string str = args.Length.ToString();
for (int i = 0; i < args.Length; i++)
{
str += "\n" + "Arg" + i + ": " + args[i];
}
Debug.Log("接受到的参数:" + str);
if (args.Length == 2)
{
Debug.Log("打开的文件:" + args[1].Trim());
}
#endregion
}
/// <summary>
/// 获取路径
/// </summary>
/// <returns></returns>
public string S_GetPath()
{
string path = "";
if (path.Length < 3)
{
string[] sss = System.Reflection.Assembly.GetExecutingAssembly().Location.Split('\\');
string ss = "";
for (int i = 0; i < sss.Length - 3; i++)
{
ss += sss[i] + "\\";
}
path = ss;
}
return path;
}
private void Register() {
if (FileTypeRegister.FileTypeRegistered(".vedu"))
{
FileTypeRegInfo info = new FileTypeRegInfo();
info.ExtendName = ".tale";
info.Description = "tale的自定义文件";
info.IcoPath = Application.streamingAssetsPath + "/Tale.ico";
info.ExePath = S_GetPath() + "/" + Application.productName + ".exe";
FileTypeRegister.RegisterFileType(info);
}
}
private void OnGUI()
{
if (GUI.Button(new Rect(30, 50, 180, 60), "注册"))
{
Register();
}
}
}
测试
exe导出后使用管理员模式打开,点击注册按钮注册.tale文件,注册完毕后会在注册表的HKEY_CLASSES_ROOT中添加.tale,在HKEY_CLASSES_MACHINE中添加.tale和TALE_FILETYPE。
桌面新建任意文件,如test.txt,将后缀改为tale可以看到文件图标显示为程序中注册的图标,双击test.tale打开了导出的exe文件并打印了test.tale的路径,如图所示: