Skip to content

Commit f4b6cd4

Browse files
EmandMharayuu9
andauthored
feat: add AsNativeArray() read‑only accessor to NetworkList<T> (#3567)
Continues: #3562 from @harayuu9 This PR exposes the contents of a `NetworkList<T>` as a **read‑only `NativeArray<T>`**. It addresses the recurring need to interoperate efficiently with Burst‑compiled or `IJob`‑based code without paying the cost of element‑wise copies or allocations. The accessor simply forwards `m_List.AsReadOnly()`, preserving zero‑allocation semantics while respecting `NativeArray`’s lifetime constraints (call‑site must guarantee the list is not mutated or disposed). No behavioural changes are introduced—only a pure additive surface‑area expansion. ## Changelog - Added: `NetworkList<T>.AsNativeArray()` to return the list contents as a `NativeArray<T>.ReadOnly`. ## Testing and Documentation - No tests have been added. - Includes documentation for the newly added public API function. ## Backport This adds a new API surface area and so no backport is required. --------- Co-authored-by: Yuto Harada <[email protected]>
1 parent 1bcb377 commit f4b6cd4

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1010

1111
### Added
1212

13+
- Added `AsNativeArray()` read‑only accessor to `NetworkList<T>` (#3567)
1314

1415
### Fixed
1516

com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,24 @@ public T this[int index]
631631
}
632632
}
633633

634+
/// <summary>
635+
/// Gets a **zero‑allocation**, <see cref="NativeArray{T}.ReadOnly"/> view over the current
636+
/// elements of this <see cref="NetworkList{T}"/>.
637+
/// </summary>
638+
/// <remarks>
639+
/// The returned array stays valid **only until** the list is mutated (add, remove,
640+
/// clear, resize) or <see cref="Dispose()"/> is called on the container. Continuing to use
641+
/// the array after it is invalid will result in undefined behaviour;
642+
/// callers are responsible for ensuring a safe lifetime.
643+
/// </remarks>
644+
/// <returns>
645+
/// A <see cref="NativeArray{T}.ReadOnly"/> reference that shares the same backing memory as this list.
646+
/// </returns>
647+
public NativeArray<T>.ReadOnly AsNativeArray()
648+
{
649+
return m_List.AsReadOnly();
650+
}
651+
634652
private void HandleAddListEvent(NetworkListEvent<T> listEvent)
635653
{
636654
m_DirtyEvents.Add(listEvent);

0 commit comments

Comments
 (0)