(UE4 4.20)UE4 TimeLine(UTimelineComponent)
藍圖TimeLine
在UE4中,實現一段時間內數值呈現自定義變化的功能,可以用來藍圖節點的TimeLine 功能,
如下所示:
雙擊TimeLine節點能定義各種自定義的數值變化曲線, X軸是時間,Y軸是值,如下所示:
UTimeLineComponent 和 UCurveFloat(或者UCurveVector, UCurveLinearColor)
那麼如何用UE4 C++實現藍圖節點的TimeLine 功能呢,我找到資料可以用UTimeLineComponent元件(不可掛載)
看原始碼:
/**
* TimelineComponent holds a series of events, floats, vectors or colors with associated keyframes.
* Events can be triggered at keyframes along the timeline.
* Floats, vectors, and colors are interpolated between keyframes along the timeline.
*/
UCLASS(MinimalAPI)
class UTimelineComponent : public UActorComponent
{
GENERATED_UCLASS_BODY()
private:
/** The actual timeline structure */
UPROPERTY(ReplicatedUsing=OnRep_Timeline)
FTimeline TheTimeline;
/** True if global time dilation should be ignored by this timeline, false otherwise. */
UPROPERTY()
uint32 bIgnoreTimeDilation : 1;
UTimeLineComponent封裝了FTimeLine, 提供了對應於藍圖節點的各種功能(Play, PlayFromStart, Reverse等等),看下面
UFUNCTION(BlueprintCallable, Category="Components|Timeline")
ENGINE_API void Play();
/** Start playback of timeline from the start */
UFUNCTION(BlueprintCallable, Category="Components|Timeline")
ENGINE_API void PlayFromStart();
/** Start playback of timeline in reverse */
UFUNCTION(BlueprintCallable, Category="Components|Timeline")
ENGINE_API void Reverse();
/** Start playback of timeline in reverse from the end */
UFUNCTION(BlueprintCallable, Category="Components|Timeline")
ENGINE_API void ReverseFromEnd();
/** Stop playback of timeline */
UFUNCTION(BlueprintCallable, Category="Components|Timeline")
下面是用UTimeLineComponent 和 函式數值曲線 實現的一段演示
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TestActor.generated.h"
class UCurveFloat;
class UTimelineComponent;
UCLASS()
class MYPROJECT_API ATestActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATestActor();
UPROPERTY(EditAnywhere)
UCurveFloat* CurveFloat;
UPROPERTY()
UTimelineComponent* timeLineComponent;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
UFUNCTION()
void OnTimelineCallBack(float value);
UFUNCTION()
void OnTimelineFinishedCallback();
};
#include "TestActor.h"
#include "Components/TimelineComponent.h"
#include "Engine/Engine.h"
// Sets default values
ATestActor::ATestActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ATestActor::BeginPlay()
{
Super::BeginPlay();
timeLineComponent = NewObject<UTimelineComponent>(this);
timeLineComponent->RegisterComponent();
timeLineComponent->CreationMethod = EComponentCreationMethod::UserConstructionScript;
this->BlueprintCreatedComponents.Add(timeLineComponent);
timeLineComponent->SetIsReplicated(false);
timeLineComponent->SetPropertySetObject(this);
timeLineComponent->SetLooping(false);
timeLineComponent->SetTimelineLength(5.0f);
//timeLineComponent->SetNewTime(5.0f);
timeLineComponent->SetTimelineLengthMode(ETimelineLengthMode::TL_TimelineLength);
timeLineComponent->SetPlaybackPosition(0.0f, false);
timeLineComponent->SetDirectionPropertyName(FName("TimelineDirection"));
FOnTimelineFloat onTimelineCallBack;
FOnTimelineEventStatic onTimelineFinishedCallback;
onTimelineCallBack.BindUFunction(this, FName{ TEXT("OnTimelineCallBack") });
onTimelineFinishedCallback.BindUFunction(this, FName{ TEXT("OnTimelineFinishedCallback") });
timeLineComponent->AddInterpFloat(CurveFloat, onTimelineCallBack);
timeLineComponent->SetTimelineFinishedFunc(onTimelineFinishedCallback);
timeLineComponent->Play();
}
void ATestActor::OnTimelineCallBack(float value)
{
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red, FString::Printf(TEXT("%f"), value));
UE_LOG(LogTemp, Warning, TEXT("%f"), value);
}
void ATestActor::OnTimelineFinishedCallback()
{
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red, FString("Finish"));
UE_LOG(LogTemp, Warning, TEXT("Finish"));
}
採用的函式曲線:
我們的 TimeLine 為 5s
執行結果:
這裡比較注意的是 UTimeLineComponent 的 介面 SetTimelineLengthMode 對應於 藍圖Timeline 編輯曲線的
“Use LastKeyFrame” , 其他的對應介面差不多命名都一樣。
參考資料
相關文章
- (UE4 4.20)UE4 UCLASS,UENUM, USTRUCT, UPROPERTY 的 常用配置Struct
- (UE4 4.20 )UE4的GC(垃圾回收)程式設計規範GC程式設計
- (UE4 4.20)UE4 如何判斷一個點是否在導航網格(Navigation)內Navigation
- (UE4 4.20)UE4 繼承AnimNotify建立自定義動畫通知事件(結合PoseableMeshComponent實現技能殘影效果)繼承動畫事件
- UE4委託
- UE4 智慧指標指標
- 《Inside UE4》開篇IDE
- UE4 Dash功能實現
- UE4藍圖學習
- UE4 C++ 攀爬功能C++
- UE4 LoadMap 初始化
- UE4的移動碰撞
- UE4點選原始碼分析原始碼
- ue4打包遊戲遊戲
- UE4 c++ -- 簡單的UMGC++
- UE4網路模組解析(一)
- UE4 ProjectileMovement Component 延遲啟動Project
- ue4新增使用者介面、UIUI
- UE4 C++ Widget的NativeConstruct 與 NativePreConstructC++Struct
- UE4藍圖AI角色製作(三)AI
- UE4中C++程式設計(一)C++程式設計
- Houdini在UE4特效中的嘗試分享特效
- UE4 Shader 編譯以及變種實現編譯
- UE4 C++(11):移動元件和碰撞C++元件
- UE4 地形編輯-建立地形Landscape/terrainAI
- ue4為角色新增HP並繫結UIUI
- ue4繫結動畫、重定向動畫動畫
- 《Exploring in UE4》移動元件詳解[原理分析]元件
- UE4 中射線檢測的簡單探索
- 雲渲染系統可多個UE4程式使用嗎?
- UE4 UDP是如何進行可靠傳輸的UDP
- 如何在UE4中實現植物風場效果?
- 《Exploring in UE4》遊戲角色的移動原理(上)遊戲
- 《Exploring in UE4》遊戲角色的移動原理(下)遊戲
- UE4中的主要材料和光線跟蹤
- 如何在 UE4 移動端中實現 HZB?
- 《Exploring in UE4》網路同步原理深入(下):原理分析
- UE4純C++實現遊戲中快捷欄C++遊戲