UE4 C++ Timeline
我命名有点不规范注意点看
结束事件绑定
每次更改绑定
调用时间轴开始方法方法
1.先建C++类,用碰撞触发时间轴
代码
.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/BoxComponent.h"
#include "Components/TimelineComponent.h"
#include "GameFramework/Actor.h"
#include "Door.generated.h"
UCLASS()
class QWE_API ADoor : public AActor
{
GENERATED_BODY()
public:
UPROPERTY(VisibleAnywhere)
USceneComponent* SceneComponent;
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* Door;
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* DoorClient;
UPROPERTY(VisibleAnywhere)
UBoxComponent* BoxComponent;
UPROPERTY(VisibleAnywhere)
UTimelineComponent* DoorTimeline;
UPROPERTY(EditAnywhere)
UCurveFloat* DoorTimelineFloatCurve;
public:
// Sets default values for this actors properties
ADoor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION()
void BeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
UFUNCTION()
void CompleteTimelineComp();
UFUNCTION()
void OnTimelineTick(float Output);
};
.CPP
// Fill out your copyright notice in the Description page of Project Settings.
#include "Actor/Door.h"
// Sets default values
ADoor::ADoor()
{
// 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;
SceneComponent=CreateDefaultSubobject<USceneComponent>(TEXT("SceneComponent"));
RootComponent=SceneComponent;
Door=CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Door"));
Door->SetupAttachment(SceneComponent);
DoorClient=CreateDefaultSubobject<UStaticMeshComponent>(TEXT("DoorClient"));
DoorClient->SetupAttachment(Door);
BoxComponent=CreateDefaultSubobject<UBoxComponent>(TEXT("BoxComponent"));
BoxComponent->SetupAttachment(SceneComponent);
DoorTimeline=CreateDefaultSubobject<UTimelineComponent>(TEXT("DoorTimeline"));
}
// Called when the game starts or when spawned
void ADoor::BeginPlay()
{
Super::BeginPlay();
BoxComponent->OnComponentBeginOverlap.AddDynamic(this,&ADoor::BeginOverlap);
/*DoorTimeline->SetLooping(true);//是否循环吧*/
if (DoorTimelineFloatCurve!=nullptr)
{
FOnTimelineFloat TimelineEvent;
FOnTimelineEventStatic TimelineEventend;
TimelineEvent.BindUFunction(this,TEXT("OnTimelineTick"));
TimelineEventend.BindUFunction(this,TEXT("CompleteTimelineComp"));
DoorTimeline->AddInterpFloat(DoorTimelineFloatCurve,TimelineEvent);
DoorTimeline->SetTimelineFinishedFunc(TimelineEventend);//完成时调用
DoorTimeline->SetTimelineLengthMode(ETimelineLengthMode::TL_LastKeyFrame);//设置结束为最后一个帧
}
}
// Called every frame
void ADoor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ADoor::BeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (DoorTimelineFloatCurve!=nullptr)
{
DoorTimeline->PlayFromStart();
}
UE_LOG(LogTemp,Warning,TEXT("Conlor"));
}
void ADoor::CompleteTimelineComp()
{
UE_LOG(LogTemp,Warning,TEXT("wang cheng"));
}
void ADoor::OnTimelineTick(const float Output)
{
UE_LOG(LogTemp,Warning,TEXT("%f"),Output);
}
**
设置时间轴曲线:
**
运行结果: