For Loop (C#)
迴圈
迴圈基礎
範例程式
using UnityEngine;
public class GameManager : MonoBehaviour
{
public GameObject SpherePrefab;
void Start()
{
float length = 1.5f;
int count = 5;
for (int j = 0; j < count; j++)
{
for (int i = 0; i < count; i++)
{
GameObject go = Instantiate(SpherePrefab);
go.transform.position = new Vector3(i * length - count / 2 * length,0 ,j * length - count / 2 * length);
}
}
}
}
執行結果ˇ

迴圈謎題練習

題目1

參考答案
using UnityEngine;
public class GameManager : MonoBehaviour
{
public GameObject SpherePrefab;
void Start()
{
float length = 1.5f;
int count = 5;
for (int j = 0; j < count; j++)
{
for (int i = 0; i <= j; i++)
{
GameObject go = Instantiate(SpherePrefab);
go.transform.position = new Vector3(i * length - count / 2 * length,0 ,j * length - count / 2 * length);
}
}
}
}
題目2

題目3

題目4

Last updated