[Add] FishNet

This commit is contained in:
2026-03-30 20:11:57 +07:00
parent ee793a3361
commit c22c08753a
1797 changed files with 197950 additions and 1 deletions
@@ -0,0 +1,64 @@
#if UNITY_EDITOR
using FishNet.Configuring;
using System;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using File = System.IO.File;
namespace FishNet.Editing.NewNetworkBehaviourScript
{
internal sealed class CreateNewNetworkBehaviour : MonoBehaviour
{
private const string TEMPLATE_CLASS_NAME = "NewNetworkBehaviourTemplate";
public static string TemplatePath => Path.Combine(Configuration.Configurations.CreateNewNetworkBehaviour.templateDirectoryPath, $"{TEMPLATE_CLASS_NAME}.txt");
[MenuItem("Assets/Create/FishNet/NetworkBehaviour Script", false, -220)]
private static void CreateNewAsset()
{
try
{
EnsureTemplateExists();
ProjectWindowUtil.CreateScriptAssetFromTemplateFile(TemplatePath, $"{TEMPLATE_CLASS_NAME}.cs");
}
catch (Exception e)
{
Debug.LogError($"An exception occurred while trying to copy the NetworkBehaviour template. {e.Message}");
}
}
public static void EnsureTemplateExists()
{
try
{
if (!File.Exists(TemplatePath))
{
string fileContent = GetNewTemplateText();
File.WriteAllText(TemplatePath, fileContent);
}
}
catch (Exception e)
{
Debug.LogError($"An exception occurred while trying to create the NetworkBehaviour template. {e.Message}");
}
}
private static string GetNewTemplateText()
{
StringBuilder sb = new();
sb.AppendLine("using UnityEngine;");
sb.AppendLine("using FishNet.Object;");
sb.AppendLine();
sb.AppendLine("public class NewNetworkBehaviourTemplate : NetworkBehaviour");
sb.AppendLine("{");
sb.AppendLine(" private void Awake() { }");
sb.AppendLine();
sb.AppendLine(" private void Update() { }");
sb.AppendLine("}");
return sb.ToString();
}
}
}
#endif
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: bd7764ab379082b4f9889c73b9133da9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 207815
packageName: 'FishNet: Networking Evolved'
packageVersion: 4.6.22R
assetPath: Assets/FishNet/Runtime/Editor/NewNetworkBehaviour/CreateNewNetworkBehaviour.cs
uploadId: 866910
@@ -0,0 +1,102 @@
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using UnitySettingsProviderAttribute = UnityEditor.SettingsProviderAttribute;
using UnitySettingsProvider = UnityEditor.SettingsProvider;
using FishNet.Configuring;
using System.IO;
using System;
using System.Text.RegularExpressions;
namespace FishNet.Editing.NewNetworkBehaviourScript
{
internal static class SettingsProvider
{
private static CreateNewNetworkBehaviourConfigurations _settings;
private static GUIContent _folderIcon;
private static readonly Regex SlashRegex = new(@"[\\// ]");
[UnitySettingsProvider]
private static UnitySettingsProvider Create()
{
return new("Project/Fish-Networking/New NetworkBehaviour Template", SettingsScope.Project)
{
label = "New NetworkBehaviour Template",
guiHandler = OnGUI,
keywords = new string[]
{
"Fish",
"Networking",
"CreateNewNetworkBehaviour",
"Template"
}
};
}
private static void OnGUI(string searchContext)
{
if (_settings == null)
_settings = Configuration.Configurations.CreateNewNetworkBehaviour;
if (_folderIcon == null)
_folderIcon = EditorGUIUtility.IconContent("d_FolderOpened Icon");
EditorGUI.BeginChangeCheck();
GUILayoutOption iconWidthConstraint = GUILayout.MaxWidth(32.0f);
GUILayoutOption iconHeightConstraint = GUILayout.MaxHeight(EditorGUIUtility.singleLineHeight);
if (GUILayout.Button("Edit template"))
{
CreateNewNetworkBehaviour.EnsureTemplateExists();
try
{
System.Diagnostics.Process.Start(CreateNewNetworkBehaviour.TemplatePath);
}
catch (Exception e)
{
Debug.LogError($"An issue occurred while trying to launch the NetworkBehaviour template. {e.Message}");
}
}
GUILayout.Space(20);
EditorGUILayout.BeginHorizontal();
GUILayout.Label("Template directory: ", GUILayout.MaxWidth(150));
string newDirectoryPath;
newDirectoryPath = EditorGUILayout.DelayedTextField(_settings.templateDirectoryPath, GUILayout.MaxWidth(600));
if (newDirectoryPath.StartsWith("Assets") && Directory.Exists(newDirectoryPath))
{
_settings.templateDirectoryPath = newDirectoryPath;
}
else
{
EditorWindow.focusedWindow.ShowNotification(new($"Directory must be inside the Assets folder."), 2);
}
if (GUILayout.Button(_folderIcon, iconHeightConstraint, iconWidthConstraint))
{
newDirectoryPath = EditorUtility.OpenFolderPanel("Select template directory", _settings.templateDirectoryPath, "");
}
if (newDirectoryPath.StartsWith(Application.dataPath, StringComparison.OrdinalIgnoreCase))
{
newDirectoryPath = SlashRegex.Replace(newDirectoryPath.Remove(0, Path.GetDirectoryName(Application.dataPath).Length + 1), Path.DirectorySeparatorChar.ToString());
_settings.templateDirectoryPath = newDirectoryPath;
}
else if (!newDirectoryPath.StartsWith(Application.dataPath, StringComparison.OrdinalIgnoreCase) && !newDirectoryPath.StartsWith("Assets"))
{
EditorWindow.focusedWindow.ShowNotification(new($"Directory must be inside the Assets folder."), 2);
}
EditorGUILayout.EndHorizontal();
// EditorGUILayout.HelpBox("By default MonoBehaviour script template will be copied", MessageType.Info);
if (EditorGUI.EndChangeCheck())
Configuration.Configurations.Write(true);
}
}
}
#endif
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: ec84d908960b66b448f94582e747689b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 207815
packageName: 'FishNet: Networking Evolved'
packageVersion: 4.6.22R
assetPath: Assets/FishNet/Runtime/Editor/NewNetworkBehaviour/SettingsProvider.cs
uploadId: 866910