UnLua访问C++属性、动态生成Actor

一、访问C++属性、动态生成Actor

1、在C++类MyBaseActor中新增蓝图可编辑属性:Name

cpp 复制代码
public:
	UPROPERTY(EditAnywhere)
	FString Name;

2、在继承自C++的蓝图子类BP_BaseActor中给Name赋值

3、在Lua中实现点击按钮动态生成Actor,并访问其Name属性

lua 复制代码
function WBP_FirstLua_C:Construct()
	self.btnTest.OnPressed:Add(self,WBP_FirstLua_C.OnClickTest)
end

function WBP_FirstLua_C:OnClickTest()
	local World = self:GetWorld()
	if not World then
		return
	end
	--加载蓝图类,注意文件路径格式:蓝图名.Lua类名
	local ActorClass = UE.UClass.Load("/Game/MyLuaTest/BP_BaseActor.BP_BaseActor_C")
	if not ActorClass then
		return
	end
	--动态生成一个actor
	local Rotation = UE.FQuat(1,1,1,1)
    local Translation = UE.FVector(1,1,1)
    local Scale = UE.FVector(1,1,1)
    local Transform = UE.FTransform(Rotation, Translation, Scale)
	local Actor = World:SpawnActor(ActorClass,Transform,UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,self,self,"")
	if not Actor then
		return
	end

	self.Actor = Actor
	print("MyBaseActor'Name is "..self.Actor.Name)
end
相关推荐
努力学习的小廉2 天前
UE5 入门系列(三):核心框架类全解析
ue5
平行云3 天前
建筑BIM/3D可视化轻量级实时评审与分享方案
unity·ue5·3dsmax·实时云渲染·云桌面·云游戏·建筑bim
BugPhysician8 天前
影之刃零首发UE崩溃排查:着色器缓存、虚拟内存与驱动版本
ue5·影之刃零·影之刃零报错·影之刃零进不去·ue崩溃·ue引擎崩溃·ue5引擎崩溃
电子云与长程纠缠9 天前
UE5魂系游戏开发教程05
开发语言·学习·游戏·ui·ue5
fanfan_hongyun10 天前
UE5.1 蓝图调用C++方法
ue5
易知微EasyV数据可视化10 天前
UE5 中接入 GDAL:从vcpkg编译到插件打包
ue5
努力学习的小廉10 天前
UE5入门系列(一):环境搭建.md
ue5
fanfan_hongyun10 天前
【UE5.1 创建C++文件】
c++·ue5
努力学习的小廉10 天前
UE5 入门系列(二):编辑器基础操作速通
ue5·编辑器
Дерек的学习记录11 天前
Unreal Engine 5:开关门实现和流程控制
开发语言·学习·ue5