码迷,mamicode.com
首页 > 编程语言 > 详细

Unity同屏优化【1】NPC的处理

时间:2016-01-12 06:31:55      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

跳转场景时调用:Singleton<ObjManager>.GetInstance().OnEnterScene();

 

 1 public void OnEnterScene()
 2 {
 3     this.m_IsUseAndroid = true;
 4     this.m_ShowingNPCList.Clear();
 5     this.DeleteNPCPool();
 6     this.m_ObjPools.Clear();
 7     this.m_OtherGameObjPools.Clear();
 8     this.m_ObjOtherPlayerHideList.Clear();
 9     this.m_ObjOtherPlayerShowList.Clear();
10 }

 

m_IsUseAndroid判断是否Android平台,还需要判断是否是低端Android。

m_ShowingNPCList已经显示出来的NPC。

m_DeleteNPCPool删除NPC池中对象。

m_ObjPools

m_OtherGameObjPools

m_ObjOtherPlayerHideList

m_ObjOtherPlayerShowList

if (this.m_IsUseAndroid)
{
    if (this.m_NPCGameObjectList == null)
    {
        this.m_NPCGameObjectList = new Dictionary<string, GameObject>();
    }
    if (this.m_NPCGameObjectList != null && this.m_NPCGameObjectList.ContainsKey(initData.m_ServerID.ToString()))
    {
        if (this.m_DeleteNPCList != null && this.m_DeleteNPCList.ContainsKey(initData.m_ServerID.ToString()))
        {                
        this.m_DeleteNPCList.Remove(initData.m_ServerID.ToString()); } GameObject gameObject = this.m_NPCGameObjectList[initData.m_ServerID.ToString()]; Obj_NPC component = gameObject.GetComponent<Obj_NPC>(); if (component != null) { component.PlayUnDissolve(); component.CanLogic = true; component.Init(initData); } gameObject.SetActive(true); return; } }

Android平台下,m_NPCGameObjectList,定义private Dictionary<string, GameObject> m_NPCGameObjectList;Key值是NPC的名字,Value为GameObject

存放当前场景中的NPC的数量,当要加入场景的NPC在这个Dictionary中,取出来GameObject,删除Dictionary中的引用。

 

 1 if (this.m_IsUseAndroid && this.m_NPCGameObjectList != null)
 2 {
 3     if (!this.m_NPCGameObjectList.ContainsKey(gameObject2.name) && obj_NPC.IsNeedBecameVisible())
 4     {
 5       this.m_NPCGameObjectList.Add(gameObject2.name, gameObject2);
 6     }
 7     if (this.m_NPCGameObjectList.Count > 50)
 8     {
 9         this.DeleteNPCGameObject();
10     }
11 }

 

如果缓存的NPC数量大于50,就需要执行DeleteNPCGameObject();

 1 public void DeleteNPCGameObject()
 2 {
 3     if (!this.m_IsUseAndroid)
 4     {
 5         return;
 6     }
 7     if (this.m_DeleteNPCList == null)
 8     {
 9         return;
10     }
11     List<string> list = new List<string>();
12     int num = 0;
13     foreach (KeyValuePair<string, float> current in this.m_DeleteNPCList)
14     {
15         if (num >= 8)
16         {
17             break;
18         }
19         if (this.m_NPCGameObjectList != null && this.m_NPCGameObjectList.ContainsKey(current.Key))
20         {                
         this.ReomoveObjInScene(this.m_NPCGameObjectList[current.Key], true); 21 this.m_NPCGameObjectList[current.Key] = null; 22 this.m_NPCGameObjectList.Remove(current.Key); 23 } 24 list.Add(current.Key); 25 num++; 26 } 27 for (int i = 0; i < list.Count; i++) 28 { 29 this.m_DeleteNPCList.Remove(list[i]); 30 } 31 list.Clear(); 32 list = null; 33 }

删除只删除最先加入的8个,然后将这8个NPC从m_DeleteNPCList,private Dictionary<string, float> m_DeleteNPCList;Key之为NPC名字,Value为Time.time。m_DeleteNPCList在OnEnterScene时通过DeleteNPCPool时Clear()。

 

 1 public void DeleteNPCPool()
 2 {
 3     if (this.m_NPCGameObjectList != null)
 4     {
 5         this.m_NPCGameObjectList.Clear();
 6         this.m_NPCGameObjectList = null;
 7     }
 8     if (this.m_DeleteNPCList != null)
 9     {
10         this.m_DeleteNPCList.Clear();
11         this.m_DeleteNPCList = null;
12     }
13 }

 

Unity同屏优化【1】NPC的处理

标签:

原文地址:http://www.cnblogs.com/yuxk/p/5123108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!