Skip to content

Instantly share code, notes, and snippets.

@baobao
Created March 2, 2025 23:42
Show Gist options
  • Save baobao/4d5b45c6a82ef1cacf0808ef400a03c5 to your computer and use it in GitHub Desktop.
Save baobao/4d5b45c6a82ef1cacf0808ef400a03c5 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling;
public enum TransformAccessType
{
transformAccess,
GetComponentAccess,
CachedTransform,
}
public class ComponentPerformanceTest : MonoBehaviour
{
// インスペクタから切り替えて負荷確認
public TransformAccessType type;
Transform t;
void Update ()
{
if (t == null)
{
t = transform;
}
Profiler.BeginSample ("### Check Transform ###");
for (int i = 0; i < 10000; i++) {
if (type == TransformAccessType.CachedTransform)
{
// Transformをキャッシュして使用
t.localPosition = Vector3.zero;
}
else if (type == TransformAccessType.GetComponentAccess)
{
// GetComponentを使用
GetComponent<Transform> ().localPosition = Vector3.zero;
}
else
{
// transformアクセス
transform.localPosition = Vector3.zero;
}
}
Profiler.EndSample ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment