zoukankan      html  css  js  c++  java
  • [UE4]C++中SpawnActor用法(动态创建Actor)

    C++中创建一个Level并添加的Runtime当中

    Level.Add(GetWorld()->SpawnActor<ABuildingModLevel>()); 

    C++中Spawn一个基于蓝图的Actor

    https://answers.unrealengine.com/questions/60897/spawn-actorobject-from-code.htm

    Here is how I spawn a blueprint via C++. Note that the blueprint I spawn has a base class that was created in C++ also.

    .h

    TSubclassOf<YourClass> BlueprintVar; // YourClass is the base class that your blueprint uses 

    .cpp(注意,这段代码必须放在构造函数中。UE4其他类型的蓝图,比如Widget蓝图,都可以通过下面这种方式加载。)

    ClassThatWillSpawnTheBlueprint::ClassThatWillSpawnTheBlueprint(const class FPostConstructInitializeProperties& PCIP)  
        : Super(PCIP)  
    {  
        static ConstructorHelpers::FObjectFinder<UBlueprint> PutNameHere(TEXT("Blueprint'/Path/To/Your/Blueprint/BP.BP'"));  
        if (PutNameHere.Object)   
        {  
            BlueprintVar = (UClass*)PutNameHere.Object->GeneratedClass;  
        }  
    }  

    PutNameHere is just an arbitrary name you give to the constructor helper. The path to your blueprint is found by finding your blueprint in the content browser, right clicking it, and choosing Copy Reference. Then, just paste that in between the quotes.

    Now, you're ready to spawn the blueprint. You can do it in BeginPlay() or wherever, just not in the constructor.(这段代码必须放在非构造函数中,比如BeginPlay()中

    UWorld* const World = GetWorld(); // get a reference to the world  
    if (World)   
    {  
        // if world exists  
        YourClass* YC = World->SpawnActor<YourClass>(BlueprintVar, SpawnLocation, SpawnRotation);  
    }  

    If you don't know your SpawnLocation or SpawnRotation you can just throw in FVector(0,0,0) and FRotator(0,0,0) instead.

    So, since your blueprint base class was also created in C++ this makes it easy to interact with your blueprint from code. It's as simple as YC->SomeVariable = SomeValue. Hope that helps.

    APuzzleBlock* NewBlock = GetWorld()->SpawnActor<APuzzleBlock>(BlockLocation, FRotator(0,0,0));
  • 相关阅读:
    Cocos-js(html5) 学习
    Cocos-html5 初识
    quick cocos2d-x Xcode下省去clean,让修改的脚本生效
    iOS 淘宝买 开发者证书 进行真机调试
    iOS NSString的学习熟悉
    quick-cocos2d-x 加载进度条的学习
    HTML 学习之HTML语言嵌入JavaScript
    CCArray的使用(Quick-cocos2d-x)
    quick-cocos2d-x 精灵使用、动作等等的学习
    Spring(二) Mini版Spring的实现
  • 原文地址:https://www.cnblogs.com/timy/p/8622617.html
Copyright © 2011-2022 走看看