Bu Derste Loop Yani Döngü Sistemini Göreceğiz.
Döngü Sisteminin 4 Yöntemi Vardır. Bunlar While , Do , For ,Foreach Döngüleridir.
Bunlardan İşinize Yarayanı Kullanabilirsiniz.
FOR DÖNGÜSÜ:
C#
using UnityEngine;
using System.Collections;
public class ForLoop : MonoBehaviour
{
int numEnemies = 3;
void Start ()
{
for(int i = 0; i < numEnemies; i++)
{
Debug.Log("Creating enemy number: " + i);
}
}
}
JS
#pragma strict
var numEnemies : int = 3;
function Start ()
{
for(var i : int = 0; i < numEnemies; i++)
{
Debug.Log("Creating enemy number: " + i);
}
}
WHİLE DÖNGÜSÜ:
C#
using UnityEngine;
using System.Collections;
public class WhileLoop : MonoBehaviour
{
int cupsInTheSink = 4;
void Start ()
{
while(cupsInTheSink > 0)
{
Debug.Log ("I've washed a cup!");
cupsInTheSink--;
}
}
}
JS
#pragma strict
var cupsInTheSink : int = 4;
function Start ()
{
while(cupsInTheSink > 0)
{
Debug.Log ("I've washed a cup!");
cupsInTheSink--;
}
}
DOWHİLE DÖNGÜSÜ:
C#
using UnityEngine;
using System.Collections;
public class DoWhileLoop : MonoBehaviour
{
void Start()
{
bool shouldContinue = false;
do
{
print ("Hello World");
}while(shouldContinue == true);
}
}
JS
#pragma strict
function Start()
{
var shouldContinue : Boolean = false;
do
{
print ("Hello World");
}while(shouldContinue == true);
}
FOREACH DÖNGÜSÜ:
C#
using UnityEngine;
using System.Collections;
public class ForeachLoop : MonoBehaviour
{
void Start ()
{
string[] strings = new string[3];
strings[0] = "First string";
strings[1] = "Second string";
strings[2] = "Third string";
foreach(string item in strings)
{
print (item);
}
}
}
JS
#pragma strict
function Start ()
{
var strings = ["First string", "Second string", "Third string"];
for(var item : String in strings)
{
print (item);
}
}