diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index b4bc54480d..3458c1cb43 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -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) diff --git a/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeForGameObjectsSettings.cs b/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeForGameObjectsSettings.cs index a057d1e69f..110fe2fd09 100644 --- a/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeForGameObjectsSettings.cs +++ b/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeForGameObjectsSettings.cs @@ -44,6 +44,19 @@ internal static void SetAutoAddNetworkObjectSetting(bool autoAddSetting) [FilePath("ProjectSettings/NetcodeForGameObjects.settings", FilePathAttribute.Location.ProjectFolder)] internal class NetcodeForGameObjectsProjectSettings : ScriptableSingleton { + 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; diff --git a/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeSettingsProvider.cs b/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeSettingsProvider.cs index 08bbda7871..ce8023bd6b 100644 --- a/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeSettingsProvider.cs +++ b/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeSettingsProvider.cs @@ -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 { @@ -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(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; @@ -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(); @@ -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) { @@ -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(); @@ -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(); } } diff --git a/com.unity.netcode.gameobjects/Editor/Configuration/NetworkPrefabProcessor.cs b/com.unity.netcode.gameobjects/Editor/Configuration/NetworkPrefabProcessor.cs index 631cee1fae..879a8c3e75 100644 --- a/com.unity.netcode.gameobjects/Editor/Configuration/NetworkPrefabProcessor.cs +++ b/com.unity.netcode.gameobjects/Editor/Configuration/NetworkPrefabProcessor.cs @@ -9,16 +9,15 @@ namespace Unity.Netcode.Editor.Configuration /// public class NetworkPrefabProcessor : AssetPostprocessor { - private static string s_DefaultNetworkPrefabsPath = "Assets/DefaultNetworkPrefabs.asset"; public static string DefaultNetworkPrefabsPath { get { - return s_DefaultNetworkPrefabsPath; + return NetcodeForGameObjectsProjectSettings.instance.NetworkPrefabsPath; } internal set { - s_DefaultNetworkPrefabsPath = value; + NetcodeForGameObjectsProjectSettings.instance.NetworkPrefabsPath = value; // Force a recache of the prefab list s_PrefabsList = null; }