Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created January 31, 2021 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/9529c5c51778720f5bede6253a4f1e71 to your computer and use it in GitHub Desktop.
Save tsubaki/9529c5c51778720f5bede6253a4f1e71 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.LowLevel;
using System;
public class IDManager
{
Queue<int> restoreID;
int[] ID2Data;
int[] data2ID;
public int count { get; private set; }
int idIndex;
public void Initialize(int capacity)
{
count = 0;
idIndex = 0;
ID2Data = new int[capacity];
data2ID = new int[capacity];
restoreID = new Queue<int>(capacity);
}
public int GetDataIndex(int id) => ID2Data[id];
public int GetNewID()
{
var id = 0;
if (restoreID.Count != 0)
{
id = restoreID.Dequeue();
ID2Data[id] = count;
data2ID[count] = id;
}
else
{
id = idIndex;
ID2Data[id] = count;
data2ID[count] = id;
idIndex += 1;
}
count++;
return id;
}
public void RestoreID(int id)
{
count--;
var removeDataIndex = ID2Data[id];
var latestIndex = count;
var newTargetIndex = data2ID[latestIndex];
data2ID[removeDataIndex] = data2ID[latestIndex];
ID2Data[newTargetIndex] = removeDataIndex;
if ( count != id )
{
restoreID.Enqueue(id);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.LowLevel;
using System;
public static class PlayerLoopExtensions
{
public static void Add(this PlayerLoopSystem current, PlayerLoopSystem newLoopSystem, Type order)
{
var updateIndex = SubsystemIndex(current, order);
var updateSubSystem = current.subSystemList[updateIndex];
var updateSystems = new List<PlayerLoopSystem>(updateSubSystem.subSystemList);
updateSystems.Add(newLoopSystem);
updateSubSystem.subSystemList = updateSystems.ToArray();
current.subSystemList[updateIndex] = updateSubSystem;
}
public static void Remove(this PlayerLoopSystem current, PlayerLoopSystem loopSystem, Type order)
{
var updateIndex = SubsystemIndex(current, order);
var updateSubSystem = current.subSystemList[updateIndex];
var updateSystems = new List<PlayerLoopSystem>(updateSubSystem.subSystemList);
updateSystems.Remove(loopSystem);
updateSubSystem.subSystemList = updateSystems.ToArray();
current.subSystemList[updateIndex] = updateSubSystem;
}
static int SubsystemIndex(PlayerLoopSystem current, Type order) => Array.FindIndex(current.subSystemList, c => c.type == order);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment