前两天群里有小伙伴问我,如何运用UnrealEnginePython插件对UE4函数的映射(Reflection),用纯Python编程的方式彻底取代蓝图节点编程的方法,这里给出一种解决方案,需要改写的逻辑如图所示:
Step 01: 使用插件Python Editor,创建一个python脚本,取名“call_bp_func”,键入如下Python脚本:
import unreal_engine as ue # 引入KismetSystemLibrary以便调用DrawDebugSphere函数 from unreal_engine.classes import KismetSystemLibrary class CallBPFunc: def begin_play(self): # ue.print_string('Begin Play') Actor = self.uobject.get_owner() Center = self.uobject.get_actor_location() # 也可以写成Center = self.uobject.GetActorLocation(),但前者已由插件封装为本地方法,因此比后者运行速度更快,参见:https://github.com/20tab/UnrealEnginePython/blob/4b5da5bf4ca598e8d0f67eb938749bc381a67d6c/README.md Radius = Actor.Sphere.GetScaledSphereRadius() KismetSystemLibrary.DrawDebugSphere(Actor, Center, Radius, 12, Actor.Color, 999999.0, 0.0) # 这里注意DrawDebugSphere方法本身有一个缺省值"WorldContextObject",因此函数的第一个值将Actor传入作为WorldContextObject
Step 02: 以上脚本包含中文注释,直接执行可能会报错,因此用PEP8-ize功能对脚本进行格式化
如果还未安装Autopep8模组会提示如下错误:
此时,来到插件安装目录下的\Binaries\Win64
文件夹,运行CMD命令:
./python.exe -m pip install --target . autopep8
再点击PEP8-ize按钮即可自动格式化脚本
Step 03: 创建一个蓝图用于运行Python代码,为此蓝图添加Python Component,并填上脚本名、类名
添加一个Sphere,半径可以自行设置
同时为了给要绘制的DebugSphere着色,可以为蓝图添加一个Linear Color变量,取名为Color,颜色自行设置(如红色)
Step 04: 保存蓝图和代码,将蓝图拖放到场景中,可以看到脚本成功被运行了
更多内容请参见我在B站发布的系列视频