DataTypeManager.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using LitJson;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class DataTypeManager : MonoBehaviour
  7. {
  8. List<GameObject> objlist=new List<GameObject>();
  9. public void init(JsonData data)
  10. {
  11. int max =this.transform.childCount;
  12. for (int i = 0;i<objlist.Count;i++)
  13. {
  14. Destroy( objlist[i]);
  15. }
  16. float total =0;
  17. for (int i = 0;i<data["value"].Count;i++)
  18. {
  19. total+=float.Parse(data["value"][i]["count"].ToString());
  20. }
  21. for (int i = 0;i<data["value"].Count;i++)
  22. {
  23. GameObject go =new GameObject("item_"+i);
  24. objlist.Add(go);
  25. go.transform.parent=this.transform;
  26. go.transform.localPosition = Vector3.zero;
  27. go.transform.localScale = Vector3.one;
  28. RawImage rw = go.AddComponent<RawImage>();
  29. rw.color = GetColor(int.Parse(data["value"][i]["status"].ToString()));
  30. go.GetComponent<RectTransform>().sizeDelta = new Vector2(float.Parse(data["value"][i]["count"].ToString())/total*100f, go.GetComponent<RectTransform>().sizeDelta.y);
  31. }
  32. }
  33. public static Color GetColor(int colorid)
  34. {
  35. switch(colorid)
  36. {
  37. case 1:
  38. return Color.green;
  39. break;
  40. case 2:
  41. return Color.yellow;
  42. break;
  43. case 3:
  44. return Color.red;
  45. break;
  46. case 4:
  47. return Color.gray;
  48. break;
  49. }
  50. return Color.gray;
  51. }
  52. }