54 lines
2.2 KiB
C#
54 lines
2.2 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace MSCO
|
|
{
|
|
/// <summary>
|
|
/// Gemeinsame Liste aller PlayMaker-FSMs der Szene. FindObjectsOfType ist teuer (tausende FSMs in MSC);
|
|
/// statt dass Türen-, Fahrzeug-, Wetter- und Montage-Sync jeweils selbst suchen, wird einmal gesucht
|
|
/// und das Ergebnis eine Weile geteilt.
|
|
/// </summary>
|
|
public static class SceneCache
|
|
{
|
|
static PlayMakerFSM[] fsms = new PlayMakerFSM[0];
|
|
static float builtAt = -1000f;
|
|
static int builtForLevel = -1;
|
|
|
|
static Rigidbody[] bodies = new Rigidbody[0];
|
|
static float bodiesAt = -1000f;
|
|
static int bodiesForLevel = -1;
|
|
|
|
/// <summary>
|
|
/// Alle Rigidbodies; höchstens maxAge Sekunden alt (Objekt-Sync und Verkehr teilen sich die teure Suche).
|
|
/// Einträge können inzwischen zerstört sein (== null prüfen).
|
|
/// </summary>
|
|
public static Rigidbody[] Rigidbodies(float maxAge)
|
|
{
|
|
if (Time.realtimeSinceStartup - bodiesAt > maxAge || bodiesForLevel != Application.loadedLevel)
|
|
{
|
|
Object[] found = Object.FindObjectsOfType(typeof(Rigidbody));
|
|
bodies = new Rigidbody[found.Length];
|
|
for (int i = 0; i < found.Length; i++) bodies[i] = (Rigidbody)found[i];
|
|
bodiesAt = Time.realtimeSinceStartup;
|
|
bodiesForLevel = Application.loadedLevel;
|
|
}
|
|
return bodies;
|
|
}
|
|
|
|
/// <summary>Alle FSMs; höchstens maxAge Sekunden alt. Einträge können inzwischen zerstört sein (== null prüfen).</summary>
|
|
public static PlayMakerFSM[] Fsms(float maxAge)
|
|
{
|
|
if (Time.realtimeSinceStartup - builtAt > maxAge || builtForLevel != Application.loadedLevel)
|
|
{
|
|
Object[] found = Object.FindObjectsOfType(typeof(PlayMakerFSM));
|
|
var list = new List<PlayMakerFSM>(found.Length);
|
|
foreach (Object o in found) list.Add((PlayMakerFSM)o);
|
|
fsms = list.ToArray();
|
|
builtAt = Time.realtimeSinceStartup;
|
|
builtForLevel = Application.loadedLevel;
|
|
}
|
|
return fsms;
|
|
}
|
|
}
|
|
}
|