/// <summary> /// A simple prefab is that which only needs to be instantiated via name and position/rotation/scalar /// </summary> /// <param name="thisSimpleValue"></param> public static GameObject InstantiateSimplePrefab(GameObject theSimplePrefab, string thisKey, string thisSimpleValue) { string[] words = thisSimpleValue.Split(':'); PositionRotationScale prs = new PositionRotationScale(); prs.position = getVector3(words[0]); prs.rotation = getRotation3(words[1]); prs.scaler = getVector3(words[2]); GameObject newObject = GameObject.Instantiate(theSimplePrefab, prs.position, prs.rotation); newObject.transform.localScale = prs.scaler; newObject.tag = thisKey; return(newObject); }
/// <summary> /// Adds simple gameobject, only requiring name and position/rotation/scale, to serialize for later instantiation /// </summary> /// <param name="tagMap"></param> /// <param name="tagKeyToAdd"></param> /// <param name="objToAdd"></param> public static void SimpleAddToTagGameObjectMap(Dictionary <string, List <string> > tagMap, string tagKeyToAdd, GameObject objToAdd) { PositionRotationScale prs = new PositionRotationScale(); prs.position = objToAdd.transform.position; prs.rotation = objToAdd.transform.rotation; prs.scaler = objToAdd.transform.localScale; string val_string = prs.position.ToString() + ":" + prs.rotation.ToString() + ":" + prs.scaler.ToString(); if (!tagMap.ContainsKey(tagKeyToAdd)) { List <string> prsList = new List <string>(); prsList.Add(val_string); tagMap.Add(tagKeyToAdd, prsList); } else { tagMap[tagKeyToAdd].Add(val_string); } }
/// <summary> /// Compacts an entire level into a new scriptable object using args defined in LevelInformation /// TagList is the list of tags used in this level /// </summary> public static LevelInformation SerializeLevelIntoScriptObj(LevelInformation theLevelInfo) { LevelInformation retLevelInfo = null; if (theLevelInfo != null) { Debug.Log("Serializing " + theLevelInfo.name + " into scriptable object"); Dictionary <string, List <string> > tagGameObjectMap = new Dictionary <string, List <string> >(); Dictionary <string, string> theSwtichControlDict = new Dictionary <string, string>(); if (theLevelInfo.tagList.Count < 1) { Debug.Log("Forgot to type tags in! Type the tags you used for each item to be serialized"); } foreach (string t in theLevelInfo.tagList) { GameObject[] t_arr = GameObject.FindGameObjectsWithTag(t); switch (t) { case "Switcher": //Add switcher and its position, and what tag switcher references //Serialized in form: [name:pos:rot:scale , tag I reference] //For example: {[Lever:0:0:0 , "Lever_0"]} foreach (GameObject l in t_arr) { PositionRotationScale prs = new PositionRotationScale(); prs.position = l.transform.position; prs.rotation = l.transform.rotation; prs.scaler = l.transform.localScale; string key_string = l.name + ":" + prs.position.ToString() + ":" + prs.rotation.ToString() + ":" + prs.scaler.ToString(); string tagToRef = l.GetComponent <Switch>().objTag; theSwtichControlDict.Add(key_string, tagToRef); } break; case "Wall": case "Lever_0": case "Lever_1": case "Lever_2": case "Lever_3": case "Enemy": case "Spikes_0": case "Spikes_1": //Serialized in form = [tag , pos:rot:scale] //For example: [Lever_0 , 5:5:5] foreach (GameObject go in t_arr) { SimpleAddToTagGameObjectMap(tagGameObjectMap, t, go); } break; default: break; } } retLevelInfo = ScriptableObject.CreateInstance <LevelInformation>(); retLevelInfo.tagList = theLevelInfo.tagList; retLevelInfo.tagGameObjectListJSON = JsonConvert.SerializeObject(tagGameObjectMap); //Serialize simple walls and doors with name and position information retLevelInfo.switcherControlJSON = JsonConvert.SerializeObject(theSwtichControlDict); //Serialize levers with control information //Only defined in level creation if (GameObject.FindGameObjectWithTag("PlayerStartPosition") != null) { retLevelInfo.playerStartPosition = GameObject.FindGameObjectWithTag("PlayerStartPosition").gameObject.transform.position; } else { retLevelInfo.playerStartPosition = theLevelInfo.playerStartPosition; } Debug.Log("returning following as vals in script obj"); Debug.Log(retLevelInfo.tagList); Debug.Log(retLevelInfo.tagGameObjectListJSON); Debug.Log(retLevelInfo.switcherControlJSON); Debug.Log(retLevelInfo.playerStartPosition); #if UNITY_EDITOR //Save ScriptableObject when we are done adding data EditorUtility.SetDirty(retLevelInfo); AssetDatabase.SaveAssets(); #endif } else { Debug.LogWarning("No levelInfo to build from!"); } theLevelInfo = null; return(retLevelInfo); }
/// <summary> /// Deserializes the scriptable object and instantiates each object by its tag into the level /// </summary> /// <param name="theLevelInfo"></param> /// <param name="theWallPrefab"></param> /// <param name="theDoorPrefab"></param> /// <param name="thePlayerPrefab"></param> /// <param name="theLeverPrefab"></param> /// <param name="thePosPoints"></param> public static void DeserializeFromScriptObj(LevelInformation theLevelInfo) { if (theLevelInfo != null) { Debug.Log("Deserializing " + theLevelInfo.name + " scriptable object and instantiating the following:"); //Instantiate only player GameObject go = GameObject.FindGameObjectWithTag("Player"); if (!go) { Debug.Log("The gameobject with tag = Player"); GameObject.Instantiate(StaticPlayerPrefab, theLevelInfo.playerStartPosition, StaticPlayerPrefab.transform.rotation); StaticPositionPoints.positionPoint = theLevelInfo.playerStartPosition; } //Deserialize JSON string for simple objects in scriptable object Dictionary <string, List <string> > tagGameObjMap = new Dictionary <string, List <string> >(); tagGameObjMap = JsonConvert.DeserializeObject <Dictionary <string, List <string> > >(theLevelInfo.tagGameObjectListJSON); //TagGameObjectList = Tag of prefab : list positions to instantiate the prefab if (!string.IsNullOrEmpty(theLevelInfo.tagGameObjectListJSON) || !string.IsNullOrEmpty(theLevelInfo.switcherControlJSON)) { foreach (KeyValuePair <string, List <string> > tagToObjList in tagGameObjMap) { Debug.Log("Each gameobject with tag = " + tagToObjList.Key); switch (tagToObjList.Key) { case "Wall": foreach (string s in tagToObjList.Value) { InstantiateSimplePrefab(StaticWallPrefab, tagToObjList.Key, s); } break; case "Lever_0": case "Lever_1": case "Lever_2": case "Lever_3": foreach (string s in tagToObjList.Value) { InstantiateSimplePrefab(StaticDoorPrefab, tagToObjList.Key, s); } break; case "Enemy": GameObject enemyObjCheck = GameObject.FindGameObjectWithTag("Enemy"); if (!enemyObjCheck && EnemySpawned == false) { foreach (string s in tagToObjList.Value) { GameObject enemyObj = InstantiateSimplePrefab(StaticEnemyPrefab, tagToObjList.Key, s); enemyObj.GetComponent <EnemyAI>().player = GameObject.FindGameObjectWithTag("Player"); } EnemySpawned = true; } break; case "Spikes_0": foreach (string s in tagToObjList.Value) { InstantiateSimplePrefab(StaticSpikePrefab, tagToObjList.Key, s); } break; } } //Deserialize JSON string for levers in scriptable object Dictionary <string, string> switcherControlDict = new Dictionary <string, string>(); switcherControlDict = JsonConvert.DeserializeObject <Dictionary <string, string> >(theLevelInfo.switcherControlJSON); //Instantiate Switcher, and add references obtained from json serialized object foreach (KeyValuePair <string, string> switchToRef in switcherControlDict) { string[] words = switchToRef.Key.Split(':'); string switchName = words[0]; PositionRotationScale prs = new PositionRotationScale(); prs.position = getVector3(words[1]); prs.rotation = getRotation3(words[2]); prs.scaler = getVector3(words[3]); InstantiateSwitchPrefab(switchName, prs.position, prs.rotation, prs.scaler, switchToRef.Value); } } } }