keywords:UE4, Dynamic Load, Animation Blueprint, LoadObject, LoadClass, 动画蓝图, 资源加载

问题现象

假如用以下方式加载动画蓝图:

FString AnimBPStringTest = "AnimBlueprint'/Game/ThirdPerson/Animations/ThirdPerson_AnimBP.ThirdPerson_AnimBP'";
UAnimBlueprint* AnimationBP = LoadObject<UAnimBlueprint>(nullptr, *AnimBPStringTest);

在PIE和Standalone模式下都可加载成功,但是一旦打包运行(打包配置中添加了该资源cook)就会加载失败,并提示如下错误:

LogUObjectGlobals:Warning: Failed to find object 'AnimBlueprint /Game/ThirdPerson/Animations/ThirdPerson_AnimBP.ThirdPerson_AnimBP'
解决办法

方式一:

FString AnimClassPath = "Class'/Game/mixamo/Heidi/IcloneAnimBP.IcloneAnimBP_C'";
UClass* AnimationClass = LoadObject<UClass>(nullptr, *AnimClassPath);
SKMesh->SetAnimInstanceClass(AnimationClass);

方式二:

UClass* AnimClass = LoadClass<UAnimInstance>(nullptr,
                    TEXT("AnimBlueprint'/Game/ABP_Test.ABP_Test_C'"));
SKMesh->SetAnimInstanceClass(AnimClass);

参考:Why can’t i dynamically load an animation BP in a packaged game
https://answers.unrealengine.com/questions/263863/why-cant-i-dynamically-load-an-animation-bp-in-a-p.html


二八佳人体似酥,腰间仗剑斩凡夫。虽然不见人头落,暗里教君骨髓枯。 ---唐·吕洞宾