Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
huliangjie edited this page Jul 7, 2020 · 1 revision

取值优化

  • 在性能关键时以及大量的循环逻辑中, 将需要使用的多层嵌套目标预先取出赋值使用可以提高效率
for (let i = 0; i < n; i++) {
    // 产生了 UnityEngine, Vector3, Slerp 三次 object 取值
    UnityEngine.Vector3.Slerp(a, b, i / n);
}

// 优化版: 
let Slerp = UnityEngine.Vector3.Slerp;
for (let i = 0; i < n; i++) {
    Slerp(a, b, i / n);
}
  • 大部分情况下 C# 返回的对象将产生一个临时 JS 对象绑定, 预先取出可以提高效率
class FooClass {
    x: number;
    y: number;
    z: number;

    Update() {
        // 这里 UnityEngine.Camera.main, UnityEngine.Camera.main.transform 每次访问都将分别产生一个临时的 JS 绑定对象
        UnityEngine.Camera.main.transform.localPosition = new UnityEngine.Vector3(this.x, this.y, this.z);
    }
}

// 优化版: 
let Vector3 = UnityEngine.Vector3;

class FooClass {
    x: number;
    y: number;
    z: number;
    transform: UnityEngine.Transform;

    Awake() {
        this.transform = UnityEngine.Camera.main.transform;
    }

    Update() {
        this.transform.localPosition = new Vector3(this.x, this.y, this.z);
    }
}
Clone this wiki locally