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
相关推荐
netcy13 小时前
UE5 小部件 组合框(键)ComboBox (Key)
ue5
二DUAN帝2 天前
态势仿真推演系统 AFSIM+UE 架构选型
qt·ue5·afsim
maplewen.3 天前
【UE】GAS Gameplay Ability
ue5
帅_shuai_3 天前
UE5.6 Slate独立程序——初始化空白Slate环境
ue5
吴梓穆3 天前
UE5 c++ 模板函数
java·c++·ue5
吴梓穆3 天前
UE5 c++ 暴露变量和方法给蓝图
java·c++·ue5
混迹中的咸鱼3 天前
UE5 网络联机常用命令
网络·ue5
UTwelve4 天前
【UE】使用 Shot 命令,在UE中完成截图,及Shot命令的参数设置
ue5
HAPPY酷4 天前
UE5性能分析:追踪目录与分析目录的区别
ue5
UTwelve4 天前
【UE】如何正确旋转法线贴图
性能优化·ue5·材质·贴图·着色器