优化——简易的地图优化

最近需要去做下地图的优化(优化就是减低DC),写了个练手的优化方案,没有通过,因为不够灵活,而且优化程度没有九宫格划分来的地图优化来的好,所以最后就取消了。这里我就将我写的简易的优化发上来,大家看看还有什么需要改进的。

优化的想法是,以玩家作为中心画一个圈。大于这个圈的就消失,在里面的就显示。每五秒调用下这个方法。这样就能减少很多DC。

这里我贴出我的代码,还在测试的,没有完全实现这个优化的想法,各位看官将就看看。

public class ClearNextGameObject : MonoBehaviour
{
    private List<GameObject> screenObject;
    private List<float> list;
    public float Distance = 10;
    private bool Five = false;

    private void Awake()
    {
         GetGameObject();
    }

    private void AddTheR()
    {
        if (ETModel.UnitComponent.Instance.MyUnit != null)
        {
            Vector3 unit = ETModel.UnitComponent.Instance.MyUnit.Position;
            for (int i = 0; i < screenObject.Count; i++)
            {
                float r = Mathf.Sqrt(Mathf.Pow((Mathf.Abs(screenObject[i].transform.localPosition.x) - Mathf.Abs(unit.x)), 2) + Mathf.Pow((Mathf.Abs(screenObject[i].transform.localPosition.z) - Mathf.Abs(unit.z)), 2));
                list.Add(r);
            }
        }
    }
    bool IsFirst = true;
    private void Update()
    {

        if (ETModel.UnitComponent.Instance.MyUnit != null)
        {

            if (IsFirst)
            {
                AddTheR();
                IsFirst = false;
                //Debug.Log("------------------------------------   "+list.Count);
            }

            HideSomeGameObject();
        }
    }

    private void GetGameObject()
    {
        GameObject ScreenRoot = GameObject.Find("SceneRoot");

        screenObject = new List<GameObject>();
        list = new List<float>();

        GetAllChild(ScreenRoot);
        //Debug.Log("---------------"+screenObject.Count);
       
    }
    int a = 0;
    private async void HideSomeGameObject()
    {
        if (Five)
        {
            return;
        }
        Five = true;

        Vector3 unit = ETModel.UnitComponent.Instance.MyUnit.Position;
        Debug.Log("_______________" + unit.x+"  "+ unit.z);
        for (int i = 0; i < screenObject.Count; i++)
        {
            //Debug.Log(list[i]);   screenObject[i].transform.localPosition.x  + unit.x < Distance ||
            if ( screenObject[i].transform.localPosition.z + unit.z < 16|| screenObject[i].transform.localPosition.z + unit.z>125 )
                //screenObject[i].transform.localPosition.x  + unit.x < 15 || screenObject[i].transform.localPosition.x  + unit.x > 100)
            //if (list[i]>1)
            {
                screenObject[i].SetActive(false);
                a++;
               
            }
            else
            {
                screenObject[i].SetActive(true);
            }
        }
           // Debug.Log("_______________________________________________________"+a);
        Five = await IsFive();
    }
    private async ETModel.ETTask<bool> IsFive()
    {
        ETModel.TimerComponent time = ETModel.Game.Scene.GetComponent<ETModel.TimerComponent>();
        float fiveTime = 2;
        while (true)
        {
            await time.WaitAsync(100);
            fiveTime -= 0.1f;
            if(fiveTime < 0)
            {
                break;
            }
        }
        return false;
    }

  
    /// <summary>
    /// 遍历所有的物体
    /// </summary>
    /// <param name="Obj"></param>
    private void GetAllChild(GameObject Obj)
    {
        //if (times <= 3) return;
        if (Obj.GetComponent<Renderer>() == null )
        {
            for (int i = 0; i < Obj.transform.childCount; i++)
            {
                GetAllChild(Obj.transform.GetChild(i).gameObject);
            }
                //times++;
        }
        else
        {
            if(Obj.tag != "Ground" && Obj.tag != "Cube")
                screenObject.Add(Obj);
        }

    }
}

我用的框架是ET框架,非常不错,有兴趣的可以去看看。我还只是入门。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值