在某些工作流中,也许需要在UE4编辑器内启动外部程序,我想了解具体该怎么做。查了些资料之后,发现很简单,只需要调用FPlatformProcess::CreateProc
函数:
/**
* Creates a new process and its primary thread. The new process runs the
* specified executable file in the security context of the calling process.
* @param URL executable name
* @param Parms command line arguments
* @param bLaunchDetached if true, the new process will have its own window
* @param bLaunchHidden if true, the new process will be minimized in the task bar
* @param bLaunchReallyHidden if true, the new process will not have a window or be in the task bar
* @param OutProcessId if non-NULL, this will be filled in with the ProcessId
* @param PriorityModifier -2 idle, -1 low, 0 normal, 1 high, 2 higher
* @param OptionalWorkingDirectory Directory to start in when running the program, or NULL to use the current working directory
* @param PipeWrite Optional HANDLE to pipe for redirecting output
* @return The process handle for use in other process functions
*/
static FProcHandle CreateProc( const TCHAR* URL, const TCHAR* Parms, bool bLaunchDetached, bool bLaunchHidden, bool bLaunchReallyHidden, uint32* OutProcessID, int32 PriorityModifier, const TCHAR* OptionalWorkingDirectory, void* PipeWriteChild, void * PipeReadChild = nullptr);
URL
是exe的路径,而Parms
是参数字符串。
我想做个小实验试试它:
我选择创建一个插件来测试。
首先,以“编辑器工具栏按钮”为模板创建插件,名字取为testExePlugin
:
选择这个模板是因为它有一个按钮可以执行特定的操作。
修改testExePlugin.cpp
中的FtestExePluginModule::PluginButtonClicked()
的内容即可改变点击按钮执行的内容。
下面我将它的内容改为(不用include其他头文件,他应该已经在CoreMinimal.h
中被include了):
void FtestExePluginModule::PluginButtonClicked()
{
const TCHAR* ExePath = L"C:/Program Files/Blender Foundation/Blender 2.81/blender.exe";
const TCHAR* Parms = L"";
FPlatformProcess::CreateProc(ExePath, Parms, true, false, false, nullptr, -1, nullptr, nullptr);
}
编译后点击插件新添加的按钮:
之后可以看到顺利运行了blender
FPlatformProcess
是平台抽象层,他会被具体的操作系统下的类typedef掉。
如我现在在windows平台下:
typedef FWindowsPlatformProcess FPlatformProcess;
FPlatformProcess
本身还有其他的函数可以使用。
例如LaunchFileInDefaultExternalApplication
函数:
/**
* Attempt to launch the provided file name in its default external application. Similar to FPlatformProcess::LaunchURL,
* with the exception that if a default application isn't found for the file, the user will be prompted with
* an "Open With..." dialog.
*
* @param FileName Name of the file to attempt to launch in its default external application
* @param Parms Optional parameters to the default application
* @param Verb Optional verb to use when opening the file, if it applies for the platform.
*/
static void LaunchFileInDefaultExternalApplication( const TCHAR* FileName, const TCHAR* Parms = NULL, ELaunchVerb::Type Verb = ELaunchVerb::Open );
它可以启动默认的应用程序打开一个文件,例如下面代码打开了"D:/Temp/test_01.png
:
FPlatformProcess::LaunchFileInDefaultExternalApplication(L"D:/Temp/test_01.png");
再例如ExploreFolder
函数:
/**
* Attempt to "explore" the folder specified by the provided file path
*
* @param FilePath File path specifying a folder to explore
*/
static void ExploreFolder( const TCHAR* FilePath );
可以浏览文件夹,例如下面代码打开了D:/Temp
文件夹:
FPlatformProcess::ExploreFolder(L"D:/Temp");