使用Unity內的有效化(Enable)與無效化(Disable)指令,可以簡單地控制元件是否有效。
以下的範例中,會使用腳本控制場景中Light元件的Enable。
using UnityEngine;
using System.Collections;
public class ScopeAndAccessModifiers : MonoBehaviour
{
////(1)
public int alpha;////(3)
private int beta = 0;////(4)
private int gama = 5;
void Example(int pens, int crayons)////(2)
{
int answer;
answer = pens * crayons * alpha;
Debug.Log(answer);
}
void Start(){}
void Update ()
{
Debug.Log("Alpha is set to:" + alpha);
}
////
}
(1)這是一個名為ScopeAndAccessModifiers的類別, 在括號中所有內容都為該類別的區域(local)變數或函式。using UnityEngine;
using System.Collections;
public class IFStatements : MonoBehaviour
{
public float coffeeTemperature = 85.0f;
private float hotLimitTemperature = 70.0f;
private float coldLimitTemperature = 40.0f;
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
TemperatureTest();
coffeeTemperature -= 5.0f * Time.deltaTime;
}
void TemperatureTest()
{
if (coffeeTemperature > hotLimitTemperature)////(1)
{
Debug.Log("Coffee is too hot.");
}
else if (coffeeTemperature < coldLimitTemperature)////(2)
{
Debug.Log("Coffee is too cold.");
}
else////(3)
{
Debug.Log("Coffee is just right.");
}
}
}
using UnityEngine;
using System.Collections;
public class CSharpSyntax : MonoBehaviour {
void Start ()
{
}
void Update ()
{
}
}
而Javascript的類別則是隱藏,開發者輸入的程式碼會自動在此類別之下。#pragma strict
function Start ()
{
}
function Update ()
{
}
using UnityEngine;
public class BasicSyntax : MonoBehaviour
{
void Start ()
{
Debug.Log(transform.position.x);
}
}
撰寫方式就像寫地址一樣,假設把Debug當作是國家,打上 . 後接著Log就是在Debug之下的城市。using UnityEngine;
using System.Collections;
public class ScriptTest : MonoBehaviour {
int myInt, newInt;
float myFloat = 5.0f;
}
首先看到 int myInt, newInt; 那一行。using UnityEngine;
using System.Collections;
public class ScriptTest : MonoBehaviour {
float myFloat = 5.0f;
void Start() {
Debug.Log(myFloat);
}
}
Start函式會在遊戲專案運行最初,執行一次函式裡的內容,遊戲連結 海龜湯的玩法是由出題者提出一個難以理解的事件,參與猜題者可以提出任何問題以試圖縮小範圍並找出事件背後真正的原因。但出題者僅能以「是」、「不是」或「沒有關係」來回答問題。 本遊戲蒐集各種論壇、平台的42個題目,提供給想玩海龜湯卻愁找不到題目的你們。 ...