[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,25 @@
using FishNet.Documenting;
using FishNet.Managing;
using FishNet.Object;
namespace FishNet.Utility.Extension
{
[APIExclude]
public static class NetworksFN
{
/// <summary>
/// Returns if logic could have potentially called already on server side, and is calling a second time for clientHost side.
/// </summary>
public static bool DoubleLogic(this NetworkObject nob, bool asServer) => !asServer && nob.NetworkManager.IsServerStarted;
/// <summary>
/// Returns if logic could have potentially called already on server side, and is calling a second time for clientHost side.
/// </summary>
public static bool DoubleLogic(this NetworkManager manager, bool asServer) => !asServer && manager.IsServerStarted;
/// <summary>
/// Returns if logic could have potentially called already on server side, and is calling a second time for clientHost side.
/// </summary>
public static bool DoubleLogic(this NetworkBehaviour nb, bool asServer) => !asServer && nb.NetworkManager.IsServerStarted;
}
}
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: cbd3adaae5d34a14ab9cf68726b3bd3e
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/Utility/Extension/Networks.cs
uploadId: 866910
@@ -0,0 +1,90 @@
using FishNet.Managing;
using FishNet.Object;
using GameKit.Dependencies.Utilities;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace FishNet.Utility.Extension
{
public static class Scenes
{
/// <summary>
/// Gets all NetworkObjects in a scene.
/// </summary>
/// <param name = "s">Scene to get objects in.</param>
/// <param name = "firstOnly">True to only return the first NetworkObject within an object chain. False will return nested NetworkObjects.</param>
/// <returns></returns>
public static void GetSceneNetworkObjects(Scene s, bool firstOnly, bool errorOnDuplicates, bool ignoreUnsetSceneIds, ref List<NetworkObject> result)
{
List<NetworkObject> nobCacheA = CollectionCaches<NetworkObject>.RetrieveList();
List<NetworkObject> nobCacheB = CollectionCaches<NetworkObject>.RetrieveList();
List<GameObject> gameObjectCache = CollectionCaches<GameObject>.RetrieveList();
Dictionary<ulong, NetworkObject> sceneIds = CollectionCaches<ulong, NetworkObject>.RetrieveDictionary();
// Iterate all root objects for the scene.
s.GetRootGameObjects(gameObjectCache);
foreach (GameObject go in gameObjectCache)
{
// Get NetworkObjects within children of each root.
go.GetComponentsInChildren(true, nobCacheA);
// If network objects are found.
if (nobCacheA.Count > 0)
{
// Add only the first networkobject
if (firstOnly)
{
/* The easiest way to see if a nob is nested is to
* get nobs in parent and if the count is greater than 1, then
* it is nested. The technique used here isn't exactly fast but
* it will only occur during scene loads, so I'm trading off speed
* for effort and readability. */
foreach (NetworkObject nob in nobCacheA)
{
if (ignoreUnsetSceneIds && !nob.IsSceneObject)
continue;
nob.GetComponentsInParent(true, nobCacheB);
// No extra nobs, only this one.
if (nobCacheB.Count == 1 && !TryDisplayDuplicateError(nob))
result.Add(nob);
}
}
// Not first only, add them all.
else
{
foreach (NetworkObject item in nobCacheA)
{
if (ignoreUnsetSceneIds && !item.IsSceneObject)
continue;
if (!TryDisplayDuplicateError(item))
result.Add(item);
}
}
}
}
CollectionCaches<ulong, NetworkObject>.Store(sceneIds);
bool TryDisplayDuplicateError(NetworkObject nob)
{
if (!errorOnDuplicates)
return false;
ulong id = nob.SceneId;
// There is a duplicate.
if (sceneIds.TryGetValue(id, out NetworkObject originalNob))
{
string err = $"Object {nob.name} and {originalNob.name} in scene {nob.gameObject.scene.name} have the same sceneId of {id}. This will result in spawning errors. Exit play mode and use the Fish-Networking menu to reserialize sceneIds for scene {nob.gameObject.scene.name}.";
NetworkManagerExtensions.LogError(err);
return true;
}
else
{
sceneIds[id] = nob;
return false;
}
}
}
}
}
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: a02f3d03f737e304e9854278f4e9211d
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/Utility/Extension/Scenes.cs
uploadId: 866910
@@ -0,0 +1,194 @@
using System;
using FishNet.Documenting;
using FishNet.Object;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace FishNet.Utility.Extension
{
[APIExclude]
public static class TransformFN
{
/// <summary>
/// Sets values of TransformProperties to a transforms world properties.
/// </summary>
public static TransformProperties GetWorldProperties(this Transform t)
{
TransformProperties tp = new(t.position, t.rotation, t.localScale);
return tp;
}
/// <summary>
/// Sets values of TransformProperties to a transforms world properties.
/// </summary>
public static TransformProperties GetWorldProperties(this Transform t, TransformProperties offset)
{
TransformProperties tp = new(t.position, t.rotation, t.localScale);
tp.Add(offset);
return tp;
}
/// <summary>
/// Sets values of TransformProperties to a transforms world properties.
/// </summary>
public static TransformPropertiesCls GetWorldPropertiesCls(this Transform t)
{
TransformPropertiesCls tp = new(t.position, t.rotation, t.localScale);
return tp;
}
/// <summary>
/// Sets values of TransformProperties to a transforms world properties.
/// </summary>
public static TransformProperties GetLocalProperties(this Transform t)
{
TransformProperties tp = new(t.localPosition, t.localRotation, t.localScale);
return tp;
}
/// <summary>
/// Sets values of TransformProperties to a transforms world properties.
/// </summary>
public static TransformPropertiesCls GetLocalPropertiesCls(this Transform t)
{
TransformPropertiesCls tp = new(t.localPosition, t.localRotation, t.localScale);
return tp;
}
/// <summary>
/// Sets values of TransformPropertiesCls to a transforms world properties.
/// </summary>
[Obsolete("Use TransformPropertiesExtensions.SetWorldProperties.")]
public static void SetWorldProperties(this TransformPropertiesCls tp, Transform t) => TransformPropertiesExtensions.SetWorldProperties(tp, t);
/// <summary>
/// Gets the offset values by subtracting this from target.
/// </summary>
/// <param name = "pos">Position offset result.</param>
/// <param name = "rot">Rotation offset result.</param>
public static void SetTransformOffsets(this Transform t, Transform target, ref Vector3 pos, ref Quaternion rot)
{
if (target == null)
return;
pos = target.position - t.position;
rot = target.rotation * Quaternion.Inverse(t.rotation);
}
/// <summary>
/// Gets the offset values by subtracting this from target.
/// </summary>
/// <param name = "zeroScale">True to set scale to Vector3.zero.</param>
public static TransformProperties GetTransformOffsets(this Transform t, Transform target)
{
if (target == null)
return default;
return new(target.position - t.position, target.rotation * Quaternion.Inverse(t.rotation), target.localScale - t.localScale);
}
/// <summary>
/// Sets a transform to local properties.
/// </summary>
public static void SetLocalProperties(this Transform t, TransformPropertiesCls tp)
{
t.localPosition = tp.Position;
t.localRotation = tp.Rotation;
t.localScale = tp.LocalScale;
}
/// <summary>
/// Sets a transform to local properties.
/// </summary>
public static void SetLocalProperties(this Transform t, TransformProperties tp)
{
t.localPosition = tp.Position;
t.localRotation = tp.Rotation;
t.localScale = tp.Scale;
}
/// <summary>
/// Sets a transform to world properties.
/// </summary>
public static void SetWorldProperties(this Transform t, TransformPropertiesCls tp)
{
t.position = tp.Position;
t.rotation = tp.Rotation;
t.localScale = tp.LocalScale;
}
/// <summary>
/// Sets a transform to world properties.
/// </summary>
public static void SetWorldProperties(this Transform t, TransformProperties tp)
{
t.position = tp.Position;
t.rotation = tp.Rotation;
t.localScale = tp.Scale;
}
/// <summary>
/// Sets local position and rotation for a transform.
/// </summary>
public static void SetLocalPositionAndRotation(this Transform t, Vector3 pos, Quaternion rot)
{
t.localPosition = pos;
t.localRotation = rot;
}
/// <summary>
/// Sets local position, rotation, and scale for a transform.
/// </summary>
public static void SetLocalPositionRotationAndScale(this Transform t, Vector3 pos, Quaternion rot, Vector3 scale)
{
t.localPosition = pos;
t.localRotation = rot;
t.localScale = scale;
}
/// <summary>
/// Sets local position, rotation, and scale using nullables for a transform. If a value is null then that property is skipped.
/// </summary>
public static void SetLocalPositionRotationAndScale(this Transform t, Vector3? nullablePos, Quaternion? nullableRot, Vector3? nullableScale)
{
if (nullablePos.HasValue)
t.localPosition = nullablePos.Value;
if (nullableRot.HasValue)
t.localRotation = nullableRot.Value;
if (nullableScale.HasValue)
t.localScale = nullableScale.Value;
}
/// <summary>
/// Sets world position, rotation, and scale using nullables for a transform. If a value is null then that property is skipped.
/// </summary>
public static void SetWorldPositionRotationAndScale(this Transform t, Vector3? nullablePos, Quaternion? nullableRot, Vector3? nullableScale)
{
if (nullablePos.HasValue)
t.position = nullablePos.Value;
if (nullableRot.HasValue)
t.rotation = nullableRot.Value;
if (nullableScale.HasValue)
t.localScale = nullableScale.Value;
}
/// <summary>
/// Oututs properties to use for a transform. When a nullable property has value that value is used, otherwise the transforms current property is used.
/// </summary>
public static void OutLocalPropertyValues(this Transform t, Vector3? nullablePos, Quaternion? nullableRot, Vector3? nullableScale, out Vector3 pos, out Quaternion rot, out Vector3 scale)
{
pos = nullablePos == null ? t.localPosition : nullablePos.Value;
rot = nullableRot == null ? t.localRotation : nullableRot.Value;
scale = nullableScale == null ? t.localScale : nullableScale.Value;
}
/// <summary>
/// Oututs properties to use for a transform. When a nullable property has value that value is used, otherwise the transforms current property is used.
/// </summary>
public static void OutWorldPropertyValues(this Transform t, Vector3? nullablePos, Quaternion? nullableRot, Vector3? nullableScale, out Vector3 pos, out Quaternion rot, out Vector3 scale)
{
pos = nullablePos == null ? t.position : nullablePos.Value;
rot = nullableRot == null ? t.rotation : nullableRot.Value;
scale = nullableScale == null ? t.localScale : nullableScale.Value;
}
}
}
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 3d311fc1bf09b9e4fbc5a17a9c50ab0d
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/Utility/Extension/Transforms.cs
uploadId: 866910