public List <GameObject> createWires(GameObject wirePrefab, GameObject sparkPrefab) { List <GameObject> wireGOs = new List <GameObject>(); foreach (KeyValuePair <int, int> entry in wires) { GameObject wire = MonoBehaviour.Instantiate(wirePrefab, Vector3.zero, wirePrefab.transform.rotation) as GameObject; wire.transform.SetParent(city.transform); WireIdentity wireIdentity = wire.GetComponent <WireIdentity>(); wireIdentity.from = city.transform.GetChild(entry.Key).gameObject; wireIdentity.to = city.transform.GetChild(entry.Value).gameObject; GameObject spark = MonoBehaviour.Instantiate(sparkPrefab, Vector3.zero, sparkPrefab.transform.rotation) as GameObject; spark.transform.SetParent(city.transform); TranslationAnimator animator = spark.GetComponent <TranslationAnimator>(); Vector3 from = city.transform.GetChild(entry.Key).position; Vector3 to = city.transform.GetChild(entry.Value).position; animator.from = new Vector3(from.x, 0.25f, from.z); animator.to = new Vector3(to.x, 0.25f, to.z); animator.addFlow(1); animator.stop(); wireIdentity.spark = spark; wire.GetComponent <ShowHideCanvas>().target = spark.GetComponentInChildren <Canvas>().gameObject; wireGOs.Add(wire); } return(wireGOs); }
public void parseJSON() { string sjson = File.ReadAllText(jsonPath); JSONObject jsonObject = new JSONObject(sjson); JSONObject frames = jsonObject.GetField("frames"); foreach (JSONObject frame in frames.list) { JSONObject moment = frame.GetField("moment"); string sMoment = moment.GetField("hour").ToString() + ":" + moment.GetField("minute").ToString(); timeUI.GetComponentInChildren <ChangeTextAnimator>().addString(sMoment); JSONObject houses = frame.GetField("houses"); for (int i = 0; i < houses.list.Count; i++) { JSONObject house = houses.list[i]; InfoPanel panel = panels[i]; // Battery percent JSONObject battery = house.GetField("battery"); float level = float.Parse(battery.GetField("level").ToString()); float capacity = float.Parse(battery.GetField("capacity").ToString()); FillImageAnimator animator = panel.battery.GetComponentsInChildren <FillImageAnimator>(true)[0]; animator.addFrame(level / capacity); // Appliances progress JSONObject appliances = house.GetField("appliances"); for (int app = 0; app < appliances.list.Count; app++) { JSONObject appliance = appliances.list[app]; float progress = float.Parse(appliance.GetField("progress").ToString()); Transform t = panel.applianceParent.transform.GetChild(app); t.GetComponentsInChildren <FillImageAnimator>(true)[0].addFrame(progress); } // Generators efficiencies JSONObject generators = house.GetField("generators"); for (int gen = 0; gen < generators.list.Count; gen++) { JSONObject generator = generators.list[gen]; float efficiency = float.Parse(generator.GetField("efficiency").ToString()); Transform t = panel.generatorParent.transform.GetChild(gen); t.GetComponentsInChildren <FillImageAnimator>(true)[0].addFrame(efficiency); } // Bid plots string plotFile = house.GetField("bid").GetField("plotFile").ToString().Replace("\"", ""); panel.bid.GetComponent <ChangeImageAnimator>().addPlot(plotFile); } JSONObject wires = frame.GetField("wires"); foreach (JSONObject wire in wires.list) { int origin = int.Parse(wire.GetField("originId").ToString()); int destination = int.Parse(wire.GetField("destinationId").ToString()); float flow = float.Parse(wire.GetField("flow").ToString()); foreach (GameObject w in wireGOs) { WireIdentity identity = w.GetComponent <WireIdentity>(); if (identity.from.transform.position == city.transform.GetChild(origin).position&& identity.to.transform.position == city.transform.GetChild(destination).position) { identity.spark.GetComponent <TranslationAnimator>().addFlow(flow); identity.GetComponent <ShowHideCanvas>().target.GetComponentInChildren <ChangeTextAnimator>().addString(Mathf.Abs(flow) + " kw"); } } } } }