Skip to content

Commit 96def2c

Browse files
authored
feat: Allow configuring the location of the default network prefabs list. [MTT-6209] (#2544)
1 parent 7db429f commit 96def2c

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1010

1111
### Added
1212

13+
- The location of the automatically-created default network prefab list can now be configured (#2544)
1314
- Added: Message size limits (max single message and max fragmented message) can now be set using NetworkManager.SetMaxSingleMessageSize() and NetworkManager.SetMaxFragmentedMessageSize() for transports that don't work with the default values (#2530)
1415
- Added `NetworkObject.SpawnWithObservers` property (default is true) that when set to false will spawn a `NetworkObject` with no observers and will not be spawned on any client until `NetworkObject.NetworkShow` is invoked. (#2568)
1516

com.unity.netcode.gameobjects/Editor/Configuration/NetcodeForGameObjectsSettings.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ internal static void SetAutoAddNetworkObjectSetting(bool autoAddSetting)
4444
[FilePath("ProjectSettings/NetcodeForGameObjects.settings", FilePathAttribute.Location.ProjectFolder)]
4545
internal class NetcodeForGameObjectsProjectSettings : ScriptableSingleton<NetcodeForGameObjectsProjectSettings>
4646
{
47+
internal static readonly string DefaultNetworkPrefabsPath = "Assets/DefaultNetworkPrefabs.asset";
48+
[SerializeField] public string NetworkPrefabsPath = DefaultNetworkPrefabsPath;
49+
public string TempNetworkPrefabsPath;
50+
51+
private void OnEnable()
52+
{
53+
if (NetworkPrefabsPath == "")
54+
{
55+
NetworkPrefabsPath = DefaultNetworkPrefabsPath;
56+
}
57+
TempNetworkPrefabsPath = NetworkPrefabsPath;
58+
}
59+
4760
[SerializeField]
4861
[FormerlySerializedAs("GenerateDefaultNetworkPrefabs")]
4962
private byte m_GenerateDefaultNetworkPrefabs;

com.unity.netcode.gameobjects/Editor/Configuration/NetcodeSettingsProvider.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
using System.Collections.Generic;
2+
using System.IO;
13
using UnityEditor;
24
using UnityEngine;
5+
using Directory = UnityEngine.Windows.Directory;
6+
using File = UnityEngine.Windows.File;
37

48
namespace Unity.Netcode.Editor.Configuration
59
{
@@ -20,11 +24,60 @@ public static SettingsProvider CreateNetcodeSettingsProvider()
2024
label = "Netcode for GameObjects",
2125
keywords = new[] { "netcode", "editor" },
2226
guiHandler = OnGuiHandler,
27+
deactivateHandler = OnDeactivate
2328
};
2429

2530
return provider;
2631
}
2732

33+
private static void OnDeactivate()
34+
{
35+
var settings = NetcodeForGameObjectsProjectSettings.instance;
36+
if (settings.TempNetworkPrefabsPath != settings.NetworkPrefabsPath)
37+
{
38+
var newPath = settings.TempNetworkPrefabsPath;
39+
if (newPath == "")
40+
{
41+
newPath = NetcodeForGameObjectsProjectSettings.DefaultNetworkPrefabsPath;
42+
settings.TempNetworkPrefabsPath = newPath;
43+
}
44+
var oldPath = settings.NetworkPrefabsPath;
45+
settings.NetworkPrefabsPath = settings.TempNetworkPrefabsPath;
46+
var dirName = Path.GetDirectoryName(newPath);
47+
if (!Directory.Exists(dirName))
48+
{
49+
var dirs = dirName.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });
50+
var dirsQueue = new Queue<string>(dirs);
51+
var parent = dirsQueue.Dequeue();
52+
while (dirsQueue.Count != 0)
53+
{
54+
var child = dirsQueue.Dequeue();
55+
var together = Path.Combine(parent, child);
56+
if (!Directory.Exists(together))
57+
{
58+
AssetDatabase.CreateFolder(parent, child);
59+
}
60+
61+
parent = together;
62+
}
63+
}
64+
65+
if (Directory.Exists(dirName))
66+
{
67+
if (File.Exists(oldPath))
68+
{
69+
AssetDatabase.MoveAsset(oldPath, newPath);
70+
if (File.Exists(oldPath))
71+
{
72+
File.Delete(oldPath);
73+
}
74+
AssetDatabase.Refresh();
75+
}
76+
}
77+
settings.SaveSettings();
78+
}
79+
}
80+
2881

2982
internal static NetcodeSettingsLabel NetworkObjectsSectionLabel;
3083
internal static NetcodeSettingsToggle AutoAddNetworkObjectToggle;
@@ -70,6 +123,7 @@ private static void OnGuiHandler(string obj)
70123
var multiplayerToolsTipStatus = NetcodeForGameObjectsEditorSettings.GetNetcodeInstallMultiplayerToolTips() == 0;
71124
var settings = NetcodeForGameObjectsProjectSettings.instance;
72125
var generateDefaultPrefabs = settings.GenerateDefaultNetworkPrefabs;
126+
var networkPrefabsPath = settings.TempNetworkPrefabsPath;
73127

74128
EditorGUI.BeginChangeCheck();
75129

@@ -97,6 +151,7 @@ private static void OnGuiHandler(string obj)
97151
{
98152
GUILayout.BeginVertical("Box");
99153
const string generateNetworkPrefabsString = "Generate Default Network Prefabs List";
154+
const string networkPrefabsLocationString = "Default Network Prefabs List path";
100155

101156
if (s_MaxLabelWidth == 0)
102157
{
@@ -114,6 +169,14 @@ private static void OnGuiHandler(string obj)
114169
"to date with all NetworkObject prefabs."),
115170
generateDefaultPrefabs,
116171
GUILayout.Width(s_MaxLabelWidth + 20));
172+
173+
GUI.SetNextControlName("Location");
174+
networkPrefabsPath = EditorGUILayout.TextField(
175+
new GUIContent(
176+
networkPrefabsLocationString,
177+
"The path to the asset the default NetworkPrefabList object should be stored in."),
178+
networkPrefabsPath,
179+
GUILayout.Width(s_MaxLabelWidth + 270));
117180
GUILayout.EndVertical();
118181
}
119182
EditorGUILayout.EndFoldoutHeaderGroup();
@@ -123,6 +186,7 @@ private static void OnGuiHandler(string obj)
123186
NetcodeForGameObjectsEditorSettings.SetAutoAddNetworkObjectSetting(autoAddNetworkObjectSetting);
124187
NetcodeForGameObjectsEditorSettings.SetNetcodeInstallMultiplayerToolTips(multiplayerToolsTipStatus ? 0 : 1);
125188
settings.GenerateDefaultNetworkPrefabs = generateDefaultPrefabs;
189+
settings.TempNetworkPrefabsPath = networkPrefabsPath;
126190
settings.SaveSettings();
127191
}
128192
}

com.unity.netcode.gameobjects/Editor/Configuration/NetworkPrefabProcessor.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ namespace Unity.Netcode.Editor.Configuration
99
/// </summary>
1010
public class NetworkPrefabProcessor : AssetPostprocessor
1111
{
12-
private static string s_DefaultNetworkPrefabsPath = "Assets/DefaultNetworkPrefabs.asset";
1312
public static string DefaultNetworkPrefabsPath
1413
{
1514
get
1615
{
17-
return s_DefaultNetworkPrefabsPath;
16+
return NetcodeForGameObjectsProjectSettings.instance.NetworkPrefabsPath;
1817
}
1918
internal set
2019
{
21-
s_DefaultNetworkPrefabsPath = value;
20+
NetcodeForGameObjectsProjectSettings.instance.NetworkPrefabsPath = value;
2221
// Force a recache of the prefab list
2322
s_PrefabsList = null;
2423
}

0 commit comments

Comments
 (0)