- 點擊C++類文件夾,在右邊的區(qū)域點擊鼠標右鍵,在彈出的菜單中選擇“新建C++類”
- 在彈出的菜單中選中“顯示所有類”,選擇GameInstanceSubsystem作為父類, 點擊“下一步”按鈕
- 輸入子類名稱“UVRVIUOnlineGameSubsystem”,選擇插件作為新類的目標模塊,點擊“公共”選擇器
-
打開C++工程,找到".Build.cs"文件,在“PublicDependencyModuleNames”下,添加"MultiPlayerPlugin"
1
2
3
4
5
6
7
8
9
10
11
public
?class
?LandMaster : ModuleRules
{
????
public
?LandMaster(ReadOnlyTargetRules Target) : base(Target)
????
{
????????
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
?
?????????
PublicDependencyModuleNames.AddRange(
new
?string[] {?
"Core"
,?
"CoreUObject"
,?
"Engine"
,?
"InputCore"
,?
"UMG"
,?
"Http"
,?
"Json"
,?
"JsonUtilities"
,?
"Sockets"
,?
"Networking"
,?
"OnlineSubsystem"
,?
"OnlineSubsystemUtils"
,?
"MultiPlayerPlugin"
?});
?
?????????
PrivateDependencyModuleNames.AddRange(
new
?string[] {?
"Slate"
,?
"SlateCore"
?});
????
}
}
-
設置“MultiPlayerPlugin.uplugin”文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
????
"FileVersion"
: 3,
????
"Version"
: 1,
????
"VersionName"
:?
"1.0"
,
????
"FriendlyName"
:?
"MultiPlayerPlugin"
,
????
"Description"
:?
"plugin for multi player "
,
????
"Category"
:?
"Other"
,
????
"CreatedBy"
:?
"VRVIU_Jacky"
,
????
"CreatedByURL"
:?
""
,
????
"DocsURL"
:?
""
,
????
"MarketplaceURL"
:?
""
,
????
"SupportURL"
:?
""
,
????
"EngineVersion"
:?
"4.26.0"
,
????
"CanContainContent"
:?
true
,
????
"Installed"
:?
true
,
????
"Modules"
: [
????????
{
????????????
"Name"
:?
"MultiPlayerPlugin"
,
????????????
"Type"
:?
"Runtime"
,
????????????
"LoadingPhase"
:?
"Default"
,
????????????
"WhitelistPlatforms"
: [
????????????????
"Win32"
,
????????????????
"Win64"
????????????
]
????????
}
????
]
}
-
設置插件“.Build.cs”文件,添加需要引用的模塊
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright Epic Games, Inc. All Rights Reserved.
?
?using
?UnrealBuildTool;
?
?public
?class
?MultiPlayerPlugin : ModuleRules
{
????
public
?MultiPlayerPlugin(ReadOnlyTargetRules Target) : base(Target)
????
{
????????
bEnableUndefinedIdentifierWarnings =?
false
;
????????
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
????????
//bUsePrecompiled = true;
????????
PublicIncludePaths.AddRange(
????????????
new
?string[] {
????????????????
"MultiPlayerPlugin/Public"
????????????
}
????????????
);
?????????????????
??????????
?????????
PrivateIncludePaths.AddRange(
????????????
new
?string[] {
?????????????????
?????????????
}
????????????
);
?????????????
??????????
?????????
PublicDependencyModuleNames.AddRange(
????????????
new
?string[]
????????????
{
????????????????
"Core"
????????????????
// ... add other public dependencies that you statically link with here ...
????????????
}
????????????
);
?????????????
??????????
?????????
PrivateDependencyModuleNames.AddRange(
????????????
new
?string[]
????????????
{
????????????????
"CoreUObject"
,
????????????????
"Engine"
,
????????????????
"Slate"
,
????????????????
"SlateCore"
,?
"UMG"
,?
"Http"
,?
"Json"
,?
"JsonUtilities"
,?
"Sockets"
,?
"Networking"
????????????????
// ... add private dependencies that you statically link with here ...?
????????????
}
????????????
);
?????????
??????????
?????????
DynamicallyLoadedModuleNames.AddRange(
????????????
new
?string[]
????????????
{
????????????????
// ... add any modules that your module loads dynamically here ...
????????????
}
????????????
);
????
}
}
-
獲取本機IP地址
1
2
3
4
5
6
7
8
9
10
11
12
13
14
FString UVRVIUOnlineGameSubsystem::GetIpAddress(
bool
?bHasPort?
/*= true*/
)
{
????
FString IpAddr(
"NONE"
);
????
bool
?canBind =?
false
;
????
TSharedRef<FInternetAddr>LocalIp = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->GetLocalHostAddr(*GLog, canBind);
????
if
?(LocalIp->IsValid())
????
{
????????
IpAddr = LocalIp->ToString(bHasPort);
????
}
????
return
?IpAddr;
}
-
創(chuàng)建會話
1
2
3
FString cmd =?
"open "
?+ Map +?
"?listen"
;
UGameplayStatics::GetPlayerController(GetWorld(), 0)->ConsoleCommand(cmd);
文章來源:http://www.zghlxwxcb.cn/news/detail-858818.htmlm_ServerAddress = ServerAddress.Len() == 0 ? GetIpAddress(
false
):ServerAddress;
-
加入會話
1
2
3
4
5
UE_LOG(LogTemp, Warning, TEXT(
"UVRVIUOnlineGameSubsystem::ConnectServer %s"
), *IntranetIP);
FString cmd =?
"open "
?+ IntranetIP;
UGameplayStatics::GetPlayerController(GetWorld(), 0)->ConsoleCommand(cmd);
m_ServerAddress = IntranetIP;
文章來源地址http://www.zghlxwxcb.cn/news/detail-858818.html
到了這里,關于Unreal Engine添加UGameInstanceSubsystem子類的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!