public void LoadInfiniteObjectPersistence(InfiniteObjectPersistence persistence) { totalDistance = persistence.totalDistance; totalSceneDistance = persistence.totalSceneDistance; objectLocationAngle = persistence.objectLocationAngle; topPlatformObjectSpawned = persistence.topPlatformObjectSpawned; bottomPlatformObjectSpawned = persistence.bottomPlatformObjectSpawned; topSceneObjectSpawned = persistence.topSceneObjectSpawned; bottomSceneObjectSpawned = persistence.bottomSceneObjectSpawned; previousPlatformSection = persistence.previousPlatformSection; previousSceneSection = persistence.previousSceneSection; spawnedPlatformSectionTransition = persistence.spawnedPlatformSectionTransition; spawnedSceneSectionTransition = persistence.spawnedSceneSectionTransition; int objectCount = objectSpawnIndex[0].Count; int width = (int)ObjectLocation.Last; for (int i = 0; i < (int)ObjectLocation.Last; ++i) { for (int j = 0; j < objectCount; ++j) { objectSpawnIndex[i][j] = persistence.objectSpawnIndex[i * width + j]; lastObjectSpawnDistance[i][j] = persistence.lastObjectSpawnDistance[i * width + j]; } for (int j = 0; j < (int)ObjectType.Last; ++j) { objectTypeSpawnIndex[i][j] = persistence.objectTypeSpawnIndex[i * width + j]; lastLocalIndex[i][j] = persistence.lastLocalIndex[i * width + j]; latestObjectTypeSpawnDistance[i][j] = persistence.latestObjectTypeSpawnDistance[i * width + j]; } } }
private void loadInfiniteObjectPersistence(InfiniteObjectPersistence persistence) { localDistance = persistence.localDistance; localSceneDistance = persistence.localSceneDistance; localPlatformHeight = persistence.localPlatformHeight; localSceneHeight = persistence.localSceneHeight; }
// For persisting the data: public void saveInfiniteObjectPersistence(ref InfiniteObjectPersistence persistence) { persistence.localDistance = localDistance; persistence.localSceneDistance = localSceneDistance; persistence.localPlatformHeight = localPlatformHeight; persistence.localSceneHeight = localSceneHeight; }
// For persisting the data: public void saveInfiniteObjectPersistence(ref InfiniteObjectPersistence persistence) { persistence.totalDistance = totalDistance; persistence.totalSceneDistance = totalSceneDistance; int objectCount = objectSpawnIndex[0].Count; persistence.objectSpawnIndex = new int[(int)ObjectLocation.Last * objectCount]; persistence.lastObjectSpawnDistance = new float[(int)ObjectLocation.Last * objectCount]; persistence.objectTypeSpawnIndex = new int[(int)ObjectLocation.Last * (int)ObjectType.Last]; persistence.lastLocalIndex = new int[(int)ObjectLocation.Last * (int)ObjectType.Last]; persistence.latestObjectTypeSpawnDistance = new float[(int)ObjectLocation.Last * (int)ObjectType.Last]; int width = (int)ObjectLocation.Last; for (int i = 0; i < (int)ObjectLocation.Last; ++i) { for (int j = 0; j < objectCount; ++j) { persistence.objectSpawnIndex[i * width + j] = objectSpawnIndex[i][j]; persistence.lastObjectSpawnDistance[i * width + j] = lastObjectSpawnDistance[i][j]; } for (int j = 0; j < (int)ObjectType.Last; ++j) { persistence.objectTypeSpawnIndex[i * width + j] = objectTypeSpawnIndex[i][j]; persistence.lastLocalIndex[i * width + j] = lastLocalIndex[i][j]; persistence.latestObjectTypeSpawnDistance[i * width + j] = latestObjectTypeSpawnDistance[i][j]; } } }
public GameObject CreateStartupObjects(bool tutorial) { InfiniteObjectPersistence prefab = (tutorial ? tutorialObjects : startupObjects); if (prefab != null) { return(GameObject.Instantiate(prefab.gameObject) as GameObject); } return(null); }
public void OnGUI() { GUILayout.Label("Infinite Object Persistence", "BoldLabel"); GUILayout.Label("1. Generate desired tracks using rules and probabilities"); GUILayout.Label("2. Hit play in Unity"); GUILayout.Label("3. Add extra objects (such as tutorial triggers)"); GUILayout.Label("4. Click \"Persist\""); GUILayout.Space(10); if (GUILayout.Button("Persist")) { saveLocation = EditorUtility.SaveFilePanelInProject("Save Location", "InfiniteObjectPersistence", "prefab", ""); if (saveLocation.Length == 0) { return; } GameObject infiniteObjectsGroup = GameObject.Find("Infinite Objects"); if (infiniteObjectsGroup != null) { GameObject persistGameObject = new GameObject(); InfiniteObjectPersistence persistence = persistGameObject.AddComponent <InfiniteObjectPersistence>() as InfiniteObjectPersistence; // Persist the Infinite Object Manager Data InfiniteObjectGenerator infiniteObjectGenerator = infiniteObjectsGroup.GetComponent <InfiniteObjectGenerator>(); infiniteObjectGenerator.saveInfiniteObjectPersistence(ref persistence); // Persist the Infinite Object History Data InfiniteObjectHistory infiniteObjectHistory = infiniteObjectsGroup.GetComponent <InfiniteObjectHistory>(); infiniteObjectHistory.saveInfiniteObjectPersistence(ref persistence); for (int i = infiniteObjectsGroup.transform.childCount - 1; i >= 0; --i) { infiniteObjectsGroup.transform.GetChild(i).parent = persistGameObject.transform; } EditorUtility.SetDirty(persistGameObject); PrefabUtility.CreatePrefab(saveLocation, persistGameObject); for (int i = persistGameObject.transform.childCount - 1; i >= 0; --i) { persistGameObject.transform.GetChild(i).parent = infiniteObjectsGroup.transform; } DestroyImmediate(persistGameObject); Debug.Log("Infinite Object Data Persisted!"); } else { Debug.Log("Error: Unable to find the Infinite Objects Game Object"); } } }
// For persisting the data: public void SaveInfiniteObjectPersistence(ref InfiniteObjectPersistence persistence) { persistence.totalDistance = totalDistance; persistence.totalSceneDistance = totalSceneDistance; persistence.objectLocationAngle = objectLocationAngle; persistence.topPlatformObjectSpawned = topPlatformObjectSpawned; persistence.bottomPlatformObjectSpawned = bottomPlatformObjectSpawned; persistence.topSceneObjectSpawned = topSceneObjectSpawned; persistence.bottomSceneObjectSpawned = bottomSceneObjectSpawned; persistence.previousPlatformSection = previousPlatformSection; persistence.previousSceneSection = previousSceneSection; persistence.spawnedPlatformSectionTransition = spawnedPlatformSectionTransition; persistence.spawnedSceneSectionTransition = spawnedSceneSectionTransition; int objectCount = objectSpawnIndex[0].Count; persistence.objectSpawnIndex = new int[(int)ObjectLocation.Last * objectCount]; persistence.lastObjectSpawnDistance = new float[(int)ObjectLocation.Last * objectCount]; persistence.objectTypeSpawnIndex = new int[(int)ObjectLocation.Last * (int)ObjectType.Last]; persistence.lastLocalIndex = new int[(int)ObjectLocation.Last * (int)ObjectType.Last]; persistence.latestObjectTypeSpawnDistance = new float[(int)ObjectLocation.Last * (int)ObjectType.Last]; int width = (int)ObjectLocation.Last; for (int i = 0; i < (int)ObjectLocation.Last; ++i) { // Version 2: /* * for (int j = 0; j < objectCount; ++j) { * persistence.objectSpawnIndex[j * width + i] = objectSpawnIndex[i][j]; * persistence.lastObjectSpawnDistance[j * width + i] = lastObjectSpawnDistance[i][j]; * } * for (int j = 0; j < (int)ObjectType.Last; ++j) { * persistence.objectTypeSpawnIndex[j * width + i] = objectTypeSpawnIndex[i][j]; * persistence.lastLocalIndex[j * width + i] = lastLocalIndex[i][j]; * persistence.latestObjectTypeSpawnDistance[j * width + i] = latestObjectTypeSpawnDistance[i][j]; * } **/ for (int j = 0; j < objectCount; ++j) { persistence.objectSpawnIndex[i * width + j] = objectSpawnIndex[i][j]; persistence.lastObjectSpawnDistance[i * width + j] = lastObjectSpawnDistance[i][j]; } for (int j = 0; j < (int)ObjectType.Last; ++j) { persistence.objectTypeSpawnIndex[i * width + j] = objectTypeSpawnIndex[i][j]; persistence.lastLocalIndex[i * width + j] = lastLocalIndex[i][j]; persistence.latestObjectTypeSpawnDistance[i * width + j] = latestObjectTypeSpawnDistance[i][j]; } } }
public void loadInfiniteObjectPersistence(InfiniteObjectPersistence persistence) { totalDistance = persistence.totalDistance; totalSceneDistance = persistence.totalSceneDistance; int objectCount = objectSpawnIndex[0].Count; int width = (int)ObjectLocation.Last; for (int i = 0; i < (int)ObjectLocation.Last; ++i) { for (int j = 0; j < objectCount; ++j) { objectSpawnIndex[i][j] = persistence.objectSpawnIndex[i * width + j]; lastObjectSpawnDistance[i][j] = persistence.lastObjectSpawnDistance[i * width + j]; } for (int j = 0; j < (int)ObjectType.Last; ++j) { objectTypeSpawnIndex[i][j] = persistence.objectTypeSpawnIndex[i * width + j]; lastLocalIndex[i][j] = persistence.lastLocalIndex[i * width + j]; latestObjectTypeSpawnDistance[i][j] = persistence.latestObjectTypeSpawnDistance[i * width + j]; } } }
// creates any startup objects, returns null if no prefabs are assigned public bool ShowStartupObjects(bool tutorial) { GameObject startupObjects = infiniteObjectManager.CreateStartupObjects(tutorial); if (startupObjects == null) { return(false); } Transform objectTypeParent; Transform topObject; Transform transformParent; InfiniteObject parentInfiniteObject; bool isSceneObject; for (int i = 0; i < 2; ++i) { isSceneObject = i == 0; objectTypeParent = startupObjects.transform.Find((isSceneObject ? "Scene" : "Platforms")); // iterate high to low because assignParent is going to set a new parent thus changing the number of children in startup objects for (int j = objectTypeParent.childCount - 1; j >= 0; --j) { topObject = objectTypeParent.GetChild(j); infiniteObjectManager.AssignParent(topObject.GetComponent <InfiniteObject>(), (isSceneObject ? ObjectType.Scene : ObjectType.Platform)); InfiniteObject[] childObjects; if (isSceneObject) { childObjects = topObject.GetComponentsInChildren <SceneObject>() as SceneObject[]; } else { childObjects = topObject.GetComponentsInChildren <PlatformObject>() as PlatformObject[]; } for (int k = 0; k < childObjects.Length; ++k) { childObjects[k].EnableDestroyOnDeactivation(); transformParent = childObjects[k].GetTransform().parent; if ((parentInfiniteObject = transformParent.GetComponent <InfiniteObject>()) != null) { childObjects[k].SetInfiniteObjectParent(parentInfiniteObject); } if (!isSceneObject) // platform { PlatformObject platformObject = ((PlatformObject)childObjects[k]); if (platformObject.leftTurn || platformObject.rightTurn) { turnPlatform[(int)ObjectLocation.Center] = platformObject; turnIndex[(int)ObjectLocation.Center] = infiniteObjectHistory.GetLastLocalIndex(ObjectLocation.Center, ObjectType.Platform); } // mark the coin objects as destructible so they get destroyed if they are collected CoinObject[] coinObjects = platformObject.GetComponentsInChildren <CoinObject>() as CoinObject[]; for (int l = 0; l < coinObjects.Length; ++l) { coinObjects[l].EnableDestroyOnDeactivation(); } } } } } // Get the persistent objects InfiniteObjectPersistence persistence = startupObjects.GetComponent <InfiniteObjectPersistence>(); infiniteObjectHistory.LoadInfiniteObjectPersistence(persistence); // All of the important objects have been taken out, destroy the game object Destroy(startupObjects.gameObject); return(true); }
// For persisting the data: public void SaveInfiniteObjectPersistence(ref InfiniteObjectPersistence persistence) { persistence.totalDistance = totalDistance; persistence.totalSceneDistance = totalSceneDistance; persistence.objectLocationAngle = objectLocationAngle; persistence.topPlatformObjectSpawned = topPlatformObjectSpawned; persistence.bottomPlatformObjectSpawned = bottomPlatformObjectSpawned; persistence.topSceneObjectSpawned = topSceneObjectSpawned; persistence.bottomSceneObjectSpawned = bottomSceneObjectSpawned; persistence.previousPlatformSection = previousPlatformSection; persistence.previousSceneSection = previousSceneSection; persistence.spawnedPlatformSectionTransition = spawnedPlatformSectionTransition; persistence.spawnedSceneSectionTransition = spawnedSceneSectionTransition; int objectCount = objectSpawnIndex[0].Count; persistence.objectSpawnIndex = new int[(int)ObjectLocation.Last * objectCount]; persistence.lastObjectSpawnDistance = new float[(int)ObjectLocation.Last * objectCount]; persistence.objectTypeSpawnIndex = new int[(int)ObjectLocation.Last * (int)ObjectType.Last]; persistence.lastLocalIndex = new int[(int)ObjectLocation.Last * (int)ObjectType.Last]; persistence.latestObjectTypeSpawnDistance = new float[(int)ObjectLocation.Last * (int)ObjectType.Last]; int width = (int)ObjectLocation.Last; for (int i = 0; i < (int)ObjectLocation.Last; ++i) { for (int j = 0; j < objectCount; ++j) { persistence.objectSpawnIndex[i * width + j] = objectSpawnIndex[i][j]; persistence.lastObjectSpawnDistance[i * width + j] = lastObjectSpawnDistance[i][j]; } for (int j = 0; j < (int)ObjectType.Last; ++j) { persistence.objectTypeSpawnIndex[i * width + j] = objectTypeSpawnIndex[i][j]; persistence.lastLocalIndex[i * width + j] = lastLocalIndex[i][j]; persistence.latestObjectTypeSpawnDistance[i * width + j] = latestObjectTypeSpawnDistance[i][j]; } } }
// creates any startup objects, returns null if no prefabs are assigned public bool showStartupObjects(bool tutorial) { GameObject startupObjects = infiniteObjectManager.createStartupObjects(tutorial); if (startupObjects == null) { return(false); } ObjectLocation location; Transform objectTypeParent; Transform parentObject; Transform transformParent; InfiniteObject parentInfiniteObject; bool isSceneObject; for (int i = 0; i < 2; ++i) { isSceneObject = i == 0; objectTypeParent = startupObjects.transform.FindChild((isSceneObject ? "Scene" : "Platforms")); // iterate high to low because assignParent is going to move set a new parent thus changing the number of children in startup objects for (int j = objectTypeParent.childCount - 1; j >= 0; --j) { parentObject = objectTypeParent.GetChild(j); // determine the direction the object is facing based off of the y angle float yAngle = parentObject.eulerAngles.y; if (yAngle > 360 || yAngle < 0) { yAngle = yAngle % 360; } if (yAngle > 269) { location = ObjectLocation.Left; } else if (yAngle > 89) { location = ObjectLocation.Right; } else { location = ObjectLocation.Center; } infiniteObjectHistory.setTopInfiniteObject(location, isSceneObject, parentObject.GetComponent <InfiniteObject>()); infiniteObjectManager.assignParent(parentObject.GetComponent <InfiniteObject>(), (isSceneObject ? ObjectType.Scene : ObjectType.Platform)); InfiniteObject[] childObjects; if (isSceneObject) { childObjects = parentObject.GetComponentsInChildren <SceneObject>() as SceneObject[]; } else { childObjects = parentObject.GetComponentsInChildren <PlatformObject>() as PlatformObject[]; } for (int k = 0; k < childObjects.Length; ++k) { childObjects[k].enableDestroyOnDeactivation(); transformParent = childObjects[k].getTransform().parent; if ((parentInfiniteObject = transformParent.GetComponent <InfiniteObject>()) != null) { childObjects[k].setInfiniteObjectParent(parentInfiniteObject); } // If there are no infinite objects under the current object, it is the last object in the hierarchy if ((isSceneObject && childObjects[k].GetComponentsInChildren <SceneObject>().Length == 1) || (!isSceneObject && childObjects[k].GetComponentsInChildren <PlatformObject>().Length == 1)) { infiniteObjectHistory.setBottomInfiniteObject(location, isSceneObject, childObjects[k].GetComponent <InfiniteObject>()); } if (!isSceneObject) // platform { PlatformObject platformObject = ((PlatformObject)childObjects[k]); if (platformObject.isLeftTurn || platformObject.isRightTurn) { turnPlatform[(int)ObjectLocation.Center] = platformObject; } // mark the coin objects as destructible so they get destroyed if they are collected CoinObject[] coinObjects = platformObject.GetComponentsInChildren <CoinObject>() as CoinObject[]; for (int l = 0; l < coinObjects.Length; ++l) { coinObjects[l].enableDestroyOnDeactivation(); } } } } } // Get the persistent objects InfiniteObjectPersistence persistence = startupObjects.GetComponent <InfiniteObjectPersistence>(); infiniteObjectHistory.loadInfiniteObjectPersistence(persistence); loadInfiniteObjectPersistence(persistence); // All of the important objects have been taken out, destroy the game object Destroy(startupObjects.gameObject); return(true); }