Unity - A计划(一年有效期) 扫二维码继续学习 二维码时效为半小时

(61评价)
价格: 2208.00元
关于场景中最多容纳的人物数量
克莱宝宝发起了问答2019-03-11
2
回复
332
浏览

我看在英雄联盟,史诗战争模拟器等游戏中,场景里同时存在一千个左右的人物电脑还不卡,我写的demo里场景中有将近200个敌人帧数就降低到30左右了。请问有什么优化的方法吗(下面是敌人代码,2d游戏,敌人已添加碰撞体,有动画)

public class EnemyFather:MonoBehaviour
{
    public int Hp;
    public int Damage;
    public float Speed;

    public int TotalHp;
    [HideInInspector]
    public Transform target;
    private Slider BloodSlider;
    protected Animator animator;
    private float TakeDamTimer;
    protected List<SpriteRenderer> spriteRenderers;
    private bool haveSetcolor;//把颜色设回来

    [HideInInspector]
    public float AttPerChange;//增加伤害的百分比
    [HideInInspector]
    public float InitSpeed;
    public Transform Totbuffs;//敌人的Buff
    public EnemyFather(int hp,int damage,float speed,int Energy)
    {
        Hp = hp;
        TotalHp = hp;
        Damage = damage;
        Speed = speed;
        InitSpeed = Speed;
        this.Energy = Energy;
        AttPerChange = 1;
    }
    protected virtual void Start() 
    {        
        target = TheBaby.instance.transform;
        BloodSlider = transform.Find("Canvas/BloodSlider").GetComponent<Slider>();
        animator = GetComponent<Animator>();
        UseDieEffect = CreateDieE;
        Totbuffs = transform.Find("Canvas/Buffs");

    }
    private void FixedUpdate()
    {        
        Attack();
        Move();
        DeCreaseDebuffTime();        
    }
    protected virtual void Move()
    {
        Vector3 direction = target.position - transform.position;
        if (direction.x > 0)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
        else
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
        transform.Translate(direction.normalized*Speed*Time.deltaTime);
    }
    public void TakeDamage(int dam)
    {
        dam = (int)(dam * AttPerChange);
        Hp -= dam;
        BloodSlider.value = (float)Hp / TotalHp;
        if (Hp <= 0)
        {           
            Die();
        }
        foreach(var item in spriteRenderers)
        {
            item.material.SetInt("_BeAttack", 1);
        }        
        TakeDamTimer = 0.1f;
        haveSetcolor = false;
    }
    protected virtual void OnCollisionStay2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            TheBaby.instance.TakeDamage1(Damage);
        }
    }
    protected virtual void Attack()//有的敌人可以攻击,我测试的这种没有攻击方式
    {
        
    }
    public virtual void ReBirth()
    {       
        if (BloodSlider != null)
        {
            Hp = TotalHp;
            BloodSlider.value = 1;
        }     
    }
    protected virtual void Die()
    {        
        UseDieEffect(gameObject);
        GameObject DieEff = GameManager.Instance.factroyManager.effectFactroy.GetItem("EDieEffRed");
        DieEff.transform.position = transform.position;
        GameManager.Instance.factroyManager.enemyFactroy.PushItem(gameObject.name.Substring(0, gameObject.name.Length - 7), gameObject);
        Totbuffs.SendMessage("EndBuff", SendMessageOptions.DontRequireReceiver);

    }
    private void DeCreaseDebuffTime()
    {
        if (TakeDamTimer < 0)
        {
            if (!haveSetcolor)
            {
                foreach (var item in spriteRenderers)
                {
                    item.material.SetInt("_BeAttack", 0);
                }
                haveSetcolor = true;
            }            
        }
        else
        { TakeDamTimer -= Time.deltaTime; }
    }
    //public void ReturnToInitSpeed()
    //{
    //    Speed = InitSpeed;
    //}
    void CreateDieE(GameObject enemy)
    {
       
    }
   
}

 

所有回复
发表回复
你还没有登录,请先 登录或 注册!