Skip to content

Commit

Permalink
fix: when modifying a prefab, Unity calls OnValidate for all scene ob…
Browse files Browse the repository at this point in the history
…jects based on that prefab, which caused Mirror to reset the sceneId because we only checked if a prefab is currently edited, not if THIS prefab is currently edited
  • Loading branch information
miwarnec committed Nov 3, 2019
1 parent 188b74e commit db99dd7
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions Assets/Mirror/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,33 @@ void SetupIDs()
m_SceneId = 0; // force 0 for prefabs
AssignAssetID(gameObject);
}
// check prefabstage BEFORE SceneObjectWithPrefabParent
// (fixes https://github.com/vis2k/Mirror/issues/976)
// are we currently in prefab editing mode? aka prefab stage
// => check prefabstage BEFORE SceneObjectWithPrefabParent
// (fixes https://github.com/vis2k/Mirror/issues/976)
// => if we don't check GetCurrentPrefabStage and only check
// GetPrefabStage(gameObject), then the 'else' case where we
// assign a sceneId and clear the assetId would still be
// triggered for prefabs. in other words: if we are in prefab
// stage, do not bother with anything else ever!
else if (PrefabStageUtility.GetCurrentPrefabStage() != null)
{
m_SceneId = 0; // force 0 for prefabs
string path = PrefabStageUtility.GetCurrentPrefabStage().prefabAssetPath;
AssignAssetID(path);
// when modifying a prefab in prefab stage, Unity calls
// OnValidate for that prefab and for all scene objects based on
// that prefab.
//
// is this GameObject the prefab that we modify, and not just a
// scene object based on the prefab?
// * GetCurrentPrefabStage = 'are we editing ANY prefab?'
// * GetPrefabStage(go) = 'are we editing THIS prefab?'
if (PrefabStageUtility.GetPrefabStage(gameObject) != null)
{
m_SceneId = 0; // force 0 for prefabs
//Debug.Log(name + " @ scene: " + gameObject.scene.name + " sceneid reset to 0 because CurrentPrefabStage=" + PrefabStageUtility.GetCurrentPrefabStage() + " PrefabStage=" + PrefabStageUtility.GetPrefabStage(gameObject));
// NOTE: might make sense to use GetPrefabStage for asset
// path, but let's not touch it while it works.
string path = PrefabStageUtility.GetCurrentPrefabStage().prefabAssetPath;
AssignAssetID(path);
}
}
else if (ThisIsASceneObjectWithPrefabParent(out GameObject prefab))
{
Expand Down

0 comments on commit db99dd7

Please sign in to comment.