static void RestoreCrossSceneReferences()
		{
			Scene activeScene = new Scene();
			AmsMultiSceneSetup activeSetup = null;
			List<AmsMultiSceneSetup.SceneEntry> bakedScenes = new List<AmsMultiSceneSetup.SceneEntry>();

			GetCommonParameters( ref activeScene, ref activeSetup, bakedScenes );
			if ( bakedScenes.Count < 1 )
				return;

			AmsDebug.Log( null, "Running RestoreCrossSceneReferences on Scene {0}", activeScene.name );

			// Do the merge (bake)
			var targetCrossRefs = AmsCrossSceneReferences.GetSceneSingleton( activeScene, false );
			if ( targetCrossRefs )
				targetCrossRefs.ResolvePendingCrossSceneReferences();

			foreach( var entry in bakedScenes )
			{
				if ( !entry.scene.isLoaded )
				{
					AmsDebug.LogError( activeSetup, "Could not restore cross-scene references for non-loaded scene: {0}", entry.scene.name );
					continue;
				}

				var sourceCrossRefs = AmsCrossSceneReferences.GetSceneSingleton( entry.scene.scene, false );
				if ( sourceCrossRefs )
					sourceCrossRefs.ResolvePendingCrossSceneReferences();
			}
		}
		static void MergeScenes()
		{
			Scene activeScene = new Scene();
			AmsMultiSceneSetup activeSetup = null;
			List<AmsMultiSceneSetup.SceneEntry> bakedScenes = new List<AmsMultiSceneSetup.SceneEntry>();

			GetCommonParameters( ref activeScene, ref activeSetup, bakedScenes );
			if ( bakedScenes.Count < 1 )
				return;

			AmsDebug.Log( null, "Running MergeScenes on Scene {0}", activeScene.name );

			foreach( var entry in bakedScenes )
			{
				if ( !entry.scene.isLoaded )
				{
					AmsDebug.LogError( activeSetup, "Could not merge non-loaded scene: {0}", entry.scene.name );
					continue;
				}

				var sourceCrossRefs = AmsCrossSceneReferences.GetSceneSingleton( entry.scene.scene, false );
				if ( sourceCrossRefs )
					GameObject.DestroyImmediate( sourceCrossRefs.gameObject, false );

				AmsDebug.Log( null, "Merging {0} into {1}", entry.scene.name, activeScene.name );
				EditorSceneManager.MergeScenes( entry.scene.scene, activeScene );
			}
		} // MergeScenes
Beispiel #3
0
		public static void HandleCrossSceneReferences( IList<Scene> scenes )
		{
			// If we don't allow cross-scene references, then early return.
			var crossSceneReferenceBehaviour = AmsPreferences.CrossSceneReferencing;
			bool bSkipCrossSceneReferences = (crossSceneReferenceBehaviour == AmsPreferences.CrossSceneReferenceHandling.UnityDefault);
			bool bSaveCrossSceneReferences = (crossSceneReferenceBehaviour == AmsPreferences.CrossSceneReferenceHandling.Save);

			if ( bSkipCrossSceneReferences || scenes.Count < 1 )
				return;

			// We need to create an AmsMultiSceneSetup singleton in every scene.  This is how we keep track of Awake scenes and
			// it also allows us to use cross-scene references.
			foreach( var scene in scenes )
			{
				if ( !scene.isLoaded )
					continue;

				// Reset all of the cross-scene references for loaded scenes.
				var crossSceneRefBehaviour = AmsCrossSceneReferences.GetSceneSingleton( scene, true );
				for (int i = 0 ; i < EditorSceneManager.sceneCount ; ++i)
				{
					var otherScene = EditorSceneManager.GetSceneAt(i);
					if ( otherScene.isLoaded )
						crossSceneRefBehaviour.ResetCrossSceneReferences( otherScene );
				}
			}

			var xSceneRefs = AmsCrossSceneReferenceProcessor.GetCrossSceneReferencesForScenes( scenes );
			if ( bSaveCrossSceneReferences && xSceneRefs.Count > 0 )
			{
				AmsDebug.LogWarning( null, "Ams Plugin: Found {0} Cross-Scene References. Saving them.", xSceneRefs.Count );
				AmsCrossSceneReferenceProcessor.SaveCrossSceneReferences( xSceneRefs );
			}

			// Zero-out these cross-scene references so we can save without pulling in those assets.
			for(int i = 0 ; i < xSceneRefs.Count ; ++i)
			{
				var xRef = xSceneRefs[i];
				int refIdToRestore = xRef.fromProperty.objectReferenceInstanceIDValue;
					
				if ( !bSaveCrossSceneReferences )
					Debug.LogWarningFormat( "Cross-Scene Reference {0} will become null", xRef );

				// Set it to null.
				xRef.fromProperty.objectReferenceInstanceIDValue = 0;
				xRef.fromProperty.serializedObject.ApplyModifiedPropertiesWithoutUndo();

				// Restore if we're not about to enter play mode
				if ( !EditorApplication.isPlayingOrWillChangePlaymode )
				{
					EditorApplication.delayCall += () =>
						{
							AmsDebug.Log( null, "Restoring Cross-Scene Ref (Post-Save): {0}", xRef );
							xRef.fromProperty.objectReferenceInstanceIDValue = refIdToRestore;
							xRef.fromProperty.serializedObject.ApplyModifiedPropertiesWithoutUndo();
						};
				}
			}
		}
Beispiel #4
0
        /// <summary>
        /// Save all of the passed-in cross-scene references.  The entries in the passed-in list will be removed as they are properly accounted for.
        /// </summary>
        /// <param name="editorCrossSceneRefs"></param>
        public static void SaveCrossSceneReferences( List<EditorCrossSceneReference> editorCrossSceneRefs )
        {
            // Save all of the cross-scene references, removing them from our input list as we receive them
            for( int i = editorCrossSceneRefs.Count-1 ; i >= 0 ; --i)
            {
                var xRef = editorCrossSceneRefs[i];

				AmsDebug.Log( null, "Saving Cross-Scene Reference: {0}", xRef );
				
                try
                {
					RuntimeCrossSceneReference serializedReference = xRef.ToSerializable();

					try
					{
						// Save the object
						var initialObject = xRef.fromProperty.objectReferenceValue;
						
						// Resolve it (this can throw exceptions)
						AmsCrossSceneReferenceResolver.Resolve( serializedReference );
#if UNITY_5_6_OR_NEWER
						xRef.fromProperty.serializedObject.UpdateIfRequiredOrScript();
#else
						xRef.fromProperty.serializedObject.UpdateIfDirtyOrScript();
#endif

						// Check to make sure it resolved properly
						if ( initialObject && xRef.fromProperty.objectReferenceValue != initialObject )
							throw new ResolveException( string.Format("Resolve should have pointed to {0} ({1}) but instead resolved to {2} ({3})", initialObject ? initialObject.ToString() : "(null)", initialObject ? initialObject.GetInstanceID() : 0, xRef.fromProperty.objectReferenceValue, xRef.fromProperty.objectReferenceInstanceIDValue) );
					}
					catch ( System.Exception ex )
					{
						AmsDebug.LogError( xRef.fromObject, "Could not perform a runtime resolve on cross-scene reference {0}.\nReason: {1}. Please review Documentation.", serializedReference, ex.Message );
						continue;
					}

					// Record the cross-scene reference
					var crossSceneRefBehaviour = AmsCrossSceneReferences.GetSceneSingleton( xRef.fromScene, true );
					crossSceneRefBehaviour.AddReference( serializedReference );

                    // Add an updated reference map value
                    if ( _referenceMap != null )
                        _referenceMap.Add( new KeyValuePair<SerializedProperty, Object>(xRef.fromProperty, xRef.fromProperty.objectReferenceValue) );
                }
                catch ( UnityException ex )
                {
                    Debug.LogException( ex );
                }
            }
        }