时钟
TimeOfDayHandler
注册代理,执行代理
#pragma once
#include "GameFramework/Actor.h"
#include "TimeOfDayHandler.generated.h"
DECLARE_MULTICAST_DELEGATE_TwoParams(FOnTimeChangedSignature, int32, int32)
UCLASS()
class BLANKUECPP_API ATimeOfDayHandler : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATimeOfDayHandler();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY()
int32 TimeScale;
UPROPERTY()
int32 Hours;
UPROPERTY()
int32 Minutes;
UPROPERTY()
float ElapsedSeconds;
FOnTimeChangedSignature OnTimeChanged;
};
#include "BlankUECPP.h"
#include "TimeOfDayHandler.h"
ATimeOfDayHandler::ATimeOfDayHandler()
{
PrimaryActorTick.bCanEverTick = true;
TimeScale = 60;
Hours = 0;
Minutes = 0;
ElapsedSeconds = 0.0f;
RootComponent = CreateDefaultSubobject<UBillboardComponent>(TEXT("BillBoard"));
}
void ATimeOfDayHandler::BeginPlay()
{
Super::BeginPlay();
}
void ATimeOfDayHandler::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
ElapsedSeconds += (DeltaTime*TimeScale);
if (ElapsedSeconds >= 60)
{
ElapsedSeconds -= 60;
Minutes++;
}
if (Minutes >= 60)
{
Minutes -= 60;
Hours++;
}
GEngine->AddOnScreenDebugMessage(-1, 0.5f, FColor::Red, FString::Printf(TEXT("M:%d H:%d"), Minutes,Hours));
OnTimeChanged.Broadcast(Hours, Minutes);
}
DemoClock
加载素材资源,绑定代理
#pragma once
#include "GameFramework/Actor.h"
#include "DemoClock.generated.h"
UCLASS()
class BLANKUECPP_API ADemoClock : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ADemoClock();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void TimeChanged(int32 Hours,int32 Minutes);
public:
UPROPERTY()
UStaticMeshComponent* ClockFace;
UPROPERTY()
UStaticMeshComponent* HourHand;
UPROPERTY()
UStaticMeshComponent* MinuteHand;
UPROPERTY()
USceneComponent* HourHandle;
UPROPERTY()
USceneComponent* MinuteHandle;
FDelegateHandle DelegateHandle;
};
#include "BlankUECPP.h"
#include "DemoClock.h"
#include "TimeOfDayHandler.h"
ADemoClock::ADemoClock()
{
PrimaryActorTick.bCanEverTick = true;
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("SceneRoot"));
HourHandle=CreateDefaultSubobject<USceneComponent>(TEXT("HourHandle"));
MinuteHandle= CreateDefaultSubobject<USceneComponent>(TEXT("MinuteHandle"));
ClockFace = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ClockFace"));
HourHand= CreateDefaultSubobject<UStaticMeshComponent>(TEXT("HourHand"));
MinuteHand= CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MinuteHand"));
ConstructorHelpers::FObjectFinder<UStaticMesh> meshAsset(TEXT("StaticMesh'/Engine/BasicShapes/Cylinder.Cylinder'"));
if (meshAsset.Object)
{
ClockFace->SetStaticMesh(meshAsset.Object);
HourHand->SetStaticMesh(meshAsset.Object);
MinuteHand->SetStaticMesh(meshAsset.Object);
}
ClockFace->SetupAttachment(RootComponent);
ClockFace->SetRelativeTransform(FTransform(FRotator(0,0,90),FVector(0,0,0),FVector(2,2,0.5)));
HourHand->AttachToComponent(HourHandle,FAttachmentTransformRules::KeepRelativeTransform);
MinuteHand->AttachToComponent(MinuteHandle, FAttachmentTransformRules::KeepRelativeTransform);
HourHandle->SetupAttachment(RootComponent);
MinuteHandle->SetupAttachment(RootComponent);
HourHand->SetRelativeTransform(FTransform(FRotator(0, 0, 0), FVector(0, 55, 25), FVector(0.1, 0.1, 0.5)));
MinuteHand->SetRelativeTransform(FTransform(FRotator(0, 0, 0), FVector(0, 60, 50), FVector(0.1, 0.1, 1)));
}
void ADemoClock::BeginPlay()
{
Super::BeginPlay();
TArray<AActor*> TimeOfDayHandlers;
UGameplayStatics::GetAllActorsOfClass(GetWorld(),ATimeOfDayHandler::StaticClass(),TimeOfDayHandlers);
if (TimeOfDayHandlers.Num() != 0)
{
auto timeOfDayHandler = Cast<ATimeOfDayHandler>(TimeOfDayHandlers[0]);
// 这个是绑定当前代理
DelegateHandle = timeOfDayHandler->OnTimeChanged.AddUObject(this, &ADemoClock::TimeChanged);
}
}
// Called every frame
void ADemoClock::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ADemoClock::TimeChanged(int32 Hours, int32 Minutes)
{
HourHandle->SetRelativeRotation(FRotator(-30*Hours,0,0));
MinuteHandle->SetRelativeRotation(FRotator(-6*Minutes,0,0));
}