有时候需要自己创建批处理文件或日志文件,在启动程序的同级目录使用,减少手动操作的时间和错误率。主要使用到的是OnPostprocessBuild方法。
1、在工程中的Editor文件夹下创建脚本
2、将文件放入Plugins的相关目录
3.脚本内容
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEditor;
- using UnityEditor.Callbacks;
- using UnityEngine;
-
- public class PostBuilds : MonoBehaviour
- {
- [PostProcessBuild]
- public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
- {
- switch (buildTarget)
- {
- case BuildTarget.StandaloneWindows:
- BuildWindowsPlayer(path);
- break;
-
- case BuildTarget.StandaloneWindows64:
- BuildWindowsPlayer(path);
- break;
-
- case BuildTarget.StandaloneLinux:
- break;
-
- case BuildTarget.StandaloneLinux64:
- BuildLinuxPlayer64(path);
- break;
-
- case BuildTarget.StandaloneLinuxUniversal:
- break;
-
- case BuildTarget.iOS:
- break;
- }
- }
- static void BuildWindowsPlayer(string path)
- {
- string configSetting = Application.dataPath + "/Plugins/StartConfigurationTools/Win/configSetting.bat";
- string buildPath = Path.GetDirectoryName(path);
- CopyShellScript(configSetting, buildPath);
-
- }
- static void BuildLinuxPlayer64(string path)
- {
- string configSetting = Application.dataPath + "/Plugins/StartConfigurationTools/Linux64/configSetting.sh";
- string buildPath = Path.GetDirectoryName(path);
- CopyShellScript(configSetting, buildPath);
-
- }
- private static void CopyShellScript(string scriptPath, string targetPath)
- {
- string scriptName = Path.GetFileName(scriptPath);
- if (File.Exists(Path.Combine(targetPath, scriptName)))
- File.Delete(Path.Combine(targetPath, scriptName));
- File.Copy(scriptPath, Path.Combine(targetPath, scriptName), false);
- }
- }
4、打包完成即可看到