public static YieldInstruction Process(GameObject owner, string workName, float rate, Func <int> step) { // Find or create Amortizer component. Needed to run coroutine Amortizer amortizer = owner.GetComponent <Amortizer>(); if (amortizer == null) { amortizer = owner.AddComponent <Amortizer>(); } return(amortizer.Process(workName, rate, step)); }
// Cache instances for the attributes which requested it IEnumerator CoCacheInstancesLoop() { while (true) { // Get a list of the types List <Type> types = new List <Type>(m_instances.Keys); List <Type> .Enumerator iter = types.GetEnumerator(); // Use an amortized task since this could be somewhat expensive //Debug.Log("About to process..."); YieldInstruction result = Amortizer.Process(gameObject, "CoCacheInstances", 2f, () => { if (iter.MoveNext()) { m_instances[iter.Current] = GameObject.FindObjectsOfType(iter.Current); //Debug.Log("Found " + m_instances[iter.Current].Length + " instances for " + iter.Current); // This may kind of estimate the amount of actual work being done. Maybe. return(m_allDeclaringTypes.Count + m_instances[iter.Current].Length); } //Debug.Log("Iter finished"); return(-1); }); yield return(result); // Apply cached instances to the attributes foreach (KeyValuePair <Type, AttributeCacheBase> pair in m_attributeCaches) { pair.Value.UpdateInstances(); } //Debug.Log("Waiting for 1 seconds"); yield return(new WaitForSeconds(1f)); } }