赞
踩
1.1 前期准备工作
先创建一个UE4的C++项目,然后创建曲线,设置曲线,这里设置的点是(0,0)(300,300),创建C++Actor类
这里是引用
2.2代码部分
```.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")); } 结果运行:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。