How about a NetworkBehaviourReference<T>?
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 25/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- csharp, unity
- 领域
- game-dev, networking
调研方向
首先检查 NetworkBehaviourReference、NetworkObjectReference 以及 issue 中描述的 RPC 参数处理。确定泛型 NetworkBehaviour 引用或直接类型化的 RPC 参数是否适合现有的序列化和 lookup 路径;完成的标准是强制使用类型化引用,并且无需当前的 cast 或 TryGet boilerplate 即可解析,同时添加适当的覆盖测试。
由索引模型根据 Issue 内容生成。
描述
It would be amazing if RPC arguments could just actually be any NetworkBehaviour derivative class instead of only NetworkBehaviourReference so that the compiler could enforce type on those RPC arguments instead of boilerplate cast / TryGet code.
Short of that, doing an RPC with NetworkBehaviourReference<MyNetworkBehaviourClass> at least gives me peace of mind that the compiler can catch me trying to pass the RPC a reference to a NetworkBehaviour that isn't a MyNetworkBehaviourClass.
I did it like this:
using System;
using System.Runtime.CompilerServices;
namespace Unity.Netcode
{
public struct NetworkBehaviourReference<T> : INetworkSerializable, IEquatable<NetworkBehaviourReference<T>>
where T : NetworkBehaviour
{
private NetworkObjectReference m_NetworkObjectReference;
private ushort m_NetworkBehaviourId;
public NetworkBehaviourReference(NetworkBehaviour networkBehaviour)
{
if (networkBehaviour == null)
{
m_NetworkObjectReference = new NetworkObjectReference((NetworkObject)null);
m_NetworkBehaviourId = 0;
return;
// throw new ArgumentNullException(nameof(networkBehaviour));
}
if (networkBehaviour.NetworkObject == null)
{
throw new ArgumentException($"Cannot create {nameof(NetworkBehaviourReference<T>)} from {nameof(NetworkBehaviour)} without a {nameof(NetworkObject)}.");
}
m_NetworkObjectReference = networkBehaviour.NetworkObject;
m_NetworkBehaviourId = networkBehaviour.NetworkBehaviourId;
}
public bool TryGet(out T networkBehaviourSubtype, NetworkManager networkManager = null)
{
networkBehaviourSubtype = GetInternal(this, null) as T;
return networkBehaviourSubtype != null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static NetworkBehaviour GetInternal(NetworkBehaviourReference<T> networkBehaviourRef, NetworkManager networkManager = null)
{
if (networkBehaviourRef.m_NetworkObjectReference.TryGet(out NetworkObject networkObject, networkManager))
{
return networkObject.GetNetworkBehaviourAtOrderIndex(networkBehaviourRef.m_NetworkBehaviourId);
}
return null;
}
public bool Equals(NetworkBehaviourReference<T> other)
{
return m_NetworkObjectReference.Equals(other.m_NetworkObjectReference) && m_NetworkBehaviourId == other.m_NetworkBehaviourId;
}
public override bool Equals(object obj)
{
return obj is NetworkBehaviourReference<T> other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
return (m_NetworkObjectReference.GetHashCode() * 397) ^ m_NetworkBehaviourId.GetHashCode();
}
}
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
m_NetworkObjectReference.NetworkSerialize(serializer);
serializer.SerializeValue(ref m_NetworkBehaviourId);
}
public static implicit operator T(NetworkBehaviourReference<T> networkBehaviourRef) => GetInternal(networkBehaviourRef) as T;
public static implicit operator NetworkBehaviourReference<T>(T networkBehaviourSubtype) => new NetworkBehaviourReference<T>(networkBehaviourSubtype);
}
}
- 主要语言
- C#
- 星标
- 2.3k
- 派生
- 461
- 平均合并
- 3 天 16 小时
- 30 天内合并 PR
- 20
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
Unity-Technologies/com.unity.netcode.gameobjects 的其他 Issue
-
stat:awaiting-response stat:imported type:bug
难度 4/5 3-5 天 新手友好度 68/100
Unity-Technologies/com.unity.netcode.gameobjects#4159 · 3 条评论 ·
-
stat:reply-needed type:support
难度 4/5 3-5 天 新手友好度 55/100
Unity-Technologies/com.unity.netcode.gameobjects#4095 · 10 条评论 ·
-
stat:awaiting-triage stat:Investigating type:bug
Unity-Technologies/com.unity.netcode.gameobjects#3912 · 5 条评论 · 已指派 1 人 ·
-
Tracking type:feature-2.x
难度 5/5 一周以上 新手友好度 35/100
Unity-Technologies/com.unity.netcode.gameobjects#3870 · 5 条评论 ·
-
Tracking type:feature
难度 4/5 3-5 天 新手友好度 48/100
Unity-Technologies/com.unity.netcode.gameobjects#3830 · 7 条评论 ·
查看 Unity-Technologies/com.unity.netcode.gameobjects 的全部 Issue
相似的 Issue
-
priority-0
难度 2/5 1-3 小时 新手友好度 68/100
-
难度 1/5 1 小时以内 新手友好度 78/100
StackExchange/StackExchange.Redis#3249 ·
-
难度 2/5 1-3 小时 新手友好度 68/100
-
type/automation type/tech-debt
难度 2/5 1-3 小时 新手友好度 78/100
-
bug
难度 2/5 1-3 小时 新手友好度 88/100