Skip to content

Commit 345e154

Browse files
fix
- Adjusted the `InScenePlaced` property to internally set or publicly get the serialized `m_InScenePlaced` field. - `NetworkObject` now implements `ISerializationCallbackReceiver` to assure `m_InScenePlaced` is always properly set for in-scene placed objects. - Removed legacy define for UNITY_2021_2_OR_NEWER since v2.x.x+ is only for Unity v6.
1 parent ec5be76 commit 345e154

1 file changed

Lines changed: 47 additions & 23 deletions

File tree

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
using Unity.Netcode.Runtime;
1010
#if UNITY_EDITOR
1111
using UnityEditor;
12-
#if UNITY_2021_2_OR_NEWER
1312
using UnityEditor.SceneManagement;
14-
#else
15-
using UnityEditor.Experimental.SceneManagement;
16-
#endif
1713
#endif
1814
using UnityEngine;
1915
using UnityEngine.SceneManagement;
@@ -28,8 +24,17 @@ namespace Unity.Netcode
2824
[AddComponentMenu("Netcode/Network Object", -99)]
2925
[DisallowMultipleComponent]
3026
[HelpURL(HelpUrls.NetworkObject)]
31-
public sealed class NetworkObject : MonoBehaviour
27+
public sealed class NetworkObject : MonoBehaviour, ISerializationCallbackReceiver
3228
{
29+
void ISerializationCallbackReceiver.OnBeforeSerialize()
30+
{
31+
m_InScenePlaced = gameObject.scene.IsValid() && gameObject.scene.buildIndex >= 0;
32+
}
33+
34+
void ISerializationCallbackReceiver.OnAfterDeserialize()
35+
{
36+
}
37+
3338
[HideInInspector]
3439
[SerializeField]
3540
internal uint GlobalObjectIdHash;
@@ -1232,15 +1237,34 @@ public bool HasOwnershipStatus(OwnershipStatus status)
12321237
/// <summary>
12331238
/// Gets if the object is a SceneObject.
12341239
/// </summary>
1240+
/// <remarks>
1241+
/// This method is marked for deprecation.<br />
1242+
/// Use <see cref="InScenePlaced"/> instead.
1243+
/// </remarks>
12351244
[Obsolete("Use InScenePlaced instead")]
12361245
public bool? IsSceneObject { get; internal set; }
12371246

12381247
/// <summary>
1239-
/// True if this object is placed in a scene; false otherwise.
1248+
/// The serialized field for <see cref="InScenePlaced"/>.
12401249
/// </summary>
12411250
[field: HideInInspector]
12421251
[field: SerializeField]
1243-
public bool InScenePlaced { get; internal set; }
1252+
private bool m_InScenePlaced;
1253+
1254+
/// <summary>
1255+
/// True if this object is placed in a scene; false otherwise.
1256+
/// </summary>
1257+
public bool InScenePlaced
1258+
{
1259+
get
1260+
{
1261+
return m_InScenePlaced;
1262+
}
1263+
internal set
1264+
{
1265+
m_InScenePlaced = value;
1266+
}
1267+
}
12441268

12451269
/// <summary>
12461270
/// Sets whether this NetworkObject was instantiated as part of a scene
@@ -2686,24 +2710,24 @@ internal bool ApplyNetworkParenting(bool removeParent = false, bool ignoreNotSpa
26862710
}
26872711
else // If the parent still isn't spawned add this to the orphaned children and return false
26882712
if (!parentNetworkObject.IsSpawned)
2689-
{
2690-
OrphanChildren.Add(this);
2691-
return false;
2692-
}
2693-
else
2694-
{
2695-
// If we made it this far, go ahead and set the network parenting values
2696-
// with the WorldPoisitonSays value set to false
2697-
// Note: Since in-scene placed NetworkObjects are parented in the scene
2698-
// the default "assumption" is that children are parenting local space
2699-
// relative.
2700-
SetNetworkParenting(parentNetworkObject.NetworkObjectId, false);
2713+
{
2714+
OrphanChildren.Add(this);
2715+
return false;
2716+
}
2717+
else
2718+
{
2719+
// If we made it this far, go ahead and set the network parenting values
2720+
// with the WorldPoisitonSays value set to false
2721+
// Note: Since in-scene placed NetworkObjects are parented in the scene
2722+
// the default "assumption" is that children are parenting local space
2723+
// relative.
2724+
SetNetworkParenting(parentNetworkObject.NetworkObjectId, false);
27012725

2702-
// Set the cached parent
2703-
SetCachedParent(parentNetworkObject.transform);
2726+
// Set the cached parent
2727+
SetCachedParent(parentNetworkObject.transform);
27042728

2705-
return true;
2706-
}
2729+
return true;
2730+
}
27072731
}
27082732

27092733
// If we are removing the parent or our latest parent is not set, then remove the parent.

0 commit comments

Comments
 (0)