Skip to content

feat: Allow configuring the location of the default network prefabs list. [MTT-6209] #2544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Added

- The location of the automatically-created default network prefab list can now be configured (#2544)
- 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)
- 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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ internal static void SetAutoAddNetworkObjectSetting(bool autoAddSetting)
[FilePath("ProjectSettings/NetcodeForGameObjects.settings", FilePathAttribute.Location.ProjectFolder)]
internal class NetcodeForGameObjectsProjectSettings : ScriptableSingleton<NetcodeForGameObjectsProjectSettings>
{
internal static readonly string DefaultNetworkPrefabsPath = "Assets/DefaultNetworkPrefabs.asset";
[SerializeField] public string NetworkPrefabsPath = DefaultNetworkPrefabsPath;
public string TempNetworkPrefabsPath;

private void OnEnable()
{
if (NetworkPrefabsPath == "")
{
NetworkPrefabsPath = DefaultNetworkPrefabsPath;
}
TempNetworkPrefabsPath = NetworkPrefabsPath;
}

[SerializeField]
[FormerlySerializedAs("GenerateDefaultNetworkPrefabs")]
private byte m_GenerateDefaultNetworkPrefabs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using Directory = UnityEngine.Windows.Directory;
using File = UnityEngine.Windows.File;

namespace Unity.Netcode.Editor.Configuration
{
Expand All @@ -20,11 +24,60 @@ public static SettingsProvider CreateNetcodeSettingsProvider()
label = "Netcode for GameObjects",
keywords = new[] { "netcode", "editor" },
guiHandler = OnGuiHandler,
deactivateHandler = OnDeactivate
};

return provider;
}

private static void OnDeactivate()
{
var settings = NetcodeForGameObjectsProjectSettings.instance;
if (settings.TempNetworkPrefabsPath != settings.NetworkPrefabsPath)
{
var newPath = settings.TempNetworkPrefabsPath;
if (newPath == "")
{
newPath = NetcodeForGameObjectsProjectSettings.DefaultNetworkPrefabsPath;
settings.TempNetworkPrefabsPath = newPath;
}
var oldPath = settings.NetworkPrefabsPath;
settings.NetworkPrefabsPath = settings.TempNetworkPrefabsPath;
var dirName = Path.GetDirectoryName(newPath);
if (!Directory.Exists(dirName))
{
var dirs = dirName.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });
var dirsQueue = new Queue<string>(dirs);
var parent = dirsQueue.Dequeue();
while (dirsQueue.Count != 0)
{
var child = dirsQueue.Dequeue();
var together = Path.Combine(parent, child);
if (!Directory.Exists(together))
{
AssetDatabase.CreateFolder(parent, child);
}

parent = together;
}
}

if (Directory.Exists(dirName))
{
if (File.Exists(oldPath))
{
AssetDatabase.MoveAsset(oldPath, newPath);
if (File.Exists(oldPath))
{
File.Delete(oldPath);
}
AssetDatabase.Refresh();
}
}
settings.SaveSettings();
}
}


internal static NetcodeSettingsLabel NetworkObjectsSectionLabel;
internal static NetcodeSettingsToggle AutoAddNetworkObjectToggle;
Expand Down Expand Up @@ -70,6 +123,7 @@ private static void OnGuiHandler(string obj)
var multiplayerToolsTipStatus = NetcodeForGameObjectsEditorSettings.GetNetcodeInstallMultiplayerToolTips() == 0;
var settings = NetcodeForGameObjectsProjectSettings.instance;
var generateDefaultPrefabs = settings.GenerateDefaultNetworkPrefabs;
var networkPrefabsPath = settings.TempNetworkPrefabsPath;

EditorGUI.BeginChangeCheck();

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

if (s_MaxLabelWidth == 0)
{
Expand All @@ -114,6 +169,14 @@ private static void OnGuiHandler(string obj)
"to date with all NetworkObject prefabs."),
generateDefaultPrefabs,
GUILayout.Width(s_MaxLabelWidth + 20));

GUI.SetNextControlName("Location");
networkPrefabsPath = EditorGUILayout.TextField(
new GUIContent(
networkPrefabsLocationString,
"The path to the asset the default NetworkPrefabList object should be stored in."),
networkPrefabsPath,
GUILayout.Width(s_MaxLabelWidth + 270));
GUILayout.EndVertical();
}
EditorGUILayout.EndFoldoutHeaderGroup();
Expand All @@ -123,6 +186,7 @@ private static void OnGuiHandler(string obj)
NetcodeForGameObjectsEditorSettings.SetAutoAddNetworkObjectSetting(autoAddNetworkObjectSetting);
NetcodeForGameObjectsEditorSettings.SetNetcodeInstallMultiplayerToolTips(multiplayerToolsTipStatus ? 0 : 1);
settings.GenerateDefaultNetworkPrefabs = generateDefaultPrefabs;
settings.TempNetworkPrefabsPath = networkPrefabsPath;
settings.SaveSettings();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ namespace Unity.Netcode.Editor.Configuration
/// </summary>
public class NetworkPrefabProcessor : AssetPostprocessor
{
private static string s_DefaultNetworkPrefabsPath = "Assets/DefaultNetworkPrefabs.asset";
public static string DefaultNetworkPrefabsPath
{
get
{
return s_DefaultNetworkPrefabsPath;
return NetcodeForGameObjectsProjectSettings.instance.NetworkPrefabsPath;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😻

}
internal set
{
s_DefaultNetworkPrefabsPath = value;
NetcodeForGameObjectsProjectSettings.instance.NetworkPrefabsPath = value;
// Force a recache of the prefab list
s_PrefabsList = null;
}
Expand Down