Browse Source

Converted (most) foreach to for statements. Avoids an allocation for the loop iterator.

master
Christopher 8 years ago
parent
commit
1ba09f25e8
  1. 7
      Assets/Fungus/Scripts/Commands/ControlAudio.cs
  2. 6
      Assets/Fungus/Scripts/Commands/ControlStage.cs
  3. 9
      Assets/Fungus/Scripts/Commands/FadeUI.cs
  4. 6
      Assets/Fungus/Scripts/Commands/Jump.cs
  5. 3
      Assets/Fungus/Scripts/Commands/Say.cs
  6. 3
      Assets/Fungus/Scripts/Commands/SendMessage.cs
  7. 12
      Assets/Fungus/Scripts/Commands/SetCollider.cs
  8. 16
      Assets/Fungus/Scripts/Commands/SetInteractable.cs
  9. 8
      Assets/Fungus/Scripts/Commands/SetSpriteOrder.cs
  10. 3
      Assets/Fungus/Scripts/Commands/ShowSprite.cs
  11. 4
      Assets/Fungus/Scripts/Commands/StopFlowchart.cs
  12. 14
      Assets/Fungus/Scripts/Commands/TweenUI.cs
  13. 3
      Assets/Fungus/Scripts/Commands/iTweenCommand.cs
  14. 21
      Assets/Fungus/Scripts/Components/Block.cs
  15. 4
      Assets/Fungus/Scripts/Components/CameraManager.cs
  16. 3
      Assets/Fungus/Scripts/Components/Clickable2D.cs
  17. 3
      Assets/Fungus/Scripts/Components/CommandCopyBuffer.cs
  18. 3
      Assets/Fungus/Scripts/Components/DialogInput.cs
  19. 22
      Assets/Fungus/Scripts/Components/Draggable2D.cs
  20. 146
      Assets/Fungus/Scripts/Components/Flowchart.cs
  21. 35
      Assets/Fungus/Scripts/Components/Localization.cs
  22. 53
      Assets/Fungus/Scripts/Components/MenuDialog.cs
  23. 11
      Assets/Fungus/Scripts/Components/SayDialog.cs
  24. 4
      Assets/Fungus/Scripts/Components/SpriteFader.cs
  25. 24
      Assets/Fungus/Scripts/Components/Writer.cs
  26. 13
      Assets/Fungus/Scripts/Utils/TextTagParser.cs

7
Assets/Fungus/Scripts/Commands/ControlAudio.cs

@ -66,11 +66,12 @@ namespace Fungus
}
var audioSources = GameObject.FindObjectsOfType<AudioSource>();
foreach (var a in audioSources)
for (int i = 0; i < audioSources.Length; i++)
{
if ((a.GetComponent<AudioSource>() != _audioSource.Value) && (a.tag == _audioSource.Value.tag))
var a = audioSources[i];
if (a != _audioSource.Value && a.tag == _audioSource.Value.tag)
{
StopLoop(a.GetComponent<AudioSource>());
StopLoop(a);
}
}
}

6
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);
}
}

9
Assets/Fungus/Scripts/Commands/FadeUI.cs

@ -34,8 +34,9 @@ namespace Fungus
protected override void ApplyTween(GameObject go)
{
var images = go.GetComponentsInChildren<Image>();
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<Text>();
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<TextMesh>();
foreach (var textMesh in textMeshes)
for (int i = 0; i < textMeshes.Length; i++)
{
var textMesh = textMeshes[i];
if (Mathf.Approximately(duration, 0f))
{
switch (fadeMode)

6
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;

3
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 != "")
{

3
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);
}
}

12
Assets/Fungus/Scripts/Commands/SetCollider.cs

@ -30,15 +30,17 @@ namespace Fungus
{
// 3D objects
var colliders = go.GetComponentsInChildren<Collider>();
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<Collider2D>();
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);
}
}

16
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<Selectable>();
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;
}
}

8
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;
}

3
Assets/Fungus/Scripts/Commands/ShowSprite.cs

@ -41,8 +41,9 @@ namespace Fungus
if (affectChildren)
{
var spriteRenderers = spriteRenderer.gameObject.GetComponentsInChildren<SpriteRenderer>();
foreach (var sr in spriteRenderers)
for (int i = 0; i < spriteRenderers.Length; i++)
{
var sr = spriteRenderers[i];
SetSpriteAlpha(sr, _visible.Value);
}
}

4
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();
}
}

14
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;
}
}

3
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<iTween>();
foreach (var tween in tweens)
for (int i = 0; i < tweens.Length; i++)
{
var tween = tweens[i];
tween.time = 0;
tween.SendMessage("Update");
}

21
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<Block> GetConnectedBlocks()
{
var connectedBlocks = new List<Block>();
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++;

4
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)
{

3
Assets/Fungus/Scripts/Components/Clickable2D.cs

@ -41,8 +41,9 @@ namespace Fungus
// TODO: Cache these objects for faster lookup
var handlers = GameObject.FindObjectsOfType<ObjectClicked>();
foreach (var handler in handlers)
for (int i = 0; i < handlers.Length; i++)
{
var handler = handlers[i];
handler.OnObjectClicked(this);
}
}

3
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);
}
}

3
Assets/Fungus/Scripts/Components/DialogInput.cs

@ -115,8 +115,9 @@ namespace Fungus
if (nextLineInputFlag)
{
var inputListeners = gameObject.GetComponentsInChildren<IDialogInputListener>();
foreach (var inputListener in inputListeners)
for (int i = 0; i < inputListeners.Length; i++)
{
var inputListener = inputListeners[i];
inputListener.OnNextLineEvent();
}
nextLineInputFlag = false;

22
Assets/Fungus/Scripts/Components/Draggable2D.cs

@ -59,14 +59,16 @@ namespace Fungus
}
var dragEnteredHandlers = GetHandlers<DragEntered>();
foreach (var handler in dragEnteredHandlers)
for (int i = 0; i < dragEnteredHandlers.Length; i++)
{
var handler = dragEnteredHandlers[i];
handler.OnDragEntered(this, other);
}
var dragCompletedHandlers = GetHandlers<DragCompleted>();
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<DragExited>();
foreach (var handler in dragExitedHandlers)
for (int i = 0; i < dragExitedHandlers.Length; i++)
{
var handler = dragExitedHandlers[i];
handler.OnDragExited(this, other);
}
var dragCompletedHandlers = GetHandlers<DragCompleted>();
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<DragStarted>();
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<DragCompleted>();
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<DragCancelled>();
foreach (var handler in dragCancelledHandlers)
for (int i = 0; i < dragCancelledHandlers.Length; i++)
{
var handler = dragCancelledHandlers[i];
handler.OnDragCancelled(this);
}

146
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<Component>();
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<int> usedIds = new List<int>();
var blocks = GetComponents<Block>();
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<Command>();
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<Variable>();
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<Block>();
var commands = GetComponents<Command>();
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<EventHandler>();
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<MessageReceived>();
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<Block>();
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<Command>();
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<Block>();
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<Block>();
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<MessageReceived>();
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
/// </summary>
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
/// </summary>
public T GetVariable<T>(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
/// </summary>
public void SetVariable<T>(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
/// </summary>
public virtual List<Variable> GetPublicVariables()
{
List<Variable> publicVariables = new List<Variable>();
foreach (var v in variables)
var publicVariables = new List<Variable>();
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<Block>();
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<Command>();
foreach (var command in commands)
for (int i = 0; i < commands.Length; i++)
{
var command = commands[i];
command.hideFlags = HideFlags.HideInInspector;
}
var eventHandlers = GetComponents<EventHandler>();
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<MonoBehaviour>();
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<Command>();
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
/// </summary>
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<Block>();
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<Block>();
var blocks = GetComponents<Block>();
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;
}
}

35
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<Flowchart>();
foreach (var flowchart in flowcharts)
for (int i = 0; i < flowcharts.Length; i++)
{
var flowchart = flowcharts[i];
var blocks = flowchart.GetComponents<Block>();
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<string> languageCodes = new List<string>();
var languageCodes = new List<string>();
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
/// </summary>
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)

53
Assets/Fungus/Scripts/Components/MenuDialog.cs

@ -154,14 +154,16 @@ namespace Fungus
{
StopAllCoroutines();
Button[] optionButtons = GetComponentsInChildren<Button>();
foreach (var button in optionButtons)
var optionButtons = GetComponentsInChildren<Button>();
for (int i = 0; i < optionButtons.Length; i++)
{
var button = optionButtons[i];
button.onClick.RemoveAllListeners();
}
foreach (var button in optionButtons)
for (int i = 0; i < optionButtons.Length; i++)
{
var button = optionButtons[i];
if (button != null)
{
button.gameObject.SetActive(false);
@ -198,53 +200,43 @@ namespace Fungus
public virtual bool AddOption(string text, bool interactable, Block targetBlock)
{
bool addedOption = false;
foreach (var button in cachedButtons)
for (int i = 0; i < cachedButtons.Length; i++)
{
var button = cachedButtons[i];
if (!button.gameObject.activeSelf)
{
button.gameObject.SetActive(true);
button.interactable = interactable;
if (interactable && autoSelectFirstButton && !cachedButtons.Select((x) => x.gameObject).Contains(EventSystem.current.currentSelectedGameObject))
if (interactable && autoSelectFirstButton && !cachedButtons.Select(x => x.gameObject).Contains(EventSystem.current.currentSelectedGameObject))
{
EventSystem.current.SetSelectedGameObject(button.gameObject);
}
Text textComponent = button.GetComponentInChildren<Text>();
if (textComponent != null)
{
textComponent.text = text;
}
var block = targetBlock;
button.onClick.AddListener(delegate {
button.onClick.AddListener(delegate
{
EventSystem.current.SetSelectedGameObject(null);
StopAllCoroutines(); // Stop timeout
StopAllCoroutines();
// Stop timeout
Clear();
HideSayDialog();
if (block != null)
{
var flowchart = block.GetFlowchart();
#if UNITY_EDITOR
// Select the new target block in the Flowchart window
flowchart.SelectedBlock = block;
#endif
gameObject.SetActive(false);
// Use a coroutine to call the block on the next frame
// Have to use the Flowchart gameobject as the MenuDialog is now inactive
flowchart.StartCoroutine(CallBlock(block));
}
});
addedOption = true;
break;
}
@ -266,15 +258,14 @@ namespace Fungus
}
bool addedOption = false;
foreach (var button in CachedButtons)
for (int i = 0; i < CachedButtons.Length; i++)
{
var button = CachedButtons[i];
if (!button.gameObject.activeSelf)
{
button.gameObject.SetActive(true);
button.interactable = interactable;
Text textComponent = button.GetComponentInChildren<Text>();
var textComponent = button.GetComponentInChildren<Text>();
if (textComponent != null)
{
textComponent.text = text;
@ -283,17 +274,16 @@ namespace Fungus
// Copy to local variables
LuaEnvironment env = luaEnv;
Closure call = callBack;
button.onClick.AddListener(delegate {
StopAllCoroutines(); // Stop timeout
button.onClick.AddListener(delegate
{
StopAllCoroutines();
// Stop timeout
Clear();
HideSayDialog();
// Use a coroutine to call the callback on the next frame
StartCoroutine(CallLuaClosure(env, call));
});
addedOption = true;
break;
}
@ -373,8 +363,9 @@ namespace Fungus
{
get {
int count = 0;
foreach (var button in cachedButtons)
for (int i = 0; i < cachedButtons.Length; i++)
{
var button = cachedButtons[i];
if (button.gameObject.activeSelf)
{
count++;

11
Assets/Fungus/Scripts/Components/SayDialog.cs

@ -227,14 +227,14 @@ namespace Fungus
{
// Stop all tweening portraits
var activeCharacters = Character.ActiveCharacters;
foreach (var c in activeCharacters)
for (int i = 0; i < activeCharacters.Count; i++)
{
var c = activeCharacters[i];
if (c.State.portraitImage != null)
{
if (LeanTween.isTweening(c.State.portraitImage.gameObject))
{
LeanTween.cancel(c.State.portraitImage.gameObject, true);
PortraitController.SetRectTransform(c.State.portraitImage.rectTransform, c.State.position);
if (c.State.dimmed == true)
{
@ -283,14 +283,15 @@ namespace Fungus
// Dim portraits of non-speaking characters
var activeStages = Stage.ActiveStages;
foreach (var stage in activeStages)
for (int i = 0; i < activeStages.Count; i++)
{
var stage = activeStages[i];
if (stage.DimPortraits)
{
var charactersOnStage = stage.CharactersOnStage;
foreach (var c in charactersOnStage)
for (int j = 0; j < charactersOnStage.Count; j++)
{
var c = charactersOnStage[j];
if (prevSpeakingCharacter != speakingCharacter)
{
if (c != null && !c.Equals(speakingCharacter))

4
Assets/Fungus/Scripts/Components/SpriteFader.cs

@ -78,13 +78,13 @@ namespace Fungus
// Fade child sprite renderers
var spriteRenderers = spriteRenderer.gameObject.GetComponentsInChildren<SpriteRenderer>();
foreach (var sr in spriteRenderers)
for (int i = 0; i < spriteRenderers.Length; i++)
{
var sr = spriteRenderers[i];
if (sr == spriteRenderer)
{
continue;
}
FadeSprite(sr, targetColor, duration, slideOffset);
}

24
Assets/Fungus/Scripts/Components/Writer.cs

@ -107,8 +107,9 @@ namespace Fungus
if (textUI == null && inputField == null && textMesh == null)
{
var allcomponents = go.GetComponents<Component>();
foreach (var c in allcomponents)
for (int i = 0; i < allcomponents.Length; i++)
{
var c = allcomponents[i];
textProperty = c.GetType().GetProperty("text");
if (textProperty != null)
{
@ -120,8 +121,9 @@ namespace Fungus
// Cache the list of child writer listeners
var allComponents = GetComponentsInChildren<Component>();
foreach (var component in allComponents)
for (int i = 0; i < allComponents.Length; i++)
{
var component = allComponents[i];
IWriterListener writerListener = component as IWriterListener;
if (writerListener != null)
{
@ -690,8 +692,9 @@ namespace Fungus
{
WriterSignals.DoWriterInput(this);
foreach (var writerListener in writerListeners)
for (int i = 0; i < writerListeners.Count; i++)
{
var writerListener = writerListeners[i];
writerListener.OnInput();
}
}
@ -700,8 +703,9 @@ namespace Fungus
{
WriterSignals.DoWriterState(this, WriterState.Start);
foreach (var writerListener in writerListeners)
for (int i = 0; i < writerListeners.Count; i++)
{
var writerListener = writerListeners[i];
writerListener.OnStart(audioClip);
}
}
@ -710,8 +714,9 @@ namespace Fungus
{
WriterSignals.DoWriterState(this, WriterState.Pause);
foreach (var writerListener in writerListeners)
for (int i = 0; i < writerListeners.Count; i++)
{
var writerListener = writerListeners[i];
writerListener.OnPause();
}
}
@ -720,8 +725,9 @@ namespace Fungus
{
WriterSignals.DoWriterState(this, WriterState.Resume);
foreach (var writerListener in writerListeners)
for (int i = 0; i < writerListeners.Count; i++)
{
var writerListener = writerListeners[i];
writerListener.OnResume();
}
}
@ -730,8 +736,9 @@ namespace Fungus
{
WriterSignals.DoWriterState(this, WriterState.End);
foreach (var writerListener in writerListeners)
for (int i = 0; i < writerListeners.Count; i++)
{
var writerListener = writerListeners[i];
writerListener.OnEnd(stopAudio);
}
}
@ -740,8 +747,9 @@ namespace Fungus
{
WriterSignals.DoWriterGlyph(this);
foreach (var writerListener in writerListeners)
for (int i = 0; i < writerListeners.Count; i++)
{
var writerListener = writerListeners[i];
writerListener.OnGlyph();
}
}

13
Assets/Fungus/Scripts/Utils/TextTagParser.cs

@ -180,8 +180,9 @@ namespace Fungus
string paramsStr = input.Substring(index + 1);
var splits = paramsStr.Split(',');
foreach (var p in splits)
for (int i = 0; i < splits.Length; i++)
{
var p = splits[i];
paramsList.Add(p.Trim());
}
return paramsList;
@ -264,16 +265,14 @@ namespace Fungus
// These characters are usually added for legibility when editing, but are not
// desireable when viewing the text in game.
bool trimLeading = false;
foreach (var token in tokens)
for (int i = 0; i < tokens.Count; i++)
{
if (trimLeading &&
token.type == TokenType.Words)
var token = tokens[i];
if (trimLeading && token.type == TokenType.Words)
{
token.paramList[0] = token.paramList[0].TrimStart(' ', '\t', '\r', '\n');
}
if (token.type == TokenType.Clear ||
token.type == TokenType.WaitForInputAndClear)
if (token.type == TokenType.Clear || token.type == TokenType.WaitForInputAndClear)
{
trimLeading = true;
}

Loading…
Cancel
Save