UE4 C++ Tips
程序员文章站
2023-08-18 08:41:05
本篇写的是关于UE4的C++方面的小技巧: 1.在构造函数里 2.加载资源 具体细节教程(非本人制作):https://ke.qq.com/course/308721 3.通过UObjectLibrary获取批量内容的地址 4.计时器 ......
本篇写的是关于ue4的c++方面的小技巧:
1.在构造函数里
//构建组件 rootcomponent = createdefaultsubobject<uscenecomponent>(text("rootcomponent")); camera = createdefaultsubobject<ucameracomponent>(text("camera")); //把组件放到其它组件下 vrcamera->setupattachment(vrorigin); //下面这条不能用于构造函数中,否则编辑器崩溃或报错 //vrcamera->attachtocomponent(vrorigin, fattachmenttransformrules::snaptotargetincludingscale);
2.加载资源
具体细节教程(非本人制作):
//同步加载,一般用于少量物体加载 //从内存中读取文件,但由于还没从硬盘中读取,所以内存中没有(耗时相对较短),因此读取失败 uhapticfeedbackeffect_base* shakeeffect = findobject<uhapticfeedbackeffect_base>(null,text("hapticfeedbackeffect_curve'/game/virtualrealitybp/blueprints/motioncontrollerhaptics.motioncontrollerhaptics'")); //从硬盘中读取文件,放到内存中(耗时相对较长) uhapticfeedbackeffect_base* shakeeffect = loadobject<uhapticfeedbackeffect_base>(null,text("hapticfeedbackeffect_curve'/game/virtualrealitybp/blueprints/motioncontrollerhaptics.motioncontrollerhaptics'"));
.h:
fstreamablemanager* wealthloader;
tsharedptr<fstreamablehandle> wealthhandle;
.cpp:
//异步加载,一般用于大量物体加载 void awelathactor::streamablemanageroperate() { //创建加载管理器 wealthloader = new fstreamablemanager(); //执行异步加载,添加资源链接数组和加载完成回调函数,其中texturepath为加载内容的地址 wealthhandle = wealthloader->requestasyncload(texturepath, fstreamabledelegate::createuobject(this, &awelathactor::streamablemanagerloadcomplete)); } void awelathactor::streamablemanagerloadcomplete() { //加载完成后动态修改图片 tarray<uobject* >outobjects; wealthhandle->getloadedassets(outobjects); for (int32 i = 0; i < outobjects.num(); ++i) { utexture2d* worktexture = cast<utexture2d>(outobjects[i]); if (worktexture) { texturegroup.add(worktexture); } } }
3.通过uobjectlibrary获取批量内容的地址
.h: class uobjectlibrary* objectlibrary; .cpp: void awelathactor::objectlibraryoperate() { if (!objectlibrary) { objectlibrary = uobjectlibrary::createlibrary(uobject::staticclass(), false, false); //添加到根那,防止被ue4的垃圾回收机制干掉 objectlibrary->addtoroot(); } //搜索所有texture的路径 objectlibrary->loadassetdatafrompath(text("/game/resource/ui/texture/menutex")); tarray<fassetdata> texturedata; objectlibrary->getassetdatalist(texturedata); for (int32 i=0; i<texturedata.num(); ++i) { texturepath.addunique(texturedata[i].tosoftobjectpath()); } }
4.计时器
.h: ftimerhandle countdowntimerhandle; .cpp: //事件委托 ftimerdelegate updatetexturedele = ftimerdelegate::createuobject(this, &awelathactor::updatetexture); //每0.5秒执行一次事件委托 getworld()->gettimermanager().settimer(countdowntimerhandle, updatetexturedele, 0.5f, true); //停止运行定时器 getworldtimermanager().cleartimer(countdowntimerhandle);
上一篇: rabbitmq实现向各服务广播消息