ModelMgr.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using LuaInterface;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class ModelMgr : SingletonMono<ModelMgr>
  6. {
  7. private GameObject modelRoot;
  8. public GameObject ModelRoot
  9. {
  10. get { return modelRoot; }
  11. }
  12. private LuaTable luaTable;
  13. private Action<LuaTable, List<GameObject>, object[]> OnLoadedCompleted;
  14. private object[] param;
  15. public override void InitMgr()
  16. {
  17. modelRoot = GameObject.Find("ModelRoot");
  18. if (modelRoot == null)
  19. {
  20. modelRoot = new GameObject("ModelRoot");
  21. }
  22. DontDestroyOnLoad(modelRoot);
  23. }
  24. public void CreateModel(int[] ids, LuaTable table, Action<LuaTable, List<GameObject>, object[]> cb, params object[] param)
  25. {
  26. string assetPath = Constants.ModelPath;
  27. string[] assetNames = new string[ids.Length];
  28. for (int i = 0; i < ids.Length; ++i)
  29. {
  30. string name = ConfigMgr.Instance.getValue(ids[i], "AvatarPrefab", "AvatarCfg");
  31. assetNames[i] = name;
  32. }
  33. luaTable = table;
  34. OnLoadedCompleted = cb;
  35. this.param = param;
  36. ResourceMgr.Instance.LoadAsset<List<GameObject>>(OnLoadModelCompleted, assetPath, assetNames);
  37. }
  38. void OnLoadModelCompleted(List<GameObject> prefabs, string assetPath, params string[] assetName)
  39. {
  40. List<GameObject> list = new List<GameObject>();
  41. for (int i = 0; i < assetName.Length; ++i)
  42. {
  43. GameObject go = GameObject.Instantiate(prefabs[i]);
  44. go.transform.SetParent(modelRoot.transform);
  45. go.transform.localPosition = Vector3.zero;
  46. list.Add(go);
  47. }
  48. if (OnLoadedCompleted != null)
  49. {
  50. if (luaTable != null)
  51. OnLoadedCompleted(luaTable, list, param);
  52. OnLoadedCompleted = null;
  53. }
  54. luaTable = null;
  55. param = null;
  56. }
  57. }