当前位置:   article > 正文

UE4 如何用C++创建Timeline 计时器_ue创建timeline组件

ue创建timeline组件

一、前期准备

二、代码实现

三、结果

1.1  前期准备工作
  • 1

先创建一个UE4的C++项目,然后创建曲线,设置曲线,这里设置的点是(0,0)(300,300),创建C++Actor类

这里是引用在这里插入图片描述
在这里插入图片描述

2.2代码部分
  • 1
```.h文件
//定义一个浮点曲线
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TimeCurve")
		UCurveFloat* MyTimeCurve;
	//创建一个时间轴注意头文件#include"Runtime\Engine\Classes\Components\TimelineComponent.h"
	FTimeline MyTime;
	//定义时间轴曲线更新事件
	FOnTimelineFloatStatic OnMyCallTimeDelegate;
	//定义时间轴曲线播放结束事件
	FOnTimelineEventStatic OnMyFinishTimeDelegate;
	//每帧执行回调函数
	UFUNCTION()
		void CallTime();

	//时间轴播放结束回调函数
	UFUNCTION()
		void FinishTime();
	//分钟
	UPROPERTY(VisibleDefaultsOnly, Category = "RenderText")
		UTextRenderComponent* Seconds;
	//秒
	UPROPERTY(VisibleDefaultsOnly, Category = "RenderText")
		UTextRenderComponent* Minutes;
	//设置:
	UPROPERTY(VisibleDefaultsOnly, Category = "RenderText")
		UTextRenderComponent* Dian;
	float FloatCurveVal;


```cpp文件
AMyTime::AMyTime()
{
 	// 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;
	//设置分 文本渲染器
	Seconds = CreateDefaultSubobject<UTextRenderComponent>(TEXT("SecondsText"));
	Seconds->SetWorldSize(20.f);
	Seconds->SetTextRenderColor(FColor::Black);
	Seconds->SetHorizontalAlignment(EHTA_Center);
	Seconds->SetupAttachment(RootComponent);
	//设置秒 文本渲染器
	Minutes = CreateDefaultSubobject<UTextRenderComponent>(TEXT("MinutesText"));
	Minutes->SetWorldSize(20.f);
	Minutes->SetTextRenderColor(FColor::Black);
	Minutes->SetHorizontalAlignment(EHTA_Center);
	Minutes->SetupAttachment(Seconds);
	Minutes->SetRelativeLocation(FVector(0,-40,0));
	static ConstructorHelpers::FObjectFinder<UCurveFloat>Curve(TEXT("CurveFloat'/Game/MyTimeCurveFloat.MyTimeCurveFloat'"));
	MyTimeCurve = Curve.Object;

	//设置:
	Dian = CreateDefaultSubobject<UTextRenderComponent>(TEXT("dianText"));
	Dian->SetWorldSize(20.f);
	Dian->SetTextRenderColor(FColor::Black);
	Dian->SetHorizontalAlignment(EHTA_Center);
	Dian->SetupAttachment(Seconds);
	Dian->SetRelativeLocation(FVector(0, -20, 0));
	Dian->SetText(TEXT(":"));
}
void AMyTime::BeginPlay()
{
	Super::BeginPlay();
	//初始化
	OnMyCallTimeDelegate.BindUFunction(this, TEXT("CallTime"));
	MyTime.AddInterpFloat(MyTimeCurve,OnMyCallTimeDelegate);

	OnMyFinishTimeDelegate.BindUFunction(this,TEXT("FinishTime"));
	MyTime.SetTimelineFinishedFunc(OnMyFinishTimeDelegate);


	MyTime.ReverseFromEnd();
}

// Called every frame
void AMyTime::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	MyTime.TickTimeline(DeltaTime);
}

void AMyTime::CallTime()
{
	FloatCurveVal = MyTimeCurve->GetFloatValue(MyTime.GetPlaybackPosition());
	//将整型变量转成字符
	FString strSecondTime = FString::FromInt(UKismetMathLibrary::FFloor(FloatCurveVal/60));
	Seconds->SetText(strSecondTime);
	//设置秒
	int32 intMinutes = FloatCurveVal - (UKismetMathLibrary::FFloor(FloatCurveVal/60) * 60);
	//将整型变量转成字符
	FString strMinuteTime = FString::FromInt(UKismetMathLibrary::FFloor(intMinutes));
	Minutes->SetText(strMinuteTime);
	GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red, TEXT("TimeStart"));
}

void AMyTime::FinishTime()
{
	GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red, TEXT("TimeFinish"));
}

结果运行:

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/127215
推荐阅读
相关标签
  

闽ICP备14008679号