11using System ;
22using System . Collections . Generic ;
3+ using Unity . Collections ;
34using UnityEngine ;
45
56namespace Unity . Netcode
@@ -8,47 +9,39 @@ namespace Unity.Netcode
89 /// Specialized version of <see cref="INetworkPrefabInstanceHandler"/> that receives
910 /// custom instantiation data injected by the server before spawning.
1011 /// </summary>
11- public interface INetworkPrefabInstanceHandlerWithData < T > : INetworkPrefabInstanceHandlerWithData where T : struct , INetworkSerializable
12+ public interface INetworkPrefabInstanceHandlerWithData < T > where T : struct , INetworkSerializable
1213 {
13- // Propagating custom data through the entire spawn pipeline would add complexity and reduce modularity.
14- // To work around this, deserialization injects the data into this static map, keyed by the handler instance.
15- // The handler then reads the data at instantiation time, keeping the interface clean and stateless from the user's perspective.
16- // This avoids requiring implementers to store the data explicitly or declare additional fields.
17- static readonly Dictionary < INetworkPrefabInstanceHandlerWithData , T > _handlerToData = new ( ) ;
14+ NetworkObject Instantiate ( ulong ownerClientId , Vector3 position , Quaternion rotation , T instantiationData ) ;
15+ void Destroy ( NetworkObject networkObject ) ;
1816
19- NetworkObject INetworkPrefabInstanceHandler . Instantiate ( ulong ownerClientId , Vector3 position , Quaternion rotation ) => Instantiate ( ownerClientId , position , rotation , _handlerToData [ this ] ) ;
20- void INetworkPrefabInstanceHandlerWithData . RemoveDataEntry ( INetworkPrefabInstanceHandlerWithData instance ) => _handlerToData . Remove ( instance ) ;
21- bool INetworkPrefabInstanceHandlerWithData . HandlesDataType < U > ( ) => typeof ( T ) == typeof ( U ) ;
22- void INetworkPrefabInstanceHandlerWithData . ReadInstantiationData < RW > ( ref BufferSerializer < RW > serializer )
17+ public static void InjectInstantiationDataForObject ( GameObject prefab , T instantiationData )
2318 {
24- _handlerToData . TryGetValue ( this , out var value ) ;
25- serializer . SerializeValue ( ref value ) ;
26- _handlerToData [ this ] = value ;
19+ if ( prefab . TryGetComponent < NetworkObject > ( out var networkObject ) )
20+ {
21+ networkObject . NetworkManager . PrefabHandler . InjectInstantiationData ( prefab , instantiationData ) ;
22+ }
23+ else
24+ {
25+ Debug . LogError ( $ "Prefab { prefab . name } does not have a NetworkObject component.") ;
26+ }
2727 }
28-
29- NetworkObject Instantiate ( ulong ownerClientId , Vector3 position , Quaternion rotation , T instantiationData ) ;
3028 }
3129
32- /// <summary>
33- /// Internal use only. Do not implement directly. Use <see cref="INetworkPrefabInstanceHandlerWithData{T}"/> instead.
34- /// </summary>
35- public interface INetworkPrefabInstanceHandlerWithData : INetworkPrefabInstanceHandler
30+ internal interface INetworkPrefabInstanceHandlerWithData : INetworkPrefabInstanceHandler
3631 {
37- /// <summary>
38- /// Invoked during deserialization to read the instantiation data associated with this prefab instance.
39- /// </summary>
40- void ReadInstantiationData < T > ( ref BufferSerializer < T > serializer ) where T : IReaderWriter ;
32+ bool HandlesDataType < T > ( ) ;
33+ void ReadInstantiationData < TReaderWriter > ( ref BufferSerializer < TReaderWriter > serializer ) where TReaderWriter : IReaderWriter ;
34+ }
4135
42- /// <summary>
43- /// Removes the data entry for the given instance.
44- /// Is important to call this when the instance isnt referenced to avoid memory leaks.
45- /// </summary>
46- /// <param name="instance"></param>
47- void RemoveDataEntry ( INetworkPrefabInstanceHandlerWithData instance ) ;
36+ internal class HandlerWrapper < T > : INetworkPrefabInstanceHandlerWithData where T : struct , INetworkSerializable
37+ {
38+ private readonly INetworkPrefabInstanceHandlerWithData < T > _impl ;
39+ private T _payload ;
4840
49- /// <summary>
50- /// Returns true if <typeparamref name="T"/> matches the expected instantiation data type for this handler.
51- /// </summary>
52- bool HandlesDataType < T > ( ) where T : struct , INetworkSerializable ;
41+ public HandlerWrapper ( INetworkPrefabInstanceHandlerWithData < T > impl ) => _impl = impl ;
42+ public bool HandlesDataType < U > ( ) => typeof ( T ) == typeof ( U ) ;
43+ public void ReadInstantiationData < TReaderWriter > ( ref BufferSerializer < TReaderWriter > serializer ) where TReaderWriter : IReaderWriter => serializer . SerializeValue ( ref _payload ) ;
44+ public NetworkObject Instantiate ( ulong ownerClientId , Vector3 position , Quaternion rotation ) => _impl . Instantiate ( ownerClientId , position , rotation , _payload ) ;
45+ public void Destroy ( NetworkObject networkObject ) => _impl . Destroy ( networkObject ) ;
5346 }
54- }
47+ }
0 commit comments