Skip to content

feat: add AsNativeArray() read‑only accessor to `NetworkList<T> #3562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

harayuu9
Copy link

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

com.unity.netcode.gameobjects

  • Added: NetworkList<T>.AsNativeArray() → returns the list contents as a NativeArray<T>.ReadOnly.

Testing and Documentation

  • No tests have been added. (The method is a thin wrapper around an existing, fully‑tested Unity collection API.)
  • Includes documentation for a previously‑undocumented public API entry point (XML doc‑comment added inline).

@harayuu9 harayuu9 requested a review from a team as a code owner July 28, 2025 10:01
@unity-cla-assistant
Copy link

unity-cla-assistant commented Jul 28, 2025

CLA assistant check
All committers have signed the CLA.

@EmandM
Copy link
Collaborator

EmandM commented Jul 30, 2025

Thank you so much for your contribution!

To add surface area to the Netcode for GameObjects package we have the requirement that every function needs to have C# code documentation. Do you think you can add that code documentation to this new function?

@sentinel-u3d sentinel-u3d bot requested a review from EmandM July 30, 2025 15:34
@harayuu9
Copy link
Author

Thank you for the review. Just to confirm, by "C# code documentation," are you referring to XML comments? I had originally included them, but I've edited them using the <see cref> format to match the surrounding code. If you were referring to something else, please let me know.

Copy link
Collaborator

@EmandM EmandM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's my bad for reviewing too fast. I did mean the XML docs. These expanded docs are great!

Next steps: I will port this PR over to an internal branch so we can run our internal tests against it. I'll close this PR when I open the internal one, Github will link everything together so you can track the progress if you'd like

@EmandM
Copy link
Collaborator

EmandM commented Jul 30, 2025

Before I close this, do you mind confirming if you need this to land in an editor version that is older than Unity 6? Our team is trying to limit the new API surface area that we add to the older versions of the package.

@EmandM EmandM closed this Jul 30, 2025
@harayuu9
Copy link
Author

In my use case, support for versions earlier than Unity 6 is not required. So either way is fine for me — is that what you're asking?

As for the NGO, we currently require [email protected] as the minimum version, and I confirmed that at least the AsReadOnly() API exists in 2.2. So I didn’t think this would introduce any new constraints.

EmandM added a commit that referenced this pull request Aug 5, 2025
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants