-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRendererIdAllocator.cs
58 lines (48 loc) · 1.98 KB
/
RendererIdAllocator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SubsurfaceStudios.Slightmapper.Global {
public class RendererIdAllocator : MonoBehaviour {
[SerializeField]
// if you manage to have more than 4,294,967,295 static renderers
// in one scene you should probably just write your own tool
private uint AllocatedIds = 0;
public static uint GetId() => Instance.AllocatedIds++;
private static RendererIdAllocator s_Instance;
public static RendererIdAllocator Instance
{
get {
if (!s_Instance)
// Check for an instance in the scene
s_Instance = FindObjectOfType<RendererIdAllocator>(true);
if (!s_Instance) {
// No instance in scene? Make a new instance.
s_Instance = new GameObject("RENDERER_ID_ALLOC").AddComponent<RendererIdAllocator>();
s_Instance.gameObject.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
}
return s_Instance;
}
}
public static void RegisterId(uint id, RendererId self) {
Instance.s_Index ??= new RendererId[Instance.AllocatedIds];
if (id == Index.Length) {
RendererId[] newIndex = new RendererId[Index.Length + 10];
Array.Copy(Index, newIndex, Index.Length);
Instance.s_Index = newIndex;
}
Index[id] = self;
}
public static RendererId[] Index {
get => Instance.s_Index;
set => Instance.s_Index = value;
}
public RendererId[] s_Index = null;
[ContextMenu("Force Reregister all RenderIds")]
void ForceReregisterAllIds() {
foreach(var item in FindObjectsOfType<RendererId>(true)) {
item.ForceReregisterId();
}
}
}
}