← 返回
效率工具 中文

Unity

Avoid common Unity mistakes — lifecycle ordering, GetComponent caching, physics timing, and Unity's fake null.
避免常见的Unity错误——生命周期顺序、GetComponent缓存、物理时机及Unity假null问题
ivangdavila
效率工具 clawhub v1.0.0 1 版本 99914.9 Key: 无需
★ 3
Stars
📥 2,288
下载
💾 368
安装
1
版本
#latest

概述

Lifecycle Order

  • Awake before Start — use Awake for self-init, Start for cross-references
  • OnEnable called before Start — but after Awake
  • Order between scripts not guaranteed — use Script Execution Order if needed
  • Awake called even if disabled — Start only when enabled

GetComponent Performance

  • GetComponent every frame is slow — cache in Awake or Start
  • GetComponentInChildren searches recursively — expensive on deep hierarchies
  • TryGetComponent returns bool — avoids null check, slightly faster
  • Use RequireComponent attribute — ensures dependency, documents requirement

Physics Timing

  • Physics in FixedUpdate, not Update — consistent regardless of framerate
  • FixedUpdate can run 0 or multiple times per frame — don't assume 1:1
  • Rigidbody.MovePosition in FixedUpdate — transform.position bypasses physics
  • Time.deltaTime in Update, Time.fixedDeltaTime in FixedUpdate — or just use deltaTime

Unity's Fake Null

  • Destroyed objects aren't truly null — == null returns true, but object exists
  • Null-conditional ?. doesn't work properly — use == null or bool conversion
  • Destroy doesn't happen immediately — object gone next frame
  • Use DestroyImmediate only in editor — causes issues in builds

Coroutines

  • StartCoroutine needs MonoBehaviour active — disabled/destroyed stops coroutines
  • yield return null waits one frame — yield return new WaitForSeconds(1) for time
  • StopCoroutine needs same method or Coroutine reference — string overload unreliable
  • Can't return values — use callbacks or set field in coroutine

Instantiate and Pooling

  • Instantiate is expensive — pool frequently created/destroyed objects
  • Instantiate(prefab, parent) sets parent — avoids extra SetParent call
  • SetActive(false) before returning to pool — not Destroy
  • Pool inactive objects under a parent — keeps hierarchy clean

Serialization

  • [SerializeField] for private fields in inspector — prefer over public
  • public fields auto-serialize — but exposes API you may not want
  • [HideInInspector] hides but still serializes — [NonSerialized] to skip entirely
  • Serialized fields keep inspector values — code defaults ignored after first serialize

ScriptableObjects

  • Data containers that live as assets — share between scenes/prefabs
  • CreateAssetMenu attribute for easy creation — right-click → Create
  • Don't modify at runtime in builds — changes not saved (except in editor)
  • Great for config, item databases — reduces prefab duplication

Common Mistakes

  • Find methods every frame — cache references
  • String comparisons for tags — use CompareTag("Enemy"), not tag == "Enemy"
  • Physics queries allocate — use NonAlloc variants: RaycastNonAlloc
  • UI anchors wrong — stretches unexpectedly on different resolutions
  • async/await without context — use UniTask or careful error handling

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-28 21:43 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

productivity

Weather

steipete
获取当前天气和预报(无需API密钥)
★ 448 📥 226,575
ai-intelligence

Self-Improving + Proactive Agent

ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,365 📥 319,343
productivity

Baidu web search

ide-rea
使用百度AI搜索引擎(BDSE)进行网络搜索。适用于获取实时信息、文档资料或研究课题。
★ 239 📥 105,912