1.創建空白插件
2.導入在線子系統以及在線steam子系統庫
`MultiplayerSessions.uplugin`
MultiplayerSessions.Build.cs
3.創建游戲實例以及初始化會話創建流程
創建會話需要的函數,委托,委托綁定的回調,在線子系統接口綁定某一個委托的控制其綁定的生命周期的句柄
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "Subsystems/GameInstanceSubsystem.h"
#include "Interfaces/OnlineSessionInterface.h" // 包含委托定義
#include "MultiplayerSessionsSubsystem.generated.h"/*** */
UCLASS()
class MULTIPLAYERSESSIONS_API UMultiplayerSessionsSubsystem : public UGameInstanceSubsystem
{GENERATED_BODY()public:UMultiplayerSessionsSubsystem();/* 會話有關的函數 */void CreateSession(int32 NumPublicConnections, FString MatchType);void FindSessions(int32 MaxSearchResults);void JoinSession(const FOnlineSessionSearchResult& SearchResult);void DestroySession();void StartSession();protected:/* 委托的回調函數 */void OnCreateSessionComplete(FName SessionName, bool bWasSuccessful);void OnFindSessionsComplete(bool bWasSuccessful);void OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result);void OnDestroySessionComplete(FName SessionName, bool bWasSuccessful);void OnStartSessionComplete(FName SessionName, bool bWasSuccessful);private:/* 進入服務器的憑證,通過該憑證來加入同一個服務器 */IOnlineSessionPtr OnlineSessionInterface;/* 在線子系統的委托 */FOnCreateSessionCompleteDelegate OnCreateSessionCompleteDelegate;FOnFindSessionsCompleteDelegate OnFindSessionsCompleteDelegate;FOnJoinSessionCompleteDelegate OnJoinSessionCompleteDelegate;FOnDestroySessionCompleteDelegate OnDestroySessionCompleteDelegate;FOnStartSessionCompleteDelegate OnStartSessionCompleteDelegate;/* 在線子系統委托對應的句柄 *//* 例:當創建會話時,在線子系統會綁定創建完會話的委托,該函數會返回創建會話完成委托的句柄,來管理委托的綁定生命周期*/FDelegateHandle OnCreateSessionCompleteDelegateHandle;FDelegateHandle OnFindSessionsCompleteDelegateHandle;FDelegateHandle OnJoinSessionCompleteDelegateHandle;FDelegateHandle OnDestroySessionCompleteDelegateHandle;FDelegateHandle OnStartSessionCompleteDelegateHandle;private:/* 綁定委托的回調 */void BindCallBack();
};
實現代碼
4.創建系統菜單
編譯報錯
1>[3/4] Link [x64] UnrealEditor-MultiplayerSessions.dll (0:00.78 at +0:14)
1> 正在創建庫 H:\UEProject\5.3\MultiPlayer\MenuSystem\Plugins\MultiplayerSessions\Intermediate\Build\Win64\x64\UnrealEditor\Development\MultiplayerSessions\UnrealEditor-MultiplayerSessions.sup.lib 和對象 H:\UEProject\5.3\MultiPlayer\MenuSystem\Plugins\MultiplayerSessions\Intermediate\Build\Win64\x64\UnrealEditor\Development\MultiplayerSessions\UnrealEditor-MultiplayerSessions.sup.exp
1>Module.MultiplayerSessions.cpp.obj : error LNK2019: 無法解析的外部符號 "__declspec(dllimport) public: __cdecl UWidget::FFieldNotificationClassDescriptor::FFieldNotificationClassDescriptor(void)" (__imp_??0FFieldNotificationClassDescriptor@UWidget@@QEAA@XZ),函數 "public: virtual struct UE::FieldNotification::IClassDescriptor const & __cdecl UWidget::GetFieldNotificationDescriptor(void)const " (?GetFieldNotificationDescriptor@UWidget@@UEBAAEBUIClassDescriptor@FieldNotification@UE@@XZ) 中引用了該符號
1>Module.MultiplayerSessions.cpp.obj : error LNK2019: 無法解析的外部符號 "__declspec(dllimport) public: virtual __cdecl UWidget::FFieldNotificationClassDescriptor::~FFieldNotificationClassDescriptor(void)" (__imp_??1FFieldNotificationClassDescriptor@UWidget@@UEAA@XZ),函數 "void __cdecl `public: virtual struct UE::FieldNotification::IClassDescriptor const & __cdecl UWidget::GetFieldNotificationDescriptor(void)const '::`2'::`dynamic atexit destructor for 'Instance''(void)" (??__FInstance@?1??GetFieldNotificationDescriptor@UWidget@@UEBAAEBUIClassDescriptor@FieldNotification@UE@@XZ@YAXXZ) 中引用了該符號
1>Module.MultiplayerSessions.cpp.obj : error LNK2019: 無法解析的外部符號 "__declspec(dllimport) private: static class UClass * __cdecl UUserWidget::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@UUserWidget@@CAPEAVUClass@@XZ),函數 "public: static class UClass * __cdecl UUserWidget::StaticClass(void)" (?StaticClass@UUserWidget@@SAPEAVUClass@@XZ) 中引用了該符號
1> 已定義且可能匹配的符號上的提示:
無法解析的外部符號,別看報錯了這么多,先看他比比的什么
首先UWidget這個不認識,其次UUserWidget這個類也不認識
比比半天,是庫缺失了,鏈接一下就好了,直接搜索Widget,在解決方案下
沒搜到,沒關系,可能這個關鍵字就不是頭文件命名,在搜索UserWidget
直接將UMG添加到build.cs下即可
其實就是VS下的篩選器的名字
生成成功
設置UI界面配置
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Menu.generated.h"/*** */
UCLASS()
class MULTIPLAYERSESSIONS_API UMenu : public UUserWidget
{GENERATED_BODY()public:UFUNCTION(BlueprintCallable)void MenuSetup(int32 _NumberOfPublicConnections, FString _TypeOfMatch, FString _LobbyPath);public:/* 最大連接數 */UPROPERTY(BlueprintReadWrite)int32 NumberOfPublicConnections;UPROPERTY(BlueprintReadWrite)FString MatchType;/* 大廳路徑 */UPROPERTY(BlueprintReadWrite)FString LobbyPath;
};
#include "Menu.h"void UMenu::MenuSetup(int32 _NumberOfPublicConnections, FString _TypeOfMatch, FString _LobbyPath)
{LobbyPath = FString::Printf(TEXT("%s?listen"), *_LobbyPath);NumberOfPublicConnections = _NumberOfPublicConnections;MatchType = _TypeOfMatch;AddToViewport();SetVisibility(ESlateVisibility::Visible);bIsFocusable = true; // 允許接收輸入事件UWorld* World = GetWorld();if (World){APlayerController* PlayerController = World->GetFirstPlayerController();if (PlayerController){FInputModeUIOnly InputMode;InputMode.SetWidgetToFocus(TakeWidget()); // 聚焦當前控件InputMode.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock); // 不限制鼠標PlayerController->SetInputMode(InputMode); // 切換為純UI輸入模式PlayerController->bShowMouseCursor = true; // 顯示鼠標光標}}
}
創建控件藍圖
以創建的C++Menu類為父類創建