public virtual void Load(StringReaderEx str, object obj) { Type t = obj.GetType(); var fields = t.GetFields(getFieldFlags).Where(f => f.GetCustomAttributes(typeof(StoreThis), true).Length > 0).ToArray(); int len = str.ReadLineInt(); if (fields.Length != len) { throw new UnityException("Incorrect save file"); } for (int i = 0; i < len; i++) { string fieldName = str.ReadLine(); string fieldData = str.ReadLine(); //Debug.Log(" > Loading field "+fieldName); FieldInfo finfo = fields.Where(f => f.Name == fieldName).First(); if (finfo == null) { continue; } StoreBase.LoadField(finfo, obj, fieldData); } }
public static void LoadObject(StringReaderEx str, GameObject obj) { Component[] components = obj.GetComponents <Component>(); Dictionary <string, Component> byName = new Dictionary <string, Component>(); foreach (Component c in components) { byName[c.GetType().Name] = c; } bool isActive = str.ReadLineBool(); int len = str.ReadLineInt(); for (int i = 0; i < len; i++) { string name = str.ReadLine(); bool enabled = str.ReadLineBool(); if (byName.ContainsKey(name)) { Component c = byName[name]; if (c is Behaviour) { (c as Behaviour).enabled = enabled; } LoadComponent(str, c); //LoadClass<Component>(str,c); } } obj.SetActive(isActive); }
void Update() { if (!PlayerPrefs.HasKey(CheckpointName)) { GameObject.Destroy(gameObject); return; } try { StringReaderEx str = new StringReaderEx(PlayerPrefs.GetString(CheckpointName)); int enemiesCount = str.ReadLineInt(); Debug.Log("Load: enemies = " + enemiesCount); for (int i = 0; i < enemiesCount; i++) { string prefabName = str.ReadLine(); string path = prefabName; GameObject prefab = (GameObject)Resources.Load(path, typeof(GameObject)); Debug.Log("path: " + path + ", val=" + (prefab == null)); EnemyShipController ship = ((GameObject)GameObject.Instantiate(prefab)).GetComponent <EnemyShipController>(); ship.PrefabName = prefabName; //ship.transform.position = str.ReadLineVector3(); //LoadObject(str,ship.gameObject); ObjectSerializer.LoadObject(str, ship.gameObject); //LoadClass<EnemyShipController>(str,ship); //EnemyShipController enemy = GameObject.Instantiate() } PlanetController planet = GameObject.FindGameObjectWithTag("Planet").GetComponent <PlanetController>(); ObjectSerializer.LoadObject(str, planet.gameObject); ObjectSerializer.LoadObject(str, planet.Gun.gameObject); UIController ui = GameObject.Find("UICanvas").GetComponent <UIController>();; ObjectSerializer.LoadComponent(str, ui.HpBar); ObjectSerializer.LoadComponent(str, ui.Score); ObjectSerializer.LoadComponent(str, planet.Multiplier.Indicator); EnemyGeneratorController eg = GameObject.Find("EnemyGenerator").GetComponent <EnemyGeneratorController>(); ObjectSerializer.LoadComponent(str, eg); StageController[] stages = eg.GetComponentsInChildren <StageController>(true); foreach (StageController s in stages) { ObjectSerializer.LoadObject(str, s.gameObject); } } catch (Exception e) { Debug.LogError("Exception while trying to load level: " + e.ToString()); } GameObject.Destroy(gameObject); }
public override void Load(StringReaderEx str, object obj) { Animator a = obj as Animator; a.Play(str.ReadLineInt(), 0, str.ReadLineFloat()); }