1.輸入模式結構體
FInputModeGameOnly
:玩家只能與游戲世界交互,UI 不可交互。FInputModeGameAndUI
:玩家可以與游戲世界和 UI 同時交互。FInputModeUIOnly
:玩家只能與 UI 交互,無法與游戲世界進行互動。
????????FInputModeGameOnly:
構造函數,默認設置輸入模式為僅游戲輸入模式,鼠標光標會隱藏,所有輸入都會被傳遞到游戲。沒有額外的設置函數,FInputModeGameOnly
只會設置輸入為游戲模式,不需要額外的配置。
??FInputModeGameAndUI:
構造函數,默認設置輸入模式為游戲和 UI 模式。在此模式下,玩家可以同時與游戲和 UI 交互。
????????FInputModeUIOnly:構造函數,默認設置輸入模式為僅 UI 輸入模式。在此模式下,玩家只能與 UI 進行交互,游戲輸入會被禁用
? ? ? ? 函數SetWidgetToFocus(TSharedPtr<SWidget> InWidgetToFocus) 參數是某個UI界面的指針,作用是將只關注這個InWidgetToFocus界面
? ? ? ? 函數SetLockMouseToViewportBehavior(EMouseLockMode InMouseLockMode)設置鼠標模式,EMouseLockMode這個枚舉中有注釋
? ? ? ? 這兩個函數可能只會在FInputModeGameAndUI,和
FInputModeUIOnly使用。
2.OpenLevel函數,切換關卡
#include "Kismet/GameplayStatics.h"UGameplayStatics::OpenLevel(const UObject* WorldContextObject, // 一般是UWorld指針,或者是APlayerController指針。UWorld指針可以通過GetWorld()函數獲得 , APlayerController看你在哪個類中,查詢具體的獲得方式,一般是UWorldFName LevelName, //切換到哪個關卡的名字bool bAbsolute = true, //是否是絕對路徑 ,一般是默認值,第二個參數給地圖的名字即可FString Options = FString(TEXT("")) // 不知到是干什么用的,
)
3.UI界面
? ? ? ? 1.創建自UserWidget的子類,(UE中還有一個HUD也是于UI相關的類,我的理解是如果想顯示角色的血量,子彈數等比較小的UI使用HUD,如果是游戲初始界面使用UserWidget)
? ? ? ? 2.類中代碼 ,?MenuSetup函數設置為藍圖可以調用的函數,使用AddToViewport顯示UI界面,設置FInputModeUIOnly,我在點擊按鈕后設置了游戲模式函數是Button0Printf,在Initilize函數中綁定,Button0是按鈕的名字,要和UE中的UI編輯器中的名字一樣,SetGameModel函數中設置FInputModeGameOnly InputModeData;SetInputMode(InputModeData);兩行。在是地圖的關卡藍圖中調用menuSetup函數,中間的界面是create widget選擇Class需要創建這個UI類的藍圖類,在切換關卡后如果角色不可以操作可以查看
輸入模式結構體有沒有重新設置成FInputModeGameOnly或者FInputModeGameAndUI
class JUMP_API UStartUserWidget : public UUserWidget
{GENERATED_BODY()public:UFUNCTION(BlueprintCallable)void MenuSetup();protected:virtual bool Initialize() override;public:UPROPERTY(meta = (BindWidget))UButton* Button0;private:UFUNCTION()void Button0Printf();
};void UStartUserWidget::MenuSetup()
{AddToViewport();SetVisibility(ESlateVisibility::Visible);/* 設置可見性 */bIsFocusable = true;/* 設置聚焦模式 */UWorld* World = GetWorld();if (World){APlayerController* PlayerController = World->GetFirstPlayerController();if (PlayerController){FInputModeUIOnly InputModeData;InputModeData.SetWidgetToFocus(TakeWidget());/* 設置只關注與小部件 */InputModeData.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);/* 設置鼠標光標鎖定 */PlayerController->SetInputMode(InputModeData);//設置模式集中于界面PlayerController->SetShowMouseCursor(true);/* 看到光標 */}}
}bool UStartUserWidget::Initialize()
{if (!Super::Initialize()){return false;}if (Button0){Button0->OnClicked.AddDynamic(this, &UStartUserWidget::Button0Printf);//綁定回調函數}return true;
}void UStartUserWidget::Button0Printf()
{UWorld* World = GetWorld();if (World){UGameplayStatics::OpenLevel(World, FName(TEXT("JumpMap")));AJumpCharacterController* PlayerController = Cast<AJumpCharacterController>(World->GetFirstPlayerController());if (PlayerController){PlayerController->SetGameModel();}}else{UE_LOG(LogTemp, Warning, TEXT("World pointer is nullptr"));}
}
?4.關于UE中父類容器的概念
? ? ? ? 父類容器的概念通常是指 一個類或對象包含并管理其他對象或數據的結構,比如UWorld中管理這很多AActor。使用GetOuter()函數可以返回當前類的父類容器,父類容器絕對不是當前類的父類。