UEC++生成Actor

1、直接生成Actor

 

GetWorld()->SpawnActor<AActor>();

 

GetWorld()->SpawnActor(AActor::StaticClass());

 

UWorld* world = GetWorld();
world->SpawnActor<AActor>(AActor::StaticClass());
SpawnActor 在 world 类中的定义
    /** Templated version of SpawnActor that allows you to specify a class type via the template type */
    template< class T >
    T* SpawnActor( const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters() )
    {
        return CastChecked<T>(SpawnActor(T::StaticClass(), NULL, NULL, SpawnParameters),ECastCheckedType::NullAllowed);
    }

    /** Templated version of SpawnActor that allows you to specify location and rotation in addition to class type via the template type */
    template< class T >
    T* SpawnActor( FVector const& Location, FRotator const& Rotation, const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters() )
    {
        return CastChecked<T>(SpawnActor(T::StaticClass(), &Location, &Rotation, SpawnParameters),ECastCheckedType::NullAllowed);
    }
    
    /** Templated version of SpawnActor that allows you to specify the class type via parameter while the return type is a parent class of that type */
    template< class T >
    T* SpawnActor( UClass* Class, const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters() )
    {
        return CastChecked<T>(SpawnActor(Class, NULL, NULL, SpawnParameters),ECastCheckedType::NullAllowed);
    }

    /** 
     *  Templated version of SpawnActor that allows you to specify the rotation and location in addition
     *  class type via parameter while the return type is a parent class of that type 
     */
    template< class T >
    T* SpawnActor( UClass* Class, FVector const& Location, FRotator const& Rotation, const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters() )
    {
        return CastChecked<T>(SpawnActor(Class, &Location, &Rotation, SpawnParameters),ECastCheckedType::NullAllowed);
    }
    /** 
     *  Templated version of SpawnActor that allows you to specify whole Transform
     *  class type via parameter while the return type is a parent class of that type 
     */
    template< class T >
    T* SpawnActor(UClass* Class, FTransform const& Transform,const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters())
    {
        return CastChecked<T>(SpawnActor(Class, &Transform, SpawnParameters), ECastCheckedType::NullAllowed);
    }

 

2、滞后生成Actor

    // 创建actor生成变换
    FTransform tran;
    // 滞后生成Actor
    AActor* actor = GetWorld()->SpawnActorDeferred<AActor>(AActor::StaticClass(), tran);
    // 确定生成对象到世界
    actor->FinishSpawning(tran);
SpawnActorDeferred 在 world 类中的定义
    /**
     * Spawns given class and returns class T pointer, forcibly sets world transform (note this allows scale as well). WILL NOT run Construction Script of Blueprints 
     * to give caller an opportunity to set parameters beforehand.  Caller is responsible for invoking construction
     * manually by calling UGameplayStatics::FinishSpawningActor (see AActor::OnConstruction).
     */
    template< class T >
    T* SpawnActorDeferred(
        UClass* Class,
        FTransform const& Transform,
        AActor* Owner = nullptr,
        APawn* Instigator = nullptr,
        ESpawnActorCollisionHandlingMethod CollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::Undefined
        )
    {
        if( Owner )
        {
            check(this==Owner->GetWorld());
        }
        FActorSpawnParameters SpawnInfo;
        SpawnInfo.SpawnCollisionHandlingOverride = CollisionHandlingOverride;
        SpawnInfo.Owner = Owner;
        SpawnInfo.Instigator = Instigator;
        SpawnInfo.bDeferConstruction = true;
        return (Class != nullptr) ? Cast<T>(SpawnActor(Class, &Transform, SpawnInfo)) : nullptr;
    }

 

posted @ 2022-09-17 19:01  黎沐不吃香菜  阅读(1437)  评论(0)    收藏  举报