UE4中Static中使用WorldContextObject

有几种方法,但是当你需要播放关卡序列或者其他东西时,没有WorldContextObject就不太OK了。
我看了一下UE4 源码,Epic的程序员是这样解决的,
在此静态函数中重新创建一个物体然后使用它的WorldContextObject
不得不说这个设计真的很蠢,但是好像他们也没什么特别好的解决方案,所以我们沿用他们的设计

///summary
///Settings和OutActor不是必选项,只是懒得改,InLevelSequence不能为空
ULevelSequencePlayer* UBPNode_SController::PlaySequence(UObject* WorldContextObject, ULevelSequence* InLevelSequence,
	FMovieSceneSequencePlaybackSettings Settings, ALevelSequenceActor*& OutActor)
{
	UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);

	FActorSpawnParameters SpawnParams;
	SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
	SpawnParams.ObjectFlags |= RF_Transient;
	SpawnParams.bAllowDuringConstructionScript = true;

	// Defer construction for autoplay so that BeginPlay() is called
	SpawnParams.bDeferConstruction = true;

	ALevelSequenceActor* Actor = World->SpawnActor<ALevelSequenceActor>(SpawnParams);

	Actor->PlaybackSettings = Settings;
	Actor->LevelSequence = InLevelSequence;

	Actor->InitializePlayer();
	OutActor = Actor;

	FTransform DefaultTransform;
	Actor->FinishSpawning(DefaultTransform);
	return Actor->SequencePlayer;
}

定义中

	UFUNCTION(BlueprintCallable,DisplayName = "PlaySequence",meta =(WorldContext = "WorldContextObject",DynamicOutputParam="OutActor"),Category="Custom")
    static ULevelSequencePlayer* PlaySequence(UObject* WorldContextObject, ULevelSequence* InLevelSequence,FMovieSceneSequencePlaybackSettings Settings, ALevelSequenceActor*& OutActor);

所以就是说播放关卡序列光使用WorldContext = "WorldContextObject"是不够的,得重新创建一个物体,还是得说这个设计真的很蠢,浪费我一两天时间尝试各种方案

上一篇:UE4大数据可视化教程(一)


下一篇:Broadcast