Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

ue4-资源加载和实例化类

$
0
0

加载资源,然后实例化对象


资源加载,并实例化

  • 构造中加载蓝图或c++类

    • 加载并实例化一个蓝图类

          static ConstructorHelpers::FObjectFinder<UMaterial> DecalMaterialAsset(TEXT("Material'/Game/TopDownCPP/Blueprints/M_Cursor_Decal.M_Cursor_Decal'")); // 这个路径是在编辑器中右键该资源 Copy Reference,Material是 name
          if (DecalMaterialAsset.Succeeded())
          {
              CursorToWorld->SetDecalMaterial(DecalMaterialAsset.Object);
          }
    • 实例化一个c++类

          CursorToWorld = CreateDefaultSubobject<UDecalComponent>("CursorToWorld"); //必须提供一个 name(CursorToWorld)
          CursorToWorld->SetupAttachment(RootComponent); //这是一个 USceneComponent 的子类,才能显示到编辑器的 Components窗口 中,因为 USceneComponent 组件带有 transform 信息,而 UActorComponent 没有。
    • 加载一个蓝图类,并实例化成蓝图对象

          static ConstructorHelpers::FClassFinder<UCoolDownComp> CDComplAsset(TEXT("/Game/TopDownCPP/MyBp/CDCompBp")); // 这个就不能右键该资源 Copy Reference,只能是不带name的一个路径
          if (CDComplAsset.Succeeded())
          {
              UE_LOG(LogMyTest, Warning, TEXT("--- CDComplAsset.Succeeded()"));
              mCDComp = NewObject<UCoolDownComp>(this, CDComplAsset.Class, "UCoolDownComp"); //必须提供一个 name(UCoolDownComp)
              mCDComp->SetupAttachment(RootComponent);
          }
  • 不在构造中实例化蓝图c++类

    • 实例化蓝图类

      1. 起个成员,可以让编辑器中指定 AMyBullet 派生的蓝图类

        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyChar")
            TSubclassOf<AMyBullet> BulletClass;
      2. 实例化这个蓝图类

            FActorSpawnParameters SpawnInfo;
            SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
            AMyBullet* mBullet = GWorld->SpawnActor<AMyBullet>(BulletClass, SpawnInfo);
            mBullet->SetPkMsg(pkMsg);
    • 实例化c++类,直接用 xxx::StaticClass()实例化

          FActorSpawnParameters SpawnInfo;
          SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
          AMyBullet* mBullet = GWorld->SpawnActor<AMyBullet>(AMyBullet::StaticClass(), SpawnInfo);
          mBullet->SetPkMsg(pkMsg);
  • 同步加载

    bool UMyBpFuncLib::TestChangeCharAnimInstance(AMyChar* _myChar, FString _pathMesh, FString _pathAnim)
    {
        FStreamableManager* stream = new FStreamableManager();
        FStringAssetReference ref1(*_pathMesh);
        USkeletalMesh* TmpMesh = Cast<USkeletalMesh>(stream->SynchronousLoad(ref1));
        _myChar->GetMesh()->SetSkeletalMesh(TmpMesh);
    
        FStringAssetReference ref2(*_pathAnim);
        UAnimBlueprint* TmpMeshAnim = Cast<UAnimBlueprint>(stream->SynchronousLoad(ref2));
        _myChar->GetMesh()->SetAnimInstanceClass((UClass*)TmpMeshAnim->GetAnimBlueprintGeneratedClass());
        delete stream;
        return true;
    }
  • 异步加载参照这篇:ue4-异步加载资源

作者:yangxuan0261 发表于2017/2/27 15:25:07 原文链接
阅读:17 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles