/// <summary> /// 创建一个实体 /// </summary> /// <typeparam name="T">驱动脚本</typeparam> /// <param name="type">实体类型</param> /// <param name="assettype">资源类型</param> /// <param name="parent">父节点物体</param> /// <param name="name">实体名称</param> /// <returns>实体ID</returns> public long CreateNewEntity <T>(Define.EntityType type, Define.ModelType assettype, GameObject parent = null, string name = "") where T : Base.BaseMotor, new() { long id = -1; AssetMgr.GetInstance().LoadAsset(PathMgr.GetInstance().GetPath(assettype), true, true, () => { if (AssetMgr.GetInstance().GetAsset() != null) { GameObject go; go = GameObject.Instantiate(AssetMgr.GetInstance().GetAsset(), parent == null ? EntityRoot.transform : parent.transform) as GameObject; id = go.GetHashCode() >= 0 ? go.GetHashCode() * 10 : -go.GetHashCode() * 10 + 1; string goname = name == "" ? (id + "_" + assettype.ToString()) : name; go.name = goname; go.AddComponent(typeof(T)); go.GetComponent <T>().InitEntity(id, goname, type); EntityList.Add(id, go.GetComponent <T>()); } else { Debug.LogError("Create Failed!"); return; } }); return(id); }
/// <summary> /// 资源加载管理器 /// </summary> /// <returns></returns> public static AssetMgr GetInstance() { if (_instance == null) { _instance = new AssetMgr(); } return(_instance); }
/// <summary> /// 删除声音物体 /// </summary> /// <param name="type"></param> public void DeleteSound(SoundType type) { if (SoundList.ContainsKey(type)) { StopSound(type); GameObject.Destroy(SoundList[type].gameObject); AssetMgr.GetInstance().CleanAssetPool(""); SoundList.Remove(type); } }
/// <summary> /// 读取配置信息 /// </summary> /// <typeparam name="N">主键类型</typeparam> /// <typeparam name="T">配置表数据</typeparam> /// <param name="type">配置类型</param> /// <returns></returns> public Dictionary <N, T> GetConfigItems <N, T>(DataType type) where T : Base.BaseData, new() { Dictionary <N, T> list = new Dictionary <N, T>(); string[] data = new string[0]; AssetMgr.GetInstance().LoadAsset(PathMgr.GetInstance().GetPath(type), false, false, () => { if (AssetMgr.GetInstance().GetAsset() == null) { return; } data = AssetMgr.GetInstance().GetAsset().ToString().Split('\n'); for (int i = 0; i < data.Length; i++) { data[i] = data[i].Replace("\r", ""); } }); if (data.Length == 0) { return(null); } else if (data.Length == 1) { return(list); } else { for (int i = 1; i < data.Length; i++) { if (string.IsNullOrEmpty(data[i]) || data[i].StartsWith("#")) { continue; } T temp = new T(); temp.ReadData(data[i]); object key = null; if (typeof(N) == typeof(int)) { key = int.Parse(data[i].Split(',')[0]); } else if (typeof(N) == typeof(long)) { key = long.Parse(data[i].Split(',')[0]); } else { key = data[i].Split(',')[0]; } list.Add((N)key, temp); } return(list); } }
/// <summary> /// 清除所有声音 /// </summary> public void DeleteAllSound() { var e = SoundList.GetEnumerator(); while (e.MoveNext()) { e.Current.Value.Stop(); GameObject.Destroy(e.Current.Value.gameObject); AssetMgr.GetInstance().CleanAssetPool(""); } e.Dispose(); SoundList.Clear(); }
/// <summary> /// 创建声音 /// </summary> /// <param name="type">声音类型</param> /// <param name="isplay">是否立即播放</param> /// <param name="isloop">是否循环播放</param> /// <param name="volume">音量</param> public void CreateSound(SoundType type, bool isplay = false, bool isloop = false, float volume = 1.0f) { if (!HasCreate(type)) { GameObject temp = new GameObject(type.ToString()); temp.transform.parent = SoundRoot.transform; AudioSource tempsource = temp.AddComponent <AudioSource>(); AssetMgr.GetInstance().LoadAsset("", true, true, () => { tempsource.clip = (AudioClip)AssetMgr.GetInstance().GetAsset(); }); tempsource.volume = Volume * volume; tempsource.Stop(); SoundList.Add(type, tempsource); if (isplay) { PlaySound(type); } } }
/// <summary> /// 打开界面 /// </summary> /// <param name="type">界面类型</param> /// <param name="funtion">完成后回调</param> public void ShowWin(UIType type, Action funtion = null) { if (!HasWinOpen(type)) { AssetMgr.GetInstance().LoadAsset(PathMgr.GetInstance().GetPath(type), true, true, () => { var win = GameObject.Instantiate(AssetMgr.GetInstance().GetAsset(), UIRoot.transform); WinList.Add(type, ((GameObject)win).GetComponent <BaseWindow>()); UpUI = type; }); } else { WinList[type].gameObject.SetActive(true); } if (funtion != null) { funtion(); } }