-
Notifications
You must be signed in to change notification settings - Fork 447
feat: Instantiation payload support for INetworkPrefabInstanceHandler #3430
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
base: develop-2.0.0
Are you sure you want to change the base?
Conversation
…efabInstanceHandler.Instantiate() documentation
I just posted a video demonstrating how the system works:
In the video, I spawn The
2025-04-27.21-45-44.mp4By implementing a custom Here is the core implementation: public class TestHandlerDeterministicLink : INetworkPrefabInstanceHandler, INetworkInstantiationPayloadSynchronizer
{
public Dictionary<int, DeterministicIDHolder> deterministicSpawns = new Dictionary<int, DeterministicIDHolder>();
public int customSpawnID = 0;
void INetworkInstantiationPayloadSynchronizer.OnSynchronize<T>(ref BufferSerializer<T> serializer) => serializer.SerializeValue(ref customSpawnID);
public NetworkObject Instantiate(ulong clientId, Vector3 position, Quaternion rotation)
{
var obj = deterministicSpawns[customSpawnID];
TMP_Text text = obj.GetComponent<TMP_Text>();
text.SetText(text.text + "*");
return obj.GetComponent<NetworkObject>();
}
public void Destroy(NetworkObject networkObject) => GameObject.Destroy(networkObject.gameObject);
int nextDeterministicId = 0;
public void InstantiateLocally(GameObject linkablePrefab)
{
var spawned = GameObject.Instantiate(linkablePrefab);
spawned.transform.position = UnityEngine.Random.insideUnitCircle * 0.01f;
var text = spawned.GetComponent<TMP_Text>();
text.SetText(nextDeterministicId.ToString() + "&" + text.text);
var deterministicIdHolder = spawned.GetComponent<DeterministicIDHolder>();
deterministicSpawns[nextDeterministicId] = deterministicIdHolder;
deterministicIdHolder.SetID(nextDeterministicId);
nextDeterministicId++;
}
} Warning While this system enables advanced workflows, |
This would actually save us a lot of trouble. Right now when after we spawn stuff we have to run like two or three RPCs just to finish setting up objects properly. I wish you luck on get it merged on Unity 6.1, it would be super useful for our current project. |
Sure! I'm just waiting for reviewers to be assigned to this PR. |
Solves Issue #3421
Related to the discussions in Pull Request #3419 and Issue #3421 (follow-up proposal based on new approach).
This PR introduces support for sending custom instantiation payloads, allowing
INetworkPrefabInstanceHandler
to receive metadata before callingInstantiate()
.The feature is fully optional, backward-compatible, and requires no changes to existing user workflows.
Changelog
INetworkInstantiationPayloadSynchronizer
interface to synchronize custom pre-instantiation data.Testing and Documentation
INetworkPrefabInstanceHandler.Instantiate()
summary updated to mentionINetworkInstantiationPayloadSynchronizer
INetworkInstantiationPayloadSynchronizer
.Deprecated API
Backport
Implementation Example
Spawning flow:
Important
When spawning, you must update the handler's data before calling
Spawn()
orInstantiateAndSpawn()
.The data set in the handler will be serialized automatically during the prior the instantiation process.
Highlights