You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
5.4 KiB
186 lines
5.4 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
|
|
/// <summary> |
|
/// 实体管理器 |
|
/// </summary> |
|
public class EntitiesManager |
|
{ |
|
public static readonly EntitiesManager Instance = new EntitiesManager(); |
|
|
|
//private long userId = 6431758821830431551;//用户id |
|
//private long num = 0;//自增数 |
|
|
|
private Dictionary<long, GameObject> entityMap = new Dictionary<long, GameObject>(); |
|
private Dictionary<CloneObjType, IEnumerable<GameObject>> typeMap = new Dictionary<CloneObjType, IEnumerable<GameObject>>(); |
|
|
|
/// <summary> |
|
/// 创建游戏对象,并注入到实体容器 |
|
/// </summary> |
|
/// <param name="prefab"></param> |
|
/// <param name="clonePos"></param> |
|
/// <param name="parent"></param> |
|
/// <param name="id"></param> |
|
/// <returns></returns> |
|
public GameObject CreateObj(GameObject prefab, Vector3 clonePos, Transform parent, long id) |
|
{ |
|
var clonedObj = GameObject.Instantiate(prefab,clonePos,Quaternion.identity,parent) as GameObject; |
|
if (!clonedObj.GetComponent<UIdSystem>()) |
|
{ |
|
clonedObj.AddComponent<UIdSystem>().ReassignId((ulong)CurrentUserInfo.mySelf.Id, true); |
|
} |
|
clonedObj.GetComponent<BaseGameObjInfo>().SetGameObjID(id); |
|
AddEntity(id,clonedObj); |
|
|
|
return clonedObj; |
|
} |
|
|
|
/// <summary> |
|
/// 创建UI对象,并注入到实体容器 |
|
/// </summary> |
|
/// <param name="prefab"></param> |
|
/// <param name="parent"></param> |
|
/// <param name="id"></param> |
|
/// <returns></returns> |
|
public GameObject CreateObj(GameObject prefab, Transform parent, long id) |
|
{ |
|
var uiClonedObj = GameObject.Instantiate(prefab, parent) as GameObject; |
|
if (!uiClonedObj.GetComponent<UIdSystem>()) |
|
{ |
|
uiClonedObj.AddComponent<UIdSystem>(); |
|
} |
|
uiClonedObj.GetComponent<BaseGameObjInfo>().SetGameObjID(id); |
|
AddEntity(id,uiClonedObj); |
|
|
|
return uiClonedObj; |
|
} |
|
|
|
/// <summary> |
|
/// 删除实体对象,并从实体容器删除引用关系 |
|
/// </summary> |
|
/// <param name="go"></param> |
|
public void DeleteObj(GameObject go) |
|
{ |
|
BaseGameObjInfo baseGameObjInfo = go.GetComponent<BaseGameObjInfo>(); |
|
long gameObjID; |
|
if (baseGameObjInfo) |
|
{ |
|
gameObjID = baseGameObjInfo.GameObjID; |
|
} |
|
else |
|
{ |
|
gameObjID = (long)go.GetComponent<UIdSystem>().Id; |
|
} |
|
|
|
RemoveEntity(gameObjID, go); |
|
GameObject.Destroy(go); |
|
} |
|
|
|
/// <summary> |
|
/// 创建游戏对象ID |
|
/// </summary> |
|
/// <returns></returns> |
|
public long CreateObjID(long usrId) |
|
{ |
|
//userId = CurrentUserInfo.mySelf.Id; |
|
//return userId ^ (num++); |
|
//生成全局唯一ID,避免用户id一样但num又从0开始生成重复的对象ID,例如编辑灾情库时 |
|
return (long)GUID.NewGuid(1,1) - usrId; |
|
} |
|
|
|
/// <summary> |
|
/// 把游戏对象注入到实体容器内 |
|
/// </summary> |
|
/// <param name="id"></param> |
|
/// <param name="go"></param> |
|
public void AddEntity(long id, GameObject go) |
|
{ |
|
if (!entityMap.ContainsKey(id)) |
|
{ |
|
entityMap.Add(id,go); |
|
} |
|
|
|
BaseGameObjInfo baseGameObjInfo = go.GetComponent<BaseGameObjInfo>(); |
|
CloneObjType type; |
|
IEnumerable<GameObject> entities = default(IEnumerable<GameObject>); |
|
|
|
if (baseGameObjInfo) |
|
{ |
|
type = baseGameObjInfo.gameObjType; |
|
} |
|
else |
|
{ |
|
type = CloneObjType.others; |
|
} |
|
|
|
if (typeMap.TryGetValue(type, out entities)) |
|
{ |
|
((List<GameObject>)entities).Add(go); |
|
} |
|
else |
|
{ |
|
var entityList = new List<GameObject>(); |
|
entityList.Add(go); |
|
typeMap.Add(type, entityList); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 删除实体容器中的游戏实例 |
|
/// </summary> |
|
/// <param name="id"></param> |
|
/// <param name="go"></param> |
|
public void RemoveEntity(long id, GameObject go) |
|
{ |
|
entityMap.Remove(id); |
|
|
|
BaseGameObjInfo baseGameObjInfo = go.GetComponent<BaseGameObjInfo>(); |
|
CloneObjType type; |
|
IEnumerable<GameObject> entities = default(IEnumerable<GameObject>); |
|
|
|
if (baseGameObjInfo) |
|
{ |
|
type = go.GetComponent<BaseGameObjInfo>().gameObjType; |
|
} |
|
else |
|
{ |
|
type = CloneObjType.others; |
|
} |
|
|
|
if (typeMap.TryGetValue(type, out entities)) |
|
{ |
|
((List<GameObject>)entities).Remove(go); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 根据游戏对象ID查找某个游戏对象 |
|
/// </summary> |
|
/// <param name="id"></param> |
|
/// <returns></returns> |
|
public GameObject GetEntityByID(long id) |
|
{ |
|
GameObject go = null; |
|
entityMap.TryGetValue(id,out go);//没找到返回null,否则返回go |
|
return go; |
|
} |
|
|
|
/// <summary> |
|
/// 根据类型查找某一类游戏对象的集合 |
|
/// </summary> |
|
/// <param name="type"></param> |
|
/// <returns></returns> |
|
public IEnumerable<GameObject> GetEntitiesByType(CloneObjType type) |
|
{ |
|
var entities = default(IEnumerable<GameObject>); |
|
typeMap.TryGetValue(type,out entities);//没找到返回没有元数据的默认集合entities,否则返回带有元数据的集合entities |
|
return entities; |
|
} |
|
|
|
public void Reset() |
|
{ |
|
entityMap.Clear(); |
|
typeMap.Clear(); |
|
} |
|
}
|
|
|