feat: 增加输入系统
This commit is contained in:
61
Source/lonese/MyCharacter.cpp
Normal file
61
Source/lonese/MyCharacter.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
// Fill out your copyright notice in the Description page of Project Settings.
|
||||
|
||||
|
||||
#include "EnhancedInputSubsystems.h"
|
||||
#include "EnhancedInputComponent.h"
|
||||
|
||||
#include "MyCharacter.h"
|
||||
|
||||
void AMyCharacter::Move(const FInputActionValue& Value)
|
||||
{
|
||||
auto f2d = Value.Get<FVector2D>();
|
||||
AddMovementInput(GetActorRotation().Vector());
|
||||
}
|
||||
|
||||
// Sets default values
|
||||
AMyCharacter::AMyCharacter()
|
||||
{
|
||||
// Set this character 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 AMyCharacter::BeginPlay()
|
||||
{
|
||||
Super::BeginPlay();
|
||||
|
||||
}
|
||||
|
||||
// Called every frame
|
||||
void AMyCharacter::Tick(float DeltaTime)
|
||||
{
|
||||
Super::Tick(DeltaTime);
|
||||
|
||||
}
|
||||
|
||||
// Called to bind functionality to input
|
||||
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
|
||||
{
|
||||
Super::SetupPlayerInputComponent(PlayerInputComponent);
|
||||
|
||||
auto PlayerController = Cast<APlayerController>(GetController());
|
||||
UEnhancedInputLocalPlayerSubsystem* Subsystem;
|
||||
if (PlayerController)
|
||||
{
|
||||
Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer());
|
||||
}
|
||||
if (Subsystem && DefaultMapping)
|
||||
{
|
||||
Subsystem->AddMappingContext(DefaultMapping, 0);
|
||||
}
|
||||
|
||||
auto EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent);
|
||||
if (EnhancedInputComponent)
|
||||
{
|
||||
if (MoveAction) {
|
||||
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMyCharacter::Move);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
37
Source/lonese/MyCharacter.h
Normal file
37
Source/lonese/MyCharacter.h
Normal file
@@ -0,0 +1,37 @@
|
||||
// Fill out your copyright notice in the Description page of Project Settings.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "GameFramework/Character.h"
|
||||
#include "MyCharacter.generated.h"
|
||||
|
||||
UCLASS()
|
||||
class LONESE_API AMyCharacter : public ACharacter
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
|
||||
class UInputMappingContext* DefaultMapping;
|
||||
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
|
||||
class UInputAction* MoveAction;
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "Move")
|
||||
void Move(const FInputActionValue& Value);
|
||||
|
||||
// Sets default values for this character's properties
|
||||
AMyCharacter();
|
||||
|
||||
// Called every frame
|
||||
virtual void Tick(float DeltaTime) override;
|
||||
|
||||
// Called to bind functionality to input
|
||||
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
|
||||
protected:
|
||||
// Called when the game starts or when spawned
|
||||
virtual void BeginPlay() override;
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
@@ -10,5 +10,5 @@ APawn* AMyPlayerController::GetOwnedPawn()
|
||||
|
||||
void AMyPlayerController::BeginPlay()
|
||||
{
|
||||
//GetPawn()->SetActorLocation(FVector(0.f, 0.f, 300.f));
|
||||
}
|
||||
Super::BeginPlay();
|
||||
}
|
||||
@@ -22,5 +22,4 @@ protected:
|
||||
virtual void BeginPlay() override;
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
@@ -8,7 +8,7 @@ public class lonese : ModuleRules
|
||||
{
|
||||
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
|
||||
|
||||
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
|
||||
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput" });
|
||||
|
||||
PrivateDependencyModuleNames.AddRange(new string[] { });
|
||||
|
||||
|
||||
@@ -1,29 +1,34 @@
|
||||
{
|
||||
"FileVersion": 3,
|
||||
"EngineAssociation": "5.7",
|
||||
"Category": "",
|
||||
"Description": "",
|
||||
"Modules": [
|
||||
{
|
||||
"Name": "lonese",
|
||||
"Type": "Runtime",
|
||||
"LoadingPhase": "Default",
|
||||
"AdditionalDependencies": [
|
||||
"Engine"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Plugins": [
|
||||
{
|
||||
"Name": "ModelingToolsEditorMode",
|
||||
"Enabled": true,
|
||||
"TargetAllowList": [
|
||||
"Editor"
|
||||
]
|
||||
},
|
||||
{
|
||||
"Name": "Text3D",
|
||||
"Enabled": true
|
||||
}
|
||||
]
|
||||
"FileVersion": 3,
|
||||
"EngineAssociation": "5.7",
|
||||
"Category": "",
|
||||
"Description": "",
|
||||
"Modules": [
|
||||
{
|
||||
"Name": "lonese",
|
||||
"Type": "Runtime",
|
||||
"LoadingPhase": "Default"
|
||||
}
|
||||
],
|
||||
"Plugins": [
|
||||
{
|
||||
"Name": "ModelingToolsEditorMode",
|
||||
"Enabled": true
|
||||
},
|
||||
{
|
||||
"Name": "Text3D",
|
||||
"Enabled": true
|
||||
},
|
||||
{
|
||||
"Name": "VisualStudioTools",
|
||||
"Enabled": true,
|
||||
"SupportedTargetPlatforms": [
|
||||
"Win64"
|
||||
]
|
||||
}
|
||||
],
|
||||
"TargetPlatforms": [],
|
||||
"AdditionalRootDirectories": [],
|
||||
"AdditionalPluginDirectories": [],
|
||||
"EpicSampleNameHash": ""
|
||||
}
|
||||
Reference in New Issue
Block a user