大连中石油电子沙盘
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.
 
 
 
 

156 lines
4.7 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AX.Network.Common;
/// <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;
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;
uiClonedObj.GetComponent<BaseGameObjInfo>().SetGameObjID(id);
AddEntity(id,uiClonedObj);
return uiClonedObj;
}
/// <summary>
/// 删除实体对象,并从实体容器删除引用关系
/// </summary>
/// <param name="go"></param>
public void DeleteObj(GameObject go)
{
if (SelectedObjs.selectedObj == go)
{
SelectedObjs.selectedObj = null;
}
if (SelectedObjs.gameObjs.Contains(go))
{
SelectedObjs.gameObjs.Remove(go);
}
if (SelectedObjs.characters.Contains(go))
{
SelectedObjs.characters.Remove(go);
}
if (SelectedObjs.selectedCharacters.Contains(go))
{
SelectedObjs.selectedCharacters.Remove(go);
}
var gameObjID = go.GetComponent<BaseGameObjInfo>().GameObjID;
RemoveEntity(gameObjID, go);
GameObject.Destroy(go);
}
/// <summary>
/// 创建游戏对象ID
/// </summary>
/// <returns></returns>
public long CreateObjID()
{
return (long)GUID.NewGuid(0, 0);
}
/// <summary>
/// 把游戏对象注入到实体容器内
/// </summary>
/// <param name="id"></param>
/// <param name="go"></param>
public void AddEntity(long id, GameObject go)
{
entityMap.Add(id,go);
var type = go.GetComponent<BaseGameObjInfo>().gameObjType;
var entities = default(IEnumerable<GameObject>);
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);
var type = go.GetComponent<BaseGameObjInfo>().gameObjType;
var entities = default(IEnumerable<GameObject>);
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();
}
}