✨
Unity
  • 甚麼是Game Engine
  • 安裝Unity
  • 旋轉的方塊
  • Probuilder
  • 飄移的膠囊
  • 第三人稱視角角色控制
    • Sprint
  • Dual Sense
  • W3School(C#)
  • Variable, Operand&Operator, Data Type(C#)
  • For Loop (C#)
  • 物件導向觀念基礎 OOP
  • Rigidbody C#
  • NPC Navigation
Powered by GitBook
On this page
  • 迴圈基礎
  • 範例程式
  • 執行結果ˇ
  • 迴圈謎題練習
  • 題目1
  • 參考答案
  • 題目2
  • 題目3
  • 題目4

For Loop (C#)

迴圈

PreviousVariable, Operand&Operator, Data Type(C#)Next物件導向觀念基礎 OOP

Last updated 1 year ago

迴圈基礎

範例程式

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

LogoMicrosoft OneDrive