Skip to content

Commit

Permalink
SaveDuringPlay supports multi-scene editing
Browse files Browse the repository at this point in the history
  • Loading branch information
glabute committed Nov 9, 2023
1 parent fe52c14 commit a7375d1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions com.unity.cinemachine/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Added CinemachineVirtualCameraBase.CancelDamping() convenience method to snap camera to its target position.
- Added CinemachineOrbitalFollow.TargetOffset to reposition orbit center.
- SaveDuringPlay supports multi-scene editing.

### Changed
- RuntimeUtility.GetScratchCollider and RuntimeUtility.DestroyScratchCollider are now public, to allow custom extensions to use them.
Expand Down
36 changes: 21 additions & 15 deletions com.unity.cinemachine/Editor/SaveDuringPlay/SaveDuringPlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using UnityEditor;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.SceneManagement;

namespace Unity.Cinemachine.Editor
{
Expand All @@ -26,7 +27,7 @@ public static string GetFullName(GameObject current)
/// <summary>
/// Will find the named object, active or inactive, from the full path.
/// </summary>
public static GameObject FindObjectFromFullName(string fullName, GameObject[] roots)
public static GameObject FindObjectFromFullName(string fullName, List<GameObject> roots)
{
if (string.IsNullOrEmpty(fullName) || roots == null)
return null;
Expand All @@ -36,7 +37,7 @@ public static GameObject FindObjectFromFullName(string fullName, GameObject[] ro
return null;

Transform root = null;
for (int i = 0; root == null && i < roots.Length; ++i)
for (int i = 0; root == null && i < roots.Count; ++i)
if (roots[i].name == path[1])
root = roots[i].transform;

Expand All @@ -62,17 +63,20 @@ public static GameObject FindObjectFromFullName(string fullName, GameObject[] ro
return root.gameObject;
}

/// <summary>Finds all the root objects in a scene, active or not</summary>
public static GameObject[] FindAllRootObjectsInScene()
/// <summary>Finds all the root objects, active or not, in all open scenes</summary>
public static List<GameObject> FindAllRootObjectsInOpenScenes()
{
return UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects();
var allRoots = new List<GameObject>();
for (int i = 0; i < SceneManager.sceneCount; ++i)
allRoots.AddRange(SceneManager.GetSceneAt(i).GetRootGameObjects());
return allRoots;
}


/// <summary>
/// This finds all the behaviours in scene, active or inactive, excluding prefabs
/// This finds all the behaviours, active or inactive, in open scenes, excluding prefabs
/// </summary>
public static T[] FindAllBehavioursInScene<T>() where T : MonoBehaviour
public static List<T> FindAllBehavioursInOpenScenes<T>() where T : MonoBehaviour
{
List<T> objectsInScene = new ();
var allObjects = Resources.FindObjectsOfTypeAll<T>();
Expand All @@ -88,7 +92,7 @@ public static T[] FindAllBehavioursInScene<T>() where T : MonoBehaviour
continue;
objectsInScene.Add(b);
}
return objectsInScene.ToArray();
return objectsInScene;
}
}

Expand Down Expand Up @@ -302,6 +306,8 @@ class ObjectStateSaver

Dictionary<string, string> mValues = new Dictionary<string, string>();

public string ObjetFullPath => mObjectFullPath;

/// <summary>
/// Recursively collect all the field values in the MonoBehaviours
/// owned by this object and its descendants. The values are stored
Expand All @@ -310,7 +316,7 @@ class ObjectStateSaver
public void CollectFieldValues(GameObject go)
{
mObjectFullPath = ObjectTreeUtil.GetFullName(go);
GameObjectFieldScanner scanner = new GameObjectFieldScanner();
GameObjectFieldScanner scanner = new ();
scanner.FilterField = FilterField;
scanner.FilterComponent = HasSaveDuringPlay;
scanner.OnLeafField = (string fullName, Type type, ref object value) =>
Expand All @@ -323,7 +329,7 @@ public void CollectFieldValues(GameObject go)
scanner.ScanFields(go);
}

public GameObject FindSavedGameObject(GameObject[] roots)
public GameObject FindSavedGameObject(List<GameObject> roots)
{
return ObjectTreeUtil.FindObjectFromFullName(mObjectFullPath, roots);
}
Expand All @@ -335,7 +341,7 @@ public GameObject FindSavedGameObject(GameObject[] roots)
/// value in the game object, Set the GameObject's value using the value
/// recorded in the dictionary.
/// </summary>
public bool PutFieldValues(GameObject go, GameObject[] roots)
public bool PutFieldValues(GameObject go, List<GameObject> roots)
{
GameObjectFieldScanner scanner = new GameObjectFieldScanner();
scanner.FilterField = FilterField;
Expand Down Expand Up @@ -389,7 +395,7 @@ public static bool HasSaveDuringPlay(MonoBehaviour b)
/// because the reflection system breaks them down into their primitive components.
/// You can add more support here, as needed.
/// </summary>
static object LeafObjectFromString(Type type, string value, GameObject[] roots)
static object LeafObjectFromString(Type type, string value, List<GameObject> roots)
{
if (type == typeof(Single))
return float.Parse(value);
Expand Down Expand Up @@ -520,8 +526,8 @@ static void OnPlayStateChanged(PlayModeStateChange pmsc)
static HashSet<GameObject> FindInterestingObjects()
{
var objects = new HashSet<GameObject>();
var everything = ObjectTreeUtil.FindAllBehavioursInScene<MonoBehaviour>();
for (int i = 0; i < everything.Length; ++i)
var everything = ObjectTreeUtil.FindAllBehavioursInOpenScenes<MonoBehaviour>();
for (int i = 0; i < everything.Count; ++i)
{
var b = everything[i];
if (!objects.Contains(b.gameObject) && ObjectStateSaver.HasSaveDuringPlay(b))
Expand Down Expand Up @@ -557,7 +563,7 @@ static void RestoreAllInterestingStates()
{
//Debug.Log("Updating state for all interesting objects");
bool dirty = false;
var roots = ObjectTreeUtil.FindAllRootObjectsInScene();
var roots = ObjectTreeUtil.FindAllRootObjectsInOpenScenes();
for (int i = 0; i < s_SavedStates.Count; ++i)
{
var saver = s_SavedStates[i];
Expand Down

0 comments on commit a7375d1

Please sign in to comment.