diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index e8a80939f1..ce3051ca2e 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 +- Added `AsNativeArray()` read‑only accessor to `NetworkList` (#3567) ### Fixed diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs index 1a78fc7f81..9f0382c26a 100644 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs +++ b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs @@ -631,6 +631,24 @@ public T this[int index] } } + /// + /// Gets a **zero‑allocation**, view over the current + /// elements of this . + /// + /// + /// The returned array stays valid **only until** the list is mutated (add, remove, + /// clear, resize) or is called on the container. Continuing to use + /// the array after it is invalid will result in undefined behaviour; + /// callers are responsible for ensuring a safe lifetime. + /// + /// + /// A reference that shares the same backing memory as this list. + /// + public NativeArray.ReadOnly AsNativeArray() + { + return m_List.AsReadOnly(); + } + private void HandleAddListEvent(NetworkListEvent listEvent) { m_DirtyEvents.Add(listEvent);