public void createPanels(GameObject infoPanelPrefab, GameObject appliancePrefab, GameObject generatorPrefab, ApplianceSpriteDictionary spriteOfAppliance, GeneratorSpriteDictionary spriteOfGenerator) { string sjson = File.ReadAllText(jsonPath); JSONObject jsonObject = new JSONObject(sjson); JSONObject frame = jsonObject.GetField("frames").list[0]; JSONObject houses = frame.GetField("houses"); foreach (JSONObject house in houses.list) { int id = int.Parse(house.GetField("id").ToString()); Transform t = city.transform.GetChild(id); // Add collider and info panel BoxCollider collider = t.gameObject.AddComponent <BoxCollider>(); GameObject infoPanelGO = MonoBehaviour.Instantiate(infoPanelPrefab, t.position + (collider.size.y + 1) * Vector3.up, Quaternion.identity) as GameObject; InfoPanel infoPanel = infoPanelGO.GetComponent <InfoPanel>(); infoPanel.transform.SetParent(t); // Add show hide infopanel effect ShowHideCanvas showHide = t.gameObject.AddComponent <ShowHideCanvas>(); showHide.target = infoPanel.gameObject; // Create panel struct panels.Add(infoPanel); JSONObject appliances = house.GetField("appliances"); foreach (JSONObject appliance in appliances.list) { string type = appliance.GetField("type").ToString().Replace("\"", ""); createAppliance(infoPanel, (ApplianceType)Enum.Parse(typeof(ApplianceType), type.Trim()), appliancePrefab, spriteOfAppliance); } JSONObject generators = house.GetField("generators"); foreach (JSONObject generator in generators.list) { string type = generator.GetField("type").ToString().Replace("\"", ""); createGenerator(infoPanel, (GeneratorType)Enum.Parse(typeof(GeneratorType), type.Trim()), generatorPrefab, spriteOfGenerator); } } }
private void createAppliance(InfoPanel infoPanel, ApplianceType type, GameObject appliancePrefab, ApplianceSpriteDictionary spriteOfAppliance) { GameObject applianceGO = MonoBehaviour.Instantiate(appliancePrefab) as GameObject; applianceGO.transform.GetChild(0).GetComponent <Image>().sprite = spriteOfAppliance[type]; applianceGO.transform.SetParent(infoPanel.applianceParent.transform); applianceGO.GetComponent <RectTransform>().localScale = Vector3.one; applianceGO.GetComponent <RectTransform>().offsetMin = Vector2.zero; applianceGO.GetComponent <RectTransform>().offsetMax = Vector2.zero; applianceGO.transform.localPosition = new Vector3(applianceGO.transform.localPosition.x, applianceGO.transform.localPosition.y, 0); }