From 1ba09f25e8b718fa9cf0ac248c870075fde54ad1 Mon Sep 17 00:00:00 2001 From: Christopher Date: Mon, 10 Oct 2016 14:41:32 +0100 Subject: [PATCH] Converted (most) foreach to for statements. Avoids an allocation for the loop iterator. --- .../Fungus/Scripts/Commands/ControlAudio.cs | 7 +- .../Fungus/Scripts/Commands/ControlStage.cs | 6 +- Assets/Fungus/Scripts/Commands/FadeUI.cs | 9 +- Assets/Fungus/Scripts/Commands/Jump.cs | 6 +- Assets/Fungus/Scripts/Commands/Say.cs | 3 +- Assets/Fungus/Scripts/Commands/SendMessage.cs | 3 +- Assets/Fungus/Scripts/Commands/SetCollider.cs | 12 +- .../Scripts/Commands/SetInteractable.cs | 16 +- .../Fungus/Scripts/Commands/SetSpriteOrder.cs | 8 +- Assets/Fungus/Scripts/Commands/ShowSprite.cs | 3 +- .../Fungus/Scripts/Commands/StopFlowchart.cs | 4 +- Assets/Fungus/Scripts/Commands/TweenUI.cs | 14 +- .../Fungus/Scripts/Commands/iTweenCommand.cs | 3 +- Assets/Fungus/Scripts/Components/Block.cs | 21 ++- .../Scripts/Components/CameraManager.cs | 4 +- .../Fungus/Scripts/Components/Clickable2D.cs | 3 +- .../Scripts/Components/CommandCopyBuffer.cs | 3 +- .../Fungus/Scripts/Components/DialogInput.cs | 3 +- .../Fungus/Scripts/Components/Draggable2D.cs | 22 ++- Assets/Fungus/Scripts/Components/Flowchart.cs | 146 ++++++++++-------- .../Fungus/Scripts/Components/Localization.cs | 35 +++-- .../Fungus/Scripts/Components/MenuDialog.cs | 53 +++---- Assets/Fungus/Scripts/Components/SayDialog.cs | 11 +- .../Fungus/Scripts/Components/SpriteFader.cs | 4 +- Assets/Fungus/Scripts/Components/Writer.cs | 24 ++- Assets/Fungus/Scripts/Utils/TextTagParser.cs | 13 +- 26 files changed, 241 insertions(+), 195 deletions(-) diff --git a/Assets/Fungus/Scripts/Commands/ControlAudio.cs b/Assets/Fungus/Scripts/Commands/ControlAudio.cs index 6df428bf..0ee5b000 100644 --- a/Assets/Fungus/Scripts/Commands/ControlAudio.cs +++ b/Assets/Fungus/Scripts/Commands/ControlAudio.cs @@ -66,11 +66,12 @@ namespace Fungus } var audioSources = GameObject.FindObjectsOfType(); - foreach (var a in audioSources) + for (int i = 0; i < audioSources.Length; i++) { - if ((a.GetComponent() != _audioSource.Value) && (a.tag == _audioSource.Value.tag)) + var a = audioSources[i]; + if (a != _audioSource.Value && a.tag == _audioSource.Value.tag) { - StopLoop(a.GetComponent()); + StopLoop(a); } } } diff --git a/Assets/Fungus/Scripts/Commands/ControlStage.cs b/Assets/Fungus/Scripts/Commands/ControlStage.cs index 4fb3b385..0dd9dd87 100644 --- a/Assets/Fungus/Scripts/Commands/ControlStage.cs +++ b/Assets/Fungus/Scripts/Commands/ControlStage.cs @@ -73,8 +73,9 @@ namespace Fungus protected virtual void MoveToFront(Stage stage) { var activeStages = Stage.ActiveStages; - foreach (var s in activeStages) + for (int i = 0; i < activeStages.Count; i++) { + var s = activeStages[i]; if (s == stage) { s.PortraitCanvas.sortingOrder = 1; @@ -90,8 +91,9 @@ namespace Fungus { stage.DimPortraits = false; var charactersOnStage = stage.CharactersOnStage; - foreach (var character in charactersOnStage) + for (int i = 0; i < charactersOnStage.Count; i++) { + var character = charactersOnStage[i]; stage.SetDimmed(character, false); } } diff --git a/Assets/Fungus/Scripts/Commands/FadeUI.cs b/Assets/Fungus/Scripts/Commands/FadeUI.cs index f02682db..d344c79d 100644 --- a/Assets/Fungus/Scripts/Commands/FadeUI.cs +++ b/Assets/Fungus/Scripts/Commands/FadeUI.cs @@ -34,8 +34,9 @@ namespace Fungus protected override void ApplyTween(GameObject go) { var images = go.GetComponentsInChildren(); - foreach (var image in images) + for (int i = 0; i < images.Length; i++) { + var image = images[i]; if (Mathf.Approximately(duration, 0f)) { switch (fadeMode) @@ -65,8 +66,9 @@ namespace Fungus } var texts = go.GetComponentsInChildren(); - foreach (var text in texts) + for (int i = 0; i < texts.Length; i++) { + var text = texts[i]; if (Mathf.Approximately(duration, 0f)) { switch (fadeMode) @@ -96,8 +98,9 @@ namespace Fungus } var textMeshes = go.GetComponentsInChildren(); - foreach (var textMesh in textMeshes) + for (int i = 0; i < textMeshes.Length; i++) { + var textMesh = textMeshes[i]; if (Mathf.Approximately(duration, 0f)) { switch (fadeMode) diff --git a/Assets/Fungus/Scripts/Commands/Jump.cs b/Assets/Fungus/Scripts/Commands/Jump.cs index dc20214d..ebaa4b4d 100644 --- a/Assets/Fungus/Scripts/Commands/Jump.cs +++ b/Assets/Fungus/Scripts/Commands/Jump.cs @@ -30,11 +30,11 @@ namespace Fungus } var commandList = ParentBlock.CommandList; - foreach (var command in commandList) + for (int i = 0; i < commandList.Count; i++) { + var command = commandList[i]; Label label = command as Label; - if (label != null && - label.Key == _targetLabel.Value) + if (label != null && label.Key == _targetLabel.Value) { Continue(label.CommandIndex + 1); return; diff --git a/Assets/Fungus/Scripts/Commands/Say.cs b/Assets/Fungus/Scripts/Commands/Say.cs index 6672a365..d129f78b 100644 --- a/Assets/Fungus/Scripts/Commands/Say.cs +++ b/Assets/Fungus/Scripts/Commands/Say.cs @@ -108,8 +108,9 @@ namespace Fungus string displayText = storyText; var activeCustomTags = CustomTag.activeCustomTags; - foreach (var ct in activeCustomTags) + for (int i = 0; i < activeCustomTags.Count; i++) { + var ct = activeCustomTags[i]; displayText = displayText.Replace(ct.TagStartSymbol, ct.ReplaceTagStartWith); if (ct.TagEndSymbol != "" && ct.ReplaceTagEndWith != "") { diff --git a/Assets/Fungus/Scripts/Commands/SendMessage.cs b/Assets/Fungus/Scripts/Commands/SendMessage.cs index 336f0c7f..fd87b76d 100644 --- a/Assets/Fungus/Scripts/Commands/SendMessage.cs +++ b/Assets/Fungus/Scripts/Commands/SendMessage.cs @@ -59,8 +59,9 @@ namespace Fungus if (receivers != null) { - foreach (var receiver in receivers) + for (int i = 0; i < receivers.Length; i++) { + var receiver = receivers[i]; receiver.OnSendFungusMessage(_message.Value); } } diff --git a/Assets/Fungus/Scripts/Commands/SetCollider.cs b/Assets/Fungus/Scripts/Commands/SetCollider.cs index 6d7176a7..b56a64f5 100644 --- a/Assets/Fungus/Scripts/Commands/SetCollider.cs +++ b/Assets/Fungus/Scripts/Commands/SetCollider.cs @@ -30,15 +30,17 @@ namespace Fungus { // 3D objects var colliders = go.GetComponentsInChildren(); - foreach (var c in colliders) + for (int i = 0; i < colliders.Length; i++) { + var c = colliders[i]; c.enabled = activeState.Value; } // 2D objects var collider2Ds = go.GetComponentsInChildren(); - foreach (var c in collider2Ds) + for (int i = 0; i < collider2Ds.Length; i++) { + var c = collider2Ds[i]; c.enabled = activeState.Value; } } @@ -48,8 +50,9 @@ namespace Fungus public override void OnEnter() { - foreach (var go in targetObjects) + for (int i = 0; i < targetObjects.Count; i++) { + var go = targetObjects[i]; SetColliderActive(go); } @@ -65,8 +68,9 @@ namespace Fungus if (taggedObjects != null) { - foreach (var go in taggedObjects) + for (int i = 0; i < taggedObjects.Length; i++) { + var go = taggedObjects[i]; SetColliderActive(go); } } diff --git a/Assets/Fungus/Scripts/Commands/SetInteractable.cs b/Assets/Fungus/Scripts/Commands/SetInteractable.cs index ef06ca9f..66d77eb1 100644 --- a/Assets/Fungus/Scripts/Commands/SetInteractable.cs +++ b/Assets/Fungus/Scripts/Commands/SetInteractable.cs @@ -31,11 +31,13 @@ namespace Fungus return; } - foreach (var targetObject in targetObjects) + for (int i = 0; i < targetObjects.Count; i++) { + var targetObject = targetObjects[i]; var selectables = targetObject.GetComponents(); - foreach (var selectable in selectables) + for (int j = 0; j < selectables.Length; j++) { + var selectable = selectables[j]; selectable.interactable = interactableState.Value; } } @@ -59,20 +61,20 @@ namespace Fungus } string objectList = ""; - foreach (var gameObject in targetObjects) + for (int i = 0; i < targetObjects.Count; i++) { - if (gameObject == null) + var go = targetObjects[i]; + if (go == null) { continue; } - if (objectList == "") { - objectList += gameObject.name; + objectList += go.name; } else { - objectList += ", " + gameObject.name; + objectList += ", " + go.name; } } diff --git a/Assets/Fungus/Scripts/Commands/SetSpriteOrder.cs b/Assets/Fungus/Scripts/Commands/SetSpriteOrder.cs index 08bc9798..e284941d 100644 --- a/Assets/Fungus/Scripts/Commands/SetSpriteOrder.cs +++ b/Assets/Fungus/Scripts/Commands/SetSpriteOrder.cs @@ -25,8 +25,9 @@ namespace Fungus public override void OnEnter() { - foreach (var spriteRenderer in targetSprites) + for (int i = 0; i < targetSprites.Count; i++) { + var spriteRenderer = targetSprites[i]; spriteRenderer.sortingOrder = orderInLayer; } @@ -36,18 +37,17 @@ namespace Fungus public override string GetSummary() { string summary = ""; - foreach (var spriteRenderer in targetSprites) + for (int i = 0; i < targetSprites.Count; i++) { + var spriteRenderer = targetSprites[i]; if (spriteRenderer == null) { continue; } - if (summary.Length > 0) { summary += ", "; } - summary += spriteRenderer.name; } diff --git a/Assets/Fungus/Scripts/Commands/ShowSprite.cs b/Assets/Fungus/Scripts/Commands/ShowSprite.cs index 1ce8fbde..110b6517 100644 --- a/Assets/Fungus/Scripts/Commands/ShowSprite.cs +++ b/Assets/Fungus/Scripts/Commands/ShowSprite.cs @@ -41,8 +41,9 @@ namespace Fungus if (affectChildren) { var spriteRenderers = spriteRenderer.gameObject.GetComponentsInChildren(); - foreach (var sr in spriteRenderers) + for (int i = 0; i < spriteRenderers.Length; i++) { + var sr = spriteRenderers[i]; SetSpriteAlpha(sr, _visible.Value); } } diff --git a/Assets/Fungus/Scripts/Commands/StopFlowchart.cs b/Assets/Fungus/Scripts/Commands/StopFlowchart.cs index b295b2dc..c11cedbb 100644 --- a/Assets/Fungus/Scripts/Commands/StopFlowchart.cs +++ b/Assets/Fungus/Scripts/Commands/StopFlowchart.cs @@ -32,14 +32,14 @@ namespace Fungus flowchart.StopAllBlocks(); } - foreach (var f in targetFlowcharts) + for (int i = 0; i < targetFlowcharts.Count; i++) { + var f = targetFlowcharts[i]; if (f == flowchart) { // Flowchart has already been stopped continue; } - f.StopAllBlocks(); } } diff --git a/Assets/Fungus/Scripts/Commands/TweenUI.cs b/Assets/Fungus/Scripts/Commands/TweenUI.cs index e65d32be..2563c796 100644 --- a/Assets/Fungus/Scripts/Commands/TweenUI.cs +++ b/Assets/Fungus/Scripts/Commands/TweenUI.cs @@ -26,13 +26,13 @@ namespace Fungus protected virtual void ApplyTween() { - foreach (var targetObject in targetObjects) + for (int i = 0; i < targetObjects.Count; i++) { + var targetObject = targetObjects[i]; if (targetObject == null) { continue; } - ApplyTween(targetObject); } @@ -97,20 +97,20 @@ namespace Fungus } string objectList = ""; - foreach (var gameObject in targetObjects) + for (int i = 0; i < targetObjects.Count; i++) { - if (gameObject == null) + var go = targetObjects[i]; + if (go == null) { continue; } - if (objectList == "") { - objectList += gameObject.name; + objectList += go.name; } else { - objectList += ", " + gameObject.name; + objectList += ", " + go.name; } } diff --git a/Assets/Fungus/Scripts/Commands/iTweenCommand.cs b/Assets/Fungus/Scripts/Commands/iTweenCommand.cs index ceb96904..2bc8b0ac 100644 --- a/Assets/Fungus/Scripts/Commands/iTweenCommand.cs +++ b/Assets/Fungus/Scripts/Commands/iTweenCommand.cs @@ -74,8 +74,9 @@ namespace Fungus { // Force any existing iTweens on this target object to complete immediately var tweens = _targetObject.Value.GetComponents(); - foreach (var tween in tweens) + for (int i = 0; i < tweens.Length; i++) { + var tween = tweens[i]; tween.time = 0; tween.SendMessage("Update"); } diff --git a/Assets/Fungus/Scripts/Components/Block.cs b/Assets/Fungus/Scripts/Components/Block.cs index 65296820..b181a3f0 100644 --- a/Assets/Fungus/Scripts/Components/Block.cs +++ b/Assets/Fungus/Scripts/Components/Block.cs @@ -72,13 +72,13 @@ namespace Fungus // Give each child command a reference back to its parent block // and tell each command its index in the list. int index = 0; - foreach (var command in commandList) + for (int i = 0; i < commandList.Count; i++) { + var command = commandList[i]; if (command == null) { continue; } - command.ParentBlock = this; command.CommandIndex = index++; } @@ -98,13 +98,14 @@ namespace Fungus protected virtual void Update() { int index = 0; - foreach (var command in commandList) + for (int i = 0; i < commandList.Count; i++) { - if (command == null) // Null entry will be deleted automatically later + var command = commandList[i]; + if (command == null)// Null entry will be deleted automatically later + { continue; } - command.CommandIndex = index++; } } @@ -328,8 +329,9 @@ namespace Fungus public virtual List GetConnectedBlocks() { var connectedBlocks = new List(); - foreach (var command in commandList) + for (int i = 0; i < commandList.Count; i++) { + var command = commandList[i]; if (command != null) { command.GetConnectedBlocks(ref connectedBlocks); @@ -359,23 +361,20 @@ namespace Fungus public virtual void UpdateIndentLevels() { int indentLevel = 0; - foreach (var command in commandList) + for (int i = 0; i < commandList.Count; i++) { + var command = commandList[i]; if (command == null) { continue; } - if (command.CloseBlock()) { indentLevel--; } - // Negative indent level is not permitted indentLevel = Math.Max(indentLevel, 0); - command.IndentLevel = indentLevel; - if (command.OpenBlock()) { indentLevel++; diff --git a/Assets/Fungus/Scripts/Components/CameraManager.cs b/Assets/Fungus/Scripts/Components/CameraManager.cs index 93527250..d6c04acc 100644 --- a/Assets/Fungus/Scripts/Components/CameraManager.cs +++ b/Assets/Fungus/Scripts/Components/CameraManager.cs @@ -155,10 +155,10 @@ namespace Fungus camera.orthographicSize = Mathf.Lerp(startSize, endSize, Mathf.SmoothStep(0f, 1f, t)); camera.transform.position = Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0f, 1f, t)); camera.transform.rotation = Quaternion.Lerp(startRot, endRot, Mathf.SmoothStep(0f, 1f, t)); + + SetCameraZ(camera); } - SetCameraZ(camera); - if (arrived && arriveAction != null) { diff --git a/Assets/Fungus/Scripts/Components/Clickable2D.cs b/Assets/Fungus/Scripts/Components/Clickable2D.cs index 06f04a74..93c56112 100644 --- a/Assets/Fungus/Scripts/Components/Clickable2D.cs +++ b/Assets/Fungus/Scripts/Components/Clickable2D.cs @@ -41,8 +41,9 @@ namespace Fungus // TODO: Cache these objects for faster lookup var handlers = GameObject.FindObjectsOfType(); - foreach (var handler in handlers) + for (int i = 0; i < handlers.Length; i++) { + var handler = handlers[i]; handler.OnObjectClicked(this); } } diff --git a/Assets/Fungus/Scripts/Components/CommandCopyBuffer.cs b/Assets/Fungus/Scripts/Components/CommandCopyBuffer.cs index ad68d6f2..720c446d 100644 --- a/Assets/Fungus/Scripts/Components/CommandCopyBuffer.cs +++ b/Assets/Fungus/Scripts/Components/CommandCopyBuffer.cs @@ -63,8 +63,9 @@ namespace Fungus public virtual void Clear() { var commands = GetCommands(); - foreach (var command in commands) + for (int i = 0; i < commands.Length; i++) { + var command = commands[i]; DestroyImmediate(command); } } diff --git a/Assets/Fungus/Scripts/Components/DialogInput.cs b/Assets/Fungus/Scripts/Components/DialogInput.cs index 54b7232a..cbfdb714 100644 --- a/Assets/Fungus/Scripts/Components/DialogInput.cs +++ b/Assets/Fungus/Scripts/Components/DialogInput.cs @@ -115,8 +115,9 @@ namespace Fungus if (nextLineInputFlag) { var inputListeners = gameObject.GetComponentsInChildren(); - foreach (var inputListener in inputListeners) + for (int i = 0; i < inputListeners.Length; i++) { + var inputListener = inputListeners[i]; inputListener.OnNextLineEvent(); } nextLineInputFlag = false; diff --git a/Assets/Fungus/Scripts/Components/Draggable2D.cs b/Assets/Fungus/Scripts/Components/Draggable2D.cs index 20464ed4..68c661ec 100644 --- a/Assets/Fungus/Scripts/Components/Draggable2D.cs +++ b/Assets/Fungus/Scripts/Components/Draggable2D.cs @@ -59,14 +59,16 @@ namespace Fungus } var dragEnteredHandlers = GetHandlers(); - foreach (var handler in dragEnteredHandlers) + for (int i = 0; i < dragEnteredHandlers.Length; i++) { + var handler = dragEnteredHandlers[i]; handler.OnDragEntered(this, other); } var dragCompletedHandlers = GetHandlers(); - foreach (var handler in dragCompletedHandlers) + for (int i = 0; i < dragCompletedHandlers.Length; i++) { + var handler = dragCompletedHandlers[i]; handler.OnDragEntered(this, other); } } @@ -79,14 +81,16 @@ namespace Fungus } var dragExitedHandlers = GetHandlers(); - foreach (var handler in dragExitedHandlers) + for (int i = 0; i < dragExitedHandlers.Length; i++) { + var handler = dragExitedHandlers[i]; handler.OnDragExited(this, other); } var dragCompletedHandlers = GetHandlers(); - foreach (var handler in dragCompletedHandlers) + for (int i = 0; i < dragCompletedHandlers.Length; i++) { + var handler = dragCompletedHandlers[i]; handler.OnDragExited(this, other); } } @@ -108,8 +112,9 @@ namespace Fungus startingPosition = transform.position; var dragStartedHandlers = GetHandlers(); - foreach (var handler in dragStartedHandlers) + for (int i = 0; i < dragStartedHandlers.Length; i++) { + var handler = dragStartedHandlers[i]; handler.OnDragStarted(this); } } @@ -140,15 +145,15 @@ namespace Fungus bool dragCompleted = false; var handlers = GetHandlers(); - foreach (var handler in handlers) + for (int i = 0; i < handlers.Length; i++) { + var handler = handlers[i]; if (handler.DraggableObject == this) { if (handler.IsOverTarget()) { handler.OnDragCompleted(this); dragCompleted = true; - if (returnOnCompleted) { LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); @@ -160,8 +165,9 @@ namespace Fungus if (!dragCompleted) { var dragCancelledHandlers = GetHandlers(); - foreach (var handler in dragCancelledHandlers) + for (int i = 0; i < dragCancelledHandlers.Length; i++) { + var handler = dragCancelledHandlers[i]; handler.OnDragCancelled(this); } diff --git a/Assets/Fungus/Scripts/Components/Flowchart.cs b/Assets/Fungus/Scripts/Components/Flowchart.cs index da096357..e8a57452 100644 --- a/Assets/Fungus/Scripts/Components/Flowchart.cs +++ b/Assets/Fungus/Scripts/Components/Flowchart.cs @@ -159,8 +159,9 @@ namespace Fungus // Tell all components that implement IUpdateable to update to the new version var components = GetComponents(); - foreach (var component in components) + for (int i = 0; i < components.Length; i++) { + var component = components[i]; IUpdateable u = component as IUpdateable; if (u != null) { @@ -182,10 +183,10 @@ namespace Fungus // This should always be the case, but some legacy Flowcharts may have issues. List usedIds = new List(); var blocks = GetComponents(); - foreach (var block in blocks) + for (int i = 0; i < blocks.Length; i++) { - if (block.ItemId == -1 || - usedIds.Contains(block.ItemId)) + var block = blocks[i]; + if (block.ItemId == -1 || usedIds.Contains(block.ItemId)) { block.ItemId = NextItemId(); } @@ -193,10 +194,10 @@ namespace Fungus } var commands = GetComponents(); - foreach (var command in commands) + for (int i = 0; i < commands.Length; i++) { - if (command.ItemId == -1 || - usedIds.Contains(command.ItemId)) + var command = commands[i]; + if (command.ItemId == -1 || usedIds.Contains(command.ItemId)) { command.ItemId = NextItemId(); } @@ -215,8 +216,9 @@ namespace Fungus variables.RemoveAll(item => item == null); var allVariables = GetComponents(); - foreach (var variable in allVariables) + for (int i = 0; i < allVariables.Length; i++) { + var variable = allVariables[i]; if (!variables.Contains(variable)) { DestroyImmediate(variable); @@ -225,18 +227,19 @@ namespace Fungus var blocks = GetComponents(); var commands = GetComponents(); - foreach (var command in commands) + for (int i = 0; i < commands.Length; i++) { + var command = commands[i]; bool found = false; - foreach (var block in blocks) + for (int j = 0; j < blocks.Length; j++) { + var block = blocks[j]; if (block.CommandList.Contains(command)) { found = true; break; } } - if (!found) { DestroyImmediate(command); @@ -244,18 +247,19 @@ namespace Fungus } var eventHandlers = GetComponents(); - foreach (var eventHandler in eventHandlers) + for (int i = 0; i < eventHandlers.Length; i++) { + var eventHandler = eventHandlers[i]; bool found = false; - foreach (var block in blocks) + for (int j = 0; j < blocks.Length; j++) { + var block = blocks[j]; if (block._EventHandler == eventHandler) { found = true; break; } } - if (!found) { DestroyImmediate(eventHandler); @@ -283,8 +287,9 @@ namespace Fungus public static void BroadcastFungusMessage(string messageName) { var eventHandlers = UnityEngine.Object.FindObjectsOfType(); - foreach (var eventHandler in eventHandlers) + for (int i = 0; i < eventHandlers.Length; i++) { + var eventHandler = eventHandlers[i]; eventHandler.OnSendFungusMessage(messageName); } } @@ -409,14 +414,16 @@ namespace Fungus { int maxId = -1; var blocks = GetComponents(); - foreach (var block in blocks) + for (int i = 0; i < blocks.Length; i++) { + var block = blocks[i]; maxId = Math.Max(maxId, block.ItemId); } var commands = GetComponents(); - foreach (var command in commands) + for (int i = 0; i < commands.Length; i++) { + var command = commands[i]; maxId = Math.Max(maxId, command.ItemId); } return maxId + 1; @@ -441,8 +448,9 @@ namespace Fungus public virtual Block FindBlock(string blockName) { var blocks = GetComponents(); - foreach (var block in blocks) + for (int i = 0; i < blocks.Length; i++) { + var block = blocks[i]; if (block.BlockName == blockName) { return block; @@ -509,8 +517,9 @@ namespace Fungus public virtual void StopAllBlocks() { var blocks = GetComponents(); - foreach (var block in blocks) + for (int i = 0; i < blocks.Length; i++) { + var block = blocks[i]; if (block.IsExecuting()) { block.Stop(); @@ -525,8 +534,9 @@ namespace Fungus public virtual void SendFungusMessage(string messageName) { var eventHandlers = GetComponents(); - foreach (var eventHandler in eventHandlers) + for (int i = 0; i < eventHandlers.Length; i++) { + var eventHandler = eventHandlers[i]; eventHandler.OnSendFungusMessage(messageName); } } @@ -556,15 +566,13 @@ namespace Fungus while (true) { bool collision = false; - foreach(var variable in variables) + for (int i = 0; i < variables.Count; i++) { - if (variable == null || - variable == ignoreVariable || - variable.Key == null) + var variable = variables[i]; + if (variable == null || variable == ignoreVariable || variable.Key == null) { continue; } - if (variable.Key.Equals(key, StringComparison.CurrentCultureIgnoreCase)) { collision = true; @@ -600,14 +608,13 @@ namespace Fungus while (true) { bool collision = false; - foreach (var block in blocks) + for (int i = 0; i < blocks.Length; i++) { - if (block == ignoreBlock || - block.BlockName == null) + var block = blocks[i]; + if (block == ignoreBlock || block.BlockName == null) { continue; } - if (block.BlockName.Equals(key, StringComparison.CurrentCultureIgnoreCase)) { collision = true; @@ -644,15 +651,14 @@ namespace Fungus { bool collision = false; var commandList = block.CommandList; - foreach (var command in commandList) + for (int i = 0; i < commandList.Count; i++) { + var command = commandList[i]; Label label = command as Label; - if (label == null || - label == ignoreLabel) + if (label == null || label == ignoreLabel) { continue; } - if (label.Key.Equals(key, StringComparison.CurrentCultureIgnoreCase)) { collision = true; @@ -677,8 +683,9 @@ namespace Fungus /// public Variable GetVariable(string key) { - foreach (var variable in variables) + for (int i = 0; i < variables.Count; i++) { + var variable = variables[i]; if (variable != null && variable.Key == key) { return variable; @@ -696,8 +703,9 @@ namespace Fungus /// public T GetVariable(string key) where T : Variable { - foreach (var variable in variables) + for (int i = 0; i < variables.Count; i++) { + var variable = variables[i]; if (variable != null && variable.Key == key) { return variable as T; @@ -714,8 +722,9 @@ namespace Fungus /// public void SetVariable(string key, T newvariable) where T : Variable { - foreach (var v in variables) + for (int i = 0; i < variables.Count; i++) { + var v = variables[i]; if (v != null && v.Key == key) { T variable = v as T; @@ -735,9 +744,10 @@ namespace Fungus /// public virtual List GetPublicVariables() { - List publicVariables = new List(); - foreach (var v in variables) + var publicVariables = new List(); + for (int i = 0; i < variables.Count; i++) { + var v = variables[i]; if (v != null && v.Scope == VariableScope.Public) { publicVariables.Add(v); @@ -879,8 +889,9 @@ namespace Fungus if (hideComponents) { var blocks = GetComponents(); - foreach (var block in blocks) + for (int i = 0; i < blocks.Length; i++) { + var block = blocks[i]; block.hideFlags = HideFlags.HideInInspector; if (block.gameObject != gameObject) { @@ -889,27 +900,29 @@ namespace Fungus } var commands = GetComponents(); - foreach (var command in commands) + for (int i = 0; i < commands.Length; i++) { + var command = commands[i]; command.hideFlags = HideFlags.HideInInspector; } var eventHandlers = GetComponents(); - foreach (var eventHandler in eventHandlers) + for (int i = 0; i < eventHandlers.Length; i++) { + var eventHandler = eventHandlers[i]; eventHandler.hideFlags = HideFlags.HideInInspector; } } else { var monoBehaviours = GetComponents(); - foreach (var monoBehaviour in monoBehaviours) + for (int i = 0; i < monoBehaviours.Length; i++) { + var monoBehaviour = monoBehaviours[i]; if (monoBehaviour == null) { continue; } - monoBehaviour.hideFlags = HideFlags.None; monoBehaviour.gameObject.hideFlags = HideFlags.None; } @@ -943,16 +956,18 @@ namespace Fungus if (resetCommands) { var commands = GetComponents(); - foreach (var command in commands) + for (int i = 0; i < commands.Length; i++) { + var command = commands[i]; command.OnReset(); } } if (resetVariables) { - foreach (var variable in variables) + for (int i = 0; i < variables.Count; i++) { + var variable = variables[i]; variable.OnReset(); } } @@ -963,11 +978,11 @@ namespace Fungus /// public virtual bool IsCommandSupported(CommandInfoAttribute commandInfo) { - foreach (var key in hideCommands) + for (int i = 0; i < hideCommands.Count; i++) { // Match on category or command name (case insensitive) - if (String.Compare(commandInfo.Category, key, StringComparison.OrdinalIgnoreCase) == 0 || - String.Compare(commandInfo.CommandName, key, StringComparison.OrdinalIgnoreCase) == 0) + var key = hideCommands[i]; + if (String.Compare(commandInfo.Category, key, StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(commandInfo.CommandName, key, StringComparison.OrdinalIgnoreCase) == 0) { return false; } @@ -982,8 +997,9 @@ namespace Fungus public virtual bool HasExecutingBlocks() { var blocks = GetComponents(); - foreach (var block in blocks) + for (int i = 0; i < blocks.Length; i++) { + var block = blocks[i]; if (block.IsExecuting()) { return true; @@ -999,8 +1015,9 @@ namespace Fungus { var executingBlocks = new List(); var blocks = GetComponents(); - foreach (var block in blocks) + for (int i = 0; i < blocks.Length; i++) { + var block = blocks[i]; if (block.IsExecuting()) { executingBlocks.Add(block); @@ -1036,19 +1053,18 @@ namespace Fungus // Match the regular expression pattern against a text string. var results = r.Matches(input); - foreach (Match match in results) + for (int i = 0; i < results.Count; i++) { + Match match = results[i]; string key = match.Value.Substring(2, match.Value.Length - 3); - // Look for any matching private variables in this Flowchart first - foreach (var variable in variables) + for (int j = 0; j < variables.Count; j++) { + var variable = variables[j]; if (variable == null) continue; - - if (variable.Scope == VariableScope.Private && - variable.Key == key) - { + if (variable.Scope == VariableScope.Private && variable.Key == key) + { string value = variable.ToString(); sb.Replace(match.Value, value); changed = true; @@ -1088,22 +1104,22 @@ namespace Fungus // Match the regular expression pattern against a text string. var results = r.Matches(input.ToString()); - foreach (Match match in results) + for (int i = 0; i < results.Count; i++) { + Match match = results[i]; string key = match.Value.Substring(2, match.Value.Length - 3); - // Look for any matching public variables in this Flowchart - foreach (var variable in variables) + for (int j = 0; j < variables.Count; j++) { + var variable = variables[j]; if (variable == null) + { continue; - - if (variable.Scope == VariableScope.Public && - variable.Key == key) - { + } + if (variable.Scope == VariableScope.Public && variable.Key == key) + { string value = variable.ToString(); input.Replace(match.Value, value); - modified = true; } } diff --git a/Assets/Fungus/Scripts/Components/Localization.cs b/Assets/Fungus/Scripts/Components/Localization.cs index daad4ad6..604e107b 100644 --- a/Assets/Fungus/Scripts/Components/Localization.cs +++ b/Assets/Fungus/Scripts/Components/Localization.cs @@ -96,8 +96,9 @@ namespace Fungus protected virtual void CacheLocalizeableObjects() { UnityEngine.Object[] objects = Resources.FindObjectsOfTypeAll(typeof(Component)); - foreach (var o in objects) + for (int i = 0; i < objects.Length; i++) { + var o = objects[i]; ILocalizable localizable = o as ILocalizable; if (localizable != null) { @@ -116,14 +117,18 @@ namespace Fungus // Add localizable commands in same order as command list to make it // easier to localise / edit standard text. var flowcharts = GameObject.FindObjectsOfType(); - foreach (var flowchart in flowcharts) + for (int i = 0; i < flowcharts.Length; i++) { + var flowchart = flowcharts[i]; var blocks = flowchart.GetComponents(); - foreach (var block in blocks) + + for (int j = 0; j < blocks.Length; j++) { + var block = blocks[j]; var commandList = block.CommandList; - foreach (var command in commandList) + for (int k = 0; k < commandList.Count; k++) { + var command = commandList[k]; ILocalizable localizable = command as ILocalizable; if (localizable != null) { @@ -138,8 +143,9 @@ namespace Fungus // Add everything else that's localizable (including inactive objects) UnityEngine.Object[] objects = Resources.FindObjectsOfTypeAll(typeof(Component)); - foreach (var o in objects) + for (int i = 0; i < objects.Length; i++) { + var o = objects[i]; ILocalizable localizable = o as ILocalizable; if (localizable != null) { @@ -149,7 +155,6 @@ namespace Fungus // Already added continue; } - TextItem textItem = new TextItem(); textItem.standardText = localizable.GetStandardText(); textItem.description = localizable.GetDescription(); @@ -289,7 +294,7 @@ namespace Fungus // Build CSV header row and a list of the language codes currently in use string csvHeader = "Key,Description,Standard"; - List languageCodes = new List(); + var languageCodes = new List(); var values = textItems.Values; foreach (var textItem in values) { @@ -315,15 +320,17 @@ namespace Fungus row += "," + CSVSupport.Escape(textItem.description); row += "," + CSVSupport.Escape(textItem.standardText); - foreach (var languageCode in languageCodes) + for (int i = 0; i < languageCodes.Count; i++) { + var languageCode = languageCodes[i]; if (textItem.localizedStrings.ContainsKey(languageCode)) { row += "," + CSVSupport.Escape(textItem.localizedStrings[languageCode]); } else { - row += ","; // Empty field + row += ","; + // Empty field } } @@ -485,15 +492,16 @@ namespace Fungus /// public virtual void SetStandardText(string textData) { - string[] lines = textData.Split('\n'); + var lines = textData.Split('\n'); int updatedCount = 0; string stringId = ""; string buffer = ""; - foreach (var line in lines) + for (int i = 0; i < lines.Length; i++) { // Check for string id line + var line = lines[i]; if (line.StartsWith("#")) { if (stringId.Length > 0) @@ -504,7 +512,6 @@ namespace Fungus updatedCount++; } } - // Set the string id for the follow text lines stringId = line.Substring(1, line.Length - 1); buffer = ""; @@ -544,10 +551,10 @@ namespace Fungus // Match the regular expression pattern against a text string. var results = r.Matches(input.ToString()); - foreach (Match match in results) + for (int i = 0; i < results.Count; i++) { + Match match = results[i]; string key = match.Value.Substring(2, match.Value.Length - 3); - // Next look for matching localized string string localizedString = Localization.GetLocalizedString(key); if (localizedString != null) diff --git a/Assets/Fungus/Scripts/Components/MenuDialog.cs b/Assets/Fungus/Scripts/Components/MenuDialog.cs index 4c1cac2e..935d242a 100644 --- a/Assets/Fungus/Scripts/Components/MenuDialog.cs +++ b/Assets/Fungus/Scripts/Components/MenuDialog.cs @@ -154,14 +154,16 @@ namespace Fungus { StopAllCoroutines(); - Button[] optionButtons = GetComponentsInChildren