コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】GC Alloc が発生しない Stack を使用できる「StackList」紹介

概要

「StackList」を Unity プロジェクトに導入することで
GC Alloc が発生しない Stack を使用できるようになります

使用例

using Hont;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling;

public class Example : MonoBehaviour
{
    private void Update()
    {
        Profiler.BeginSample( "# StackList" );

        var stackList = new StackList<Vector3>();
        stackList.Add( new Vector3( 1, 0, 0 ) );
        stackList.Add( new Vector3( 2, 0, 0 ) );
        stackList.Add( new Vector3( 3, 0, 0 ) );
        stackList.Add( new Vector3( 4, 0, 0 ) );

        Profiler.EndSample();

        Profiler.BeginSample( "# Stack" );

        var stack = new Stack<Vector3>();
        stack.Push( new Vector3( 1, 0, 0 ) );
        stack.Push( new Vector3( 2, 0, 0 ) );
        stack.Push( new Vector3( 3, 0, 0 ) );
        stack.Push( new Vector3( 4, 0, 0 ) );

        Profiler.EndSample();
    }
}

f:id:baba_s:20190503141708p:plain