【Unity 优化】 go.tag == “Player“ 和go.CompareTag(“Player“) 谁的性能更好?

新建脚本

using System.Collections;
using UnityEngine;

public class TagTesting : MonoBehaviour
{
    const int numberOfTests = 1000000;
    GameObject go;

    // Start is called before the first frame update
    void Start()
    {
        go = new GameObject();
    }

    void PerformTagEquals()
    {
        for (int i = 0; i < numberOfTests; i++)
        {
            if(go.tag == "Player")
            {

            }
        }
    }

    void PerformCompareTag()
    {
        for (int i = 0; i < numberOfTests; i++)
        {
            if(go.CompareTag("Player"))
            {

            }
        }
    }


    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            PerformTagEquals();
            PerformCompareTag();
        }
    }
}

上面脚本中,当按下空格键的时候,PerformTagEquals()和PerformCompareTag()会执行,在各自的方法内部执行1000000此循环,我们来看一下这两种比较方式的耗时情况。

在Unity中场景中,新建一个空GameObject,将脚本挂载上去,运行。

打开Profiler, 按下空格键,然后暂停游戏,下面是Profiler的截图:
在这里插入图片描述
我们可以看到:

  • PerformTagEquals() 耗时483.53毫秒,而且产生了36.2MB的GC
  • PerformCompareTag() 耗时158.84毫秒,而且没有产生GC
总结

根据tag来查找游戏对象是开发种常用的方法,从上面的测试种可以看出,应该使用Unity提供的CompareTag方法来查找,不产生GC,性能更好。

public void Move() { int stt = sun; scoreText.text = stt.ToString(); if (Input.GetKey(KeyCode.W)) { direction = 0; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[2]; this.gameObject.transform.Translate(Vector3.up * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.S)) { this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[0]; direction = 2; this.gameObject.transform.Translate(Vector3.down * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.D)) { direction = 3; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[1]; this.gameObject.transform.Translate(Vector3.right * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.A)) { direction = 1; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[3]; this.gameObject.transform.Translate(Vector3.left * speed * Time.deltaTime); } // 播放脚步声 if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) { if (!audioSource.isPlaying) { audioSource.clip = footstepSound; audioSource.loop = true; audioSource.Play(); } } else { audioSource.Stop(); } // 攻击 if (Input.GetMouseButtonDown(0)) { // 处理鼠标左键点击事件 GameObject ins = Instantiate(bulletPrefab); Vector2 clickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); ins.GetComponent<Blogs>().clickPosition = clickPosition; if (direction == 0) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y + 0.15f); } else if (direction == 2) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y - 0.15f); } else if (direction == 3) { ins.transform.position = new Vector3(this.gameObject.transform.position.x + 0.15f, this.gameObject.transform.position.y); } else if (direction == 1) { ins.transform.position = new Vector3(this.gameObject.transform.position.x - 0.15f, this.gameObject.transform.position.y); } } }public void OnCollisionEnter2D(Collision2D collision) { if(collision.gameObject.tag == "GoldCoin") { Destroy(collision.gameObject); sun = sun + 1; if (!audioSource.isPlaying) { audioSource.clip = shique; audioSource.loop = true; audioSource.Play(); } } else { audioSource.Stop(); } //判断是否遭受攻击 if (collision.gameObject.tag == "zhi") { Destroy(collision.gameObject); hp = hp - 1; } // 检测碰撞物体的标签 if (collision.gameObject.tag == "zhi2") { // 忽略该碰撞 Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>()); } }优化代码
05-18
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity游戏资源学习站

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值