Add a single unified “OnNetworkStarted” event to NetworkManager
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 42/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- csharp, unity
- Lĩnh vực
- api, networking
Hướng nghiên cứu
Bắt đầu trong NetworkManager, đặc biệt là internal Initialize(bool server), và kiểm tra các luồng vòng đời hiện có OnServerStarted và OnClientConnectedCallback cùng các bài kiểm thử liên quan. Hoàn tất khi một sự kiện duy nhất được phát ra đúng một lần sau khi ngăn xếp mạng và transport đã sẵn sàng trong các chế độ server, client và host, với hành vi bao quát nhiều phiên bản NetworkManager và việc tải lại editor.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
The problem
There’s no single event that fires once when networking starts, regardless of mode
Currently you need to handle both OnServerStarted and OnClientConnectedCallback, which leads to redundant or missing calls depending on the mode (client/server)
Using per-connection callbacks for this is unreliable, and managing timing around the singleton or validation properties(e.g IsListening) adds unnecessary complexity
Unless i am missing something, to me this dont feels right
Current workflow
The current workaround is using coroutines or polling to wait until the singleton exists and the NetworkManager is listening.
This adds overhead and makes initialization logic fragile, even more for singleplayer sessions where the code may keep waiting forever as no connections are made
Example (based on forums suggestions):
void OnEnable()
{
StartCoroutine(SubscribeToNetworkManagerEvents());
}
IEnumerator SubscribeToNetworkManagerEvents()
{
yield return new WaitUntil(() => NetworkManager.Singleton); //This is fragile, and may add other problems, and no mention to cancelations
NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnectedCallback; //called on clients but unclear about standalone server
NetworkManager.Singleton.OnServerStarted += OnServerStarted; //never called on non host client
}
void OnDestroy()
{
if (NetworkManager.Singleton)
{
NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnectedCallback;
NetworkManager.Singleton.OnServerStarted -= OnServerStarted;
}
initialized = false;
}
void OnClientConnectedCallback(parameters)
{
RegisterMessageHandler();
}
void OnServerStarted(parameters)
{
RegisterMessageHandler();
}
bool initialized;
//Both on client connected callback and server started calls this other method
void RegisterMessageHandler()
{
if(initialized)
return;
//For example
NetworkManager.Singleton.CustomMessagingManager.RegisterNamedMessageHandler("a", B);
// In this case, Singleton can be null and CustomMessagingManager also can be null, thats why we need the yield
initialized = true;
}
These yields are required only because there is no dedicated lifecycle event indicating when the NetworkManager and its subsystems (like CustomMessagingManager) are fully initialized
As result, developers must manually simulate this missing initialization phase using coroutines or polling, which makes code more complex
Posible solution
Add new public static Action<NetworkManager> OnNetworkStarted event to NetworkManager
Triggered once when the network stack has been initialized and the transport is ready, regardless of whether it’s running as server, client, or host.
Passing the Network manager initialized (as after working on the last pull request, i saw on the tests part and other parts of code might exist more than one NetworkManager)
This would change the workflow to a much more convenient one like this
NetworkManager networkManager;
void OnEnable()
{
NetworkManager.OnNetworkStarted += OnNetworkStarted;
}
void OnNetworkStarted(NetworkManager networkManager)
{
this.networkManager = networkManager;
networkManager.CustomMessagingManager.RegisterNamedMessageHandler("a", B);
}
void OnDestroy()
{
if (networkManager)
networkManager.CustomMessagingManager.UnregisterNamedMessageHandler("a");
}
[!NOTE]
WhileNetworkBehaviour.OnNetworkSpawncan be used for components, this event would support use cases where no scene objects are involved (e.g ScriptableObjects or systems initialized outside scene contexts). It provides a clean, unified hook for all modes.This change would mostly take 2 lines
public static Action<NetworkManager> OnNetworkStartedon NetworkManager classOnNetworkStarter?.Invoke(This);at the very end of NetworkManager'sinternal void Initialize(bool server)methodIt only may cause problems on having it static with the editor reloading feature, but that could be arranged cleaning the event with the
[RuntimeInitializeOnLoadAttribute]
- Ngôn ngữ chính
- C#
- Star
- 2.3k
- Fork
- 461
- Merge trung bình
- 3 ngày 16 giờ
- Pull request đã merge (30 ngày)
- 20
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của Unity-Technologies/com.unity.netcode.gameobjects
-
stat:import type:bug
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 68/100
-
stat:reply-needed type:support
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
Unity-Technologies/com.unity.netcode.gameobjects#4095 · 10 bình luận ·
-
stat:awaiting-triage stat:Investigating type:bug
Unity-Technologies/com.unity.netcode.gameobjects#3912 · 5 bình luận · 1 người được giao ·
-
Tracking type:feature-2.x
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
Unity-Technologies/com.unity.netcode.gameobjects#3870 · 5 bình luận ·
-
Please error, or at least warn, when a managed type is included in INetworkSerializeByMemcpy Đang mởTracking type:feature
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
Unity-Technologies/com.unity.netcode.gameobjects#3830 · 7 bình luận ·
Tất cả issue của Unity-Technologies/com.unity.netcode.gameobjects
Issue tương tự
-
type/automation type/tech-debt
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
t/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
ci-failure-cause test-failure
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
area:auth FE mvp P3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
klasolsson81/jobbliggaren#1788 ·