ModelManager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class ModelManager : Singleton<ModelManager>
  7. {
  8. // public Dictionary<string, ModelItem> modellist = new Dictionary<string, ModelItem>();
  9. public Dictionary<int, ModelList> modellist = new Dictionary<int, ModelList>();
  10. public GameObject materialLibrary;
  11. public List<ModelList> Models;
  12. public void init(string message)
  13. {
  14. GameManager.Instance.text.text = " 获取素材列表";
  15. JObject jObject = JObject.Parse(message);
  16. string data = jObject["data"].ToString();
  17. JObject dataObject = JObject.Parse(data);
  18. string single = dataObject["single"].ToString();
  19. string library = dataObject["library"].ToString();
  20. string combination = dataObject["combination"].ToString();
  21. materialLibrary = new GameObject("MaterailLibrary");
  22. // 把三个素材库数据转成素材列表
  23. AddDicModelList(single);
  24. AddDicModelList(library);
  25. AddDicModelList(combination);
  26. //ModelList model = GetModelList(687);
  27. //model.name = "AAAAAAAAA";
  28. //Debug.Log(" New ModelList" + model.name + " Old ModelList " + modellist[687].name);
  29. }
  30. private void AddDicModelList(string message)
  31. {
  32. Debug.Log("AddDicModelList=1==>"+message);
  33. if (!string.IsNullOrWhiteSpace(message))
  34. {
  35. Models = JsonConvert.DeserializeObject<List<ModelList>>(message);
  36. int len = Models.Count;
  37. for (int i = 0; i < len; i++)
  38. {
  39. if (Models.Count - 1 < len && (Models[i].type == 0))
  40. Models.Remove(Models[i]);
  41. }
  42. Debug.Log(Models.Count + " AddDicModelList " + message);
  43. for (int i = 0; i < Models.Count; i++)
  44. {
  45. modellist.Add(Models[i].id, Models[i]);
  46. StartModelList(Models[i], materialLibrary);
  47. // DownLoadModel(ModelList[i]);
  48. }
  49. }
  50. }
  51. /// <summary>
  52. /// 初始化 ModelList , 根据ModelList生成组合素材
  53. /// </summary>
  54. /// <param name="modelList"></param>
  55. /// <param name="parent"> </param>
  56. public void StartModelList( ModelList modelList, GameObject parent )
  57. {
  58. modelList.materalLibrary = parent.transform;
  59. for (int j = 0; j < modelList.materialList.Count; j++)
  60. {
  61. modelList.materialList[j].updateTime = modelList.updateTime;
  62. Debug.Log("modelList.Model==>"+ modelList.Model.name);
  63. modelList.materialList[j].init(modelList.Model);
  64. // DownLoadModel(modelList);
  65. }
  66. }
  67. /// <summary>
  68. /// 从素材库中查找指定素材
  69. /// </summary>
  70. /// <param name="id"> 素材ID </param>
  71. /// <returns></returns>
  72. public ModelList GetModelList(int id)
  73. {
  74. ModelList ml = (ModelList)modellist[id].Clone();
  75. if (modellist.ContainsKey(id))
  76. return ml;
  77. else
  78. return null;
  79. }
  80. private void DownLoadModel(ModelList list)
  81. {
  82. for (int i = 0; i < list.materialList.Count; i++)
  83. {
  84. if (list.materialList[i].Model != null)
  85. Debug.Log(" 检测下载 " + list.materialList[i].name);
  86. }
  87. }
  88. }