Skip to content

Commit

Permalink
SaveDuringPlay: don't crush objects that have been reparented
Browse files Browse the repository at this point in the history
  • Loading branch information
glabute committed Aug 6, 2024
1 parent a8ae79b commit 89fafa2
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions com.unity.cinemachine/Editor/SaveDuringPlay/SaveDuringPlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,18 @@ public bool PutFieldValues(GameObject go, List<GameObject> roots)
scanner.OnLeafField = (string fullName, Type type, ref object value) =>
{
// Lookup the value in the dictionary
if (m_Values.TryGetValue(fullName, out string savedValue)
&& StringFromLeafObject(value) != savedValue)
if (m_Values.TryGetValue(fullName, out string savedStringValue)
&& StringFromLeafObject(value) != savedStringValue)
{
//Debug.Log("Put " + m_ObjectFullPath + "." + fullName + " = " + m_Values[fullName] + " --- was " + StringFromLeafObject(value));
value = LeafObjectFromString(type, m_Values[fullName].Trim(), roots);
return true; // changed
//Debug.Log("Put " + m_ObjectFullPath + "." + fullName + " = " + savedStringValue + " --- was " + StringFromLeafObject(value));
var newValue = LeafObjectFromString(type, savedStringValue.Trim(), roots);
// Ignore path mismatches due to reparenting during the game, but don't ignore if the value was deliberately set to null
if (newValue != null || savedStringValue.Length == 0)
{
value = newValue;
return true; // changed
}
}
return false;
};
Expand Down Expand Up @@ -425,14 +431,9 @@ static object LeafObjectFromString(Type type, string value, List<GameObject> roo
return (go != null) ? go.GetComponent(type) : null;
}
if (typeof(GameObject).IsAssignableFrom(type))
{
// Try to find the named game object
return GameObject.Find(value);
}
return GameObject.Find(value); // Try to find the named game object
if (typeof(ScriptableObject).IsAssignableFrom(type))
{
return AssetDatabase.LoadAssetAtPath(value, type);
}
return null;
}

Expand Down

0 comments on commit 89fafa2

Please sign in to comment.