Last active
April 13, 2017 09:01
-
-
Save inoto/6bb5affc3172eb2bd4cf047a759abfec to your computer and use it in GitHub Desktop.
Character customizer ingame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class Customizer : MonoBehaviour | |
{ | |
public GameObject Panel; | |
public GameObject Button; | |
GameObject[] current = new GameObject[3]; | |
// Use this for initialization | |
void Start () | |
{ | |
GameObject inventory = GameObject.Find("inventory"); | |
// getting all data as string | |
string file = JsonReader.LoadJsonAsResource("data.json"); | |
// getting all data as structures | |
IncomingData data = IncomingData.CreateFromJSON(file); | |
// array for Slots | |
for (int i = 1; i < data.slots.Length;) | |
{ | |
GameObject panel = Instantiate(Panel, transform.position, Quaternion.identity) as GameObject; | |
panel.transform.SetParent(inventory.transform, false); | |
// array for Items | |
for (int k = 0; k < data.items.Length; k++) | |
{ | |
if (data.items[k].slot == data.slots[i].id) | |
{ | |
// make only start items active | |
GameObject obj = GameObject.Find(data.items[k].name); | |
if (obj) | |
{ | |
if (data.items[k].id != data.slots[i].start) | |
obj.SetActive(false); | |
else | |
current[i] = obj; | |
} | |
GameObject button = Instantiate(Button, transform.position, Quaternion.identity) as GameObject; | |
RectTransform[] content = panel.GetComponentsInChildren<RectTransform>(); | |
button.transform.SetParent(content[2].transform, false); | |
// create additional variable to use in lambda | |
int slot = data.items[k].slot-1; | |
button.GetComponent<Button>().onClick.AddListener(() => ButtonClicked(obj, slot)); | |
// change button text | |
Text[] text = button.gameObject.GetComponentsInChildren<Text>(); | |
text[0].text = data.items[k].name; | |
} | |
} | |
// workaround to swap Top and Pants | |
// TODO: look into SetSiblingIndex | |
if (i == 1) | |
i = 0; | |
else if (i == 0) | |
i = 2; | |
else | |
i++; | |
} | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
transform.Rotate(new Vector3(0,Time.deltaTime*30,0)); | |
} | |
void ButtonClicked(GameObject obj, int slot) | |
{ | |
if (obj && (obj != current[slot])) | |
{ | |
current[slot].SetActive(false); | |
current[slot] = obj; | |
current[slot].SetActive(true); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[System.Serializable] | |
public class IncomingData | |
{ | |
public Items[] items; | |
public Slots[] slots; | |
public static IncomingData CreateFromJSON(string json) | |
{ | |
return JsonUtility.FromJson<IncomingData>(json); | |
} | |
} | |
[System.Serializable] | |
public class Items | |
{ | |
public int id; | |
public int slot; | |
public string name; | |
public int sort; | |
} | |
[System.Serializable] | |
public class Slots | |
{ | |
public int id; | |
public string name; | |
public int start; | |
} | |
public class JsonReader : MonoBehaviour | |
{ | |
public static string LoadJsonAsResource(string path) | |
{ | |
string filePath = path.Replace(".json", ""); | |
TextAsset jsonFile = Resources.Load<TextAsset>(filePath); | |
return jsonFile.text; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment