/** * Runs all active coroutines until their next yield. Caller must provide * the current frame and time. This allows for schedulers to run under * frame and time regimes other than the Unity's main game loop. */ public void UpdateAllCoroutines (int frame, float time) { currentFrame = frame; currentTime = time; CoroutineNode coroutine = this.first; while (coroutine != null) { // store listNext before coroutine finishes and is removed from the list CoroutineNode listNext = coroutine.listNext; if (coroutine.waitForFrame > 0 && frame >= coroutine.waitForFrame) { coroutine.waitForFrame = -1; UpdateCoroutine (coroutine); } else if (coroutine.waitForTime > 0.0f && time >= coroutine.waitForTime) { coroutine.waitForTime = -1.0f; UpdateCoroutine (coroutine); } else if (coroutine.waitForCoroutine != null && coroutine.waitForCoroutine.finished) { coroutine.waitForCoroutine = null; UpdateCoroutine (coroutine); } else if (coroutine.waitForFrame == -1 && coroutine.waitForTime == -1.0f && coroutine.waitForCoroutine == null) { // initial update UpdateCoroutine (coroutine); } coroutine = listNext; } }
private void RemoveCoroutine(CoroutineNode coroutine) { if (this.first == coroutine) { // remove first this.first = coroutine.listNext; } else { // not head of list if (coroutine.listNext != null) { // remove between coroutine.listPrevious.listNext = coroutine.listNext; coroutine.listNext.listPrevious = coroutine.listPrevious; } else if (coroutine.listPrevious != null) { // and listNext is null coroutine.listPrevious.listNext = null; // remove last } } coroutine.listPrevious = null; coroutine.listNext = null; }
private CoroutineNode CreateInstance(IEnumerator coroutine, string name) { CoroutineNode node = new CoroutineNode(coroutine); activeCoroutines.Add(name, node); return(node); }
private void AddCoroutine (CoroutineNode coroutine) { if (this.first != null) { coroutine.listNext = this.first; first.listPrevious = coroutine; } first = coroutine; }
public IEnumerator ImportModel() { PiXYZImportSettings settings = target as PiXYZImportSettings; GameObject gameObject = settings.gameObject; //Find the gameobject the script is attached to Vector3 eulerAngles = new Vector3() + gameObject.transform.rotation.eulerAngles; Vector3 scale = new Vector3() + gameObject.transform.lossyScale; if (gameObject.transform.childCount > 0) { foreach (Transform child in gameObject.transform) { DestroyImmediate(child.gameObject); } } UnityEngine.Object prefab; prefab = PrefabUtility.CreateEmptyPrefab("Assets/3DModels/" + settings.settings.prefabName + ".prefab"); loader = new PiXYZ4UnityLoader(); loader.setSourceCoordinatesSystem(settings.settings.isRightHanded, settings.settings.isZUp, settings.settings.scaleFactor); double mapUV3dSize = settings.settings.mapUV ? settings.settings.mapUV3dSize : -1; loader.configure(settings.settings.orient, mapUV3dSize, settings.settings.treeProcess, settings.settings.useLods ? settings.settings.lodsMode : LODsMode.NONE, settings.settings.lodSettings, !settings.settings.splitTo16BytesIndex, settings.settings.useMergeFinalAssemblies); CoroutineNode coco = coroutineScheduler.StartCoroutine(loader.loadFileRuntime(gameObject, settings.settings.originalFilename, true, prefab)); yield return(coco); PiXYZUtils.clearProgressBar(); if (loader.getErrorMessage().Length > 0) { Debug.LogError("PiXYZAssetImporter: loader.loadfile failed"); } else { Debug.Log("Success"); } #if UNITY_EDITOR foreach (UnityEngine.Object obj in loader.loadedObject) { AssetDatabase.AddObjectToAsset(obj, prefab); } #endif gameObject.transform.Rotate(-gameObject.transform.rotation.eulerAngles); gameObject.transform.Rotate(eulerAngles); gameObject.transform.localScale = scale; PrefabUtility.ReplacePrefab(gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab); loader = null; }
/** * Starts a coroutine, the coroutine does not run immediately but on the * next call to UpdateAllCoroutines. The execution of a coroutine can * be paused at any point using the yield statement. The yield return value * specifies when the coroutine is resumed. */ public CoroutineNode StartCoroutine (IEnumerator fiber) { // if function does not have a yield, fiber will be null and we no-op if (fiber == null) { return null; } // create coroutine node and run until we reach first yield CoroutineNode coroutine = new CoroutineNode (fiber); AddCoroutine (coroutine); return coroutine; }
/** * Starts a coroutine, the coroutine does not run immediately but on the * next call to UpdateAllCoroutines. The execution of a coroutine can * be paused at any point using the yield statement. The yield return value * specifies when the coroutine is resumed. */ public new CoroutineNode StartCoroutine(IEnumerator fiber) { // if function does not have a yield, fiber will be null and we no-op if (fiber == null) { return null; } // create coroutine node and run until we reach first yield CoroutineNode coroutine = new CoroutineNode(fiber); AddCoroutine(coroutine); return coroutine; }
public void EndCoroutine(string name) { if (CheckIfCoroutineExists(name)) { CoroutineNode tmp = GetEntry(name); DeleteInstance(tmp, name); } else { return; } }
// Start is called before the first frame update void Start() { long timestamp = TimeUtil.GetCurTimestamp(); Debug.Log("当前时间戳:" + timestamp); DateTime dateTime = TimeUtil.TimestampToDateTime(timestamp); Debug.Log("当前时间:" + dateTime.ToString()); CoroutineUtil.Instance.Create().AppendEvent(() => Debug.Log("按下Space")).AppendUntil(() => Input.GetKeyDown(KeyCode.Space)).AppendEvent(() => Debug.Log("开始倒计时3s")). AppendRepeat(3, 1, () => Debug.Log(Time.time)).Start(); node = CoroutineUtil.Instance.Create().AppendRepeat(-1, 1, () => Debug.Log("xxx")); node.Start(); }
static void Main(string[] args) { try { var config = new Configuration { EnabledCoreModules = CoreModules.None }; if (!Engine.Initialize("AwaitableCoroutine", 1, 1, config)) { throw new Exception("Failed to initialize the engine"); } var runner = new CoroutineNode(); Engine.AddNode(runner); var coroutine = runner.Create(async() => { var i = 0; await Altseed2Coroutine.DelaySecond(2.0f).UntilCompleted(async() => { Console.WriteLine($"Stepping: {i++}"); await Altseed2Coroutine.DelaySecond(0.2f); }); Console.WriteLine("Hello, AwaitableCoroutine.Altseed2"); }); while (Engine.DoEvents()) { if (coroutine.IsCompleted) { break; } Engine.Update(); } } finally { Engine.Terminate(); } }
/** * Executes coroutine until next yield. If coroutine has finished, flags * it as finished and removes it from scheduler list. */ private void UpdateCoroutine (CoroutineNode coroutine) { IEnumerator fiber = coroutine.fiber; if (coroutine.fiber.MoveNext ()) { System.Object yieldCommand = fiber.Current == null ? (System.Object) 1 : fiber.Current; if (yieldCommand.GetType () == typeof(int)) { coroutine.waitForFrame = (int) yieldCommand; coroutine.waitForFrame += (int) currentFrame; } else if (yieldCommand.GetType () == typeof(float)) { coroutine.waitForTime = (float) yieldCommand; coroutine.waitForTime += (float) currentTime; } else if (yieldCommand.GetType () == typeof(CoroutineNode)) { coroutine.waitForCoroutine = (CoroutineNode) yieldCommand; } else { throw new System.ArgumentException ("CoroutineScheduler: Unexpected coroutine yield type: " + yieldCommand.GetType ()); } } else { // coroutine finished coroutine.finished = true; RemoveCoroutine (coroutine); } }
private CoroutineNode CreateInstance(IEnumerator coroutine) { CoroutineNode node = new CoroutineNode(coroutine); return(node); }
/** * Stops all coroutines running on this behaviour. Use of this method is * discouraged, think of a natural way for your coroutines to finish * on their own instead of being forcefully stopped before they finish. * If you need finer control over stopping coroutines you can use multiple * schedulers. */ public void StopAllCoroutines () { first = null; }
/** * Stops all coroutines running on this behaviour. Use of this method is * discouraged, think of a natural way for your coroutines to finish * on their own instead of being forcefully stopped before they finish. * If you need finer control over stopping coroutines you can use multiple * schedulers. */ public new void StopAllCoroutines() { first = null; }
public IEnumerator ImportModel(Action callback) { GameObject gameObject = null; UnityEngine.Object prefab; importSettings.settings.prefabName = Path.GetFileNameWithoutExtension(selectedFile) + "_" + Path.GetRandomFileName(); prefab = PrefabUtility.CreateEmptyPrefab("Assets/3DModels/" + importSettings.settings.prefabName + ".prefab"); var method = loader.GetType().GetMethod("setSourceCoordinatesSystem", new Type[] { importSettings.settings.isRightHanded.GetType(), importSettings.settings.isZUp.GetType(), importSettings.settings.scaleFactor.GetType() } ); method.Invoke(loader, new object[] { importSettings.settings.isRightHanded, importSettings.settings.isZUp, importSettings.settings.scaleFactor }); double mapUV3dSize = importSettings.settings.mapUV ? importSettings.settings.mapUV3dSize : -1; loader.configure(importSettings.settings.orient, mapUV3dSize, importSettings.settings.treeProcess, importSettings.settings.useLods ? importSettings.settings.lodsMode : LODsMode.NONE, importSettings.settings.lodSettings, !importSettings.settings.splitTo16BytesIndex, importSettings.settings.useMergeFinalAssemblies); CoroutineNode coco = coroutineScheduler.StartCoroutine(loader.loadFileRuntime(gameObject, selectedFile, true, prefab)); yield return(coco); while (!coco.finished) { ; } if ((loader.getErrorMessage().Length > 0)) { Debug.Log("Failure"); if (callback != null) { callback(); } yield break; } else { Debug.Log("Success"); } #if UNITY_EDITOR foreach (Material material in loader.materials.Values) { loader.loadedObject.Add(material); String[] textTypes = { "_MainTex", "_BumpMap", "_Cube", "_LightMap" }; foreach (string textType in textTypes) { if (material.HasProperty(textType) && material.GetTexture(textType)) { loader.loadedObject.Add(material.GetTexture(textType)); } } } foreach (UnityEngine.Object obj in loader.loadedObject) { AssetDatabase.AddObjectToAsset(obj, prefab); } #endif GameObject importedNode; if ((importSettings.settings.treeProcess == TreeProcessType.MERGE || importSettings.settings.treeProcess == TreeProcessType.MERGE_BY_MATERIAL) && !importSettings.settings.useLods) { importedNode = loader.lastImportedObject.transform.GetChild(0).gameObject; loader.lastImportedObject.transform.GetChild(0).transform.SetParent(null); importedNode.name = loader.lastImportedObject.name; DestroyImmediate(loader.lastImportedObject); } else { importedNode = loader.lastImportedObject; } importSettings.settings.originalFilename = selectedFile; importedNode.AddComponent <PiXYZImportSettings>(); importedNode.GetComponent <PiXYZImportSettings>().settings = importSettings.settings; //Copy import settings to object inspector importedNode.GetComponent <PiXYZImportSettings>().windowId = this.GetInstanceID(); importedNode.transform.SetParent(null); PrefabUtility.ReplacePrefab(importedNode, prefab, ReplacePrefabOptions.ConnectToPrefab); if (callback != null) { callback(); } }
/** * Executes coroutine until next yield. If coroutine has finished, flags * it as finished and removes it from scheduler list. */ private void UpdateCoroutine(CoroutineNode coroutine) { IEnumerator fiber = coroutine.fiber; if (coroutine.fiber.MoveNext()) { System.Object yieldCommand = fiber.Current == null ? (System.Object)1 : fiber.Current; if (yieldCommand.GetType() == typeof(int)) { coroutine.waitForFrame = (int)yieldCommand; coroutine.waitForFrame += (int)currentFrame; } else if (yieldCommand.GetType() == typeof(float)) { coroutine.waitForTime = (float)yieldCommand; coroutine.waitForTime += (float)currentTime; } else if (yieldCommand.GetType() == typeof(CoroutineNode)) { coroutine.waitForCoroutine = (CoroutineNode)yieldCommand; } else { throw new System.ArgumentException("CoroutineScheduler: Unexpected coroutine yield type: " + yieldCommand.GetType()); } } else { // coroutine finished coroutine.finished = true; RemoveCoroutine(coroutine); } }
private void AddCoroutine(CoroutineNode coroutine) { if (this.first != null) { coroutine.listNext = this.first; first.listPrevious = coroutine; } first = coroutine; }
private void StopCR(CoroutineNode node) { StopCoroutine(node.myCoroutine); }
private void StartCR(CoroutineNode node) { StartCoroutine(node.myCoroutine); }
IEnumerator ImportModel(string filePath, GameObject parentGameObject, bool p_IsLastOne, int p_Index, int meshQuality, float scale, bool zUp, bool rightHanded) { if (!File.Exists(filePath)) { Debug.Log("CAD FILE HAS BEEN DELETED " + filePath); } GameObject modelGameObject = new GameObject(Path.GetFileNameWithoutExtension(filePath)); modelGameObject.transform.parent = parentGameObject.transform; bool orient = true; float mapUV3dSize = 100.0f; int treeProcessType = 0; List <PiXYZLODSettings> lodSettingsList = new List <PiXYZLODSettings>(); lodSettingsList.Add(GetDefaultLODSettings(meshQuality)); loader.setSourceCoordinatesSystem(rightHanded, zUp, scale); loader.configure(orient, mapUV3dSize, (TreeProcessType)treeProcessType, LODsMode.NONE, lodSettingsList, true, true); yield return(null); CoroutineNode _routine = coroutineScheduler.StartCoroutine(loader.loadFileRuntime(modelGameObject, filePath, false, null)); yield return(_routine); while (!_routine.finished) { ; } loader = null; //add Unity Colliders to every GO that has a mesh //CreateUnityColliders(modelGameObject); //set the model as static by default modelGameObject.isStatic = true; isImporting = false; cadImportTiming = Time.realtimeSinceStartup - cadImportBeginTime; Bounds tmp = getSize(parentGameObject.transform.GetChild(0).gameObject); modelGameObject.transform.localPosition = new Vector3(0 - tmp.center.x, (0.5f - tmp.center.y) - (tmp.size.y / 2), 0 - tmp.center.z); if (ImportEnded != null && p_IsLastOne == true) { ImportEnded(); } Debug.Log("CAD loaded"); coroutineScheduler.StopAllCoroutines(); yield break; }
public void RunCoroutine(IEnumerator coroutine, string name) { CoroutineNode tmp = CreateInstance(coroutine, name); StartCR(tmp); }
public CoroutineNode(int v, CoroutineNode n) { }
private void DeleteInstance(CoroutineNode node, string name) { activeCoroutines.Remove(name); StopCR(node); }