以上教學內容屬本人所有,轉載請「務必」事先告知並註明出處。
About 1005
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class IEnumeratorTest : MonoBehaviour {
private Text mytext;
void Start () {
mytext = GetComponent<Text>();
StartCoroutine(waittime(2f)); //執行 IEnumerator
}
IEnumerator waittime(float mytime){ //宣告 IEnumerator
yield return new WaitForSeconds(mytime); // 等待x秒
mytext.text = "已經過了兩秒鐘";
}
}
public class TestScript : MonoBehaviour {
void Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
FindObjectOfType<doscript>().DoSomeThing();
}
}
}
public class DoScript : MonoBehaviour {
private bool gameover = false;
private int Coins = 1;
private float Num = 0.2f;
public void DoSomeThing(){//注意,要宣告成Public
gameover = true;
Coins += 1;
Num += 0.1f;
//偵測是否有存回
Debug.Log (gameover);
Debug.Log (Coins);
Debug.Log (Num);
}
}
public class TestScript : MonoBehaviour {
//宣告跟外部腳本相同名稱
//並且在Unity介面指定該物件
//外部腳本需依附在其他物件上
public DoScript script;
void Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
//直接執行外部腳本的Function
script.DoSomeThing ();
}
}
}
public class DoScript : MonoBehaviour {
private bool gameover = false;
private int Coins = 1;
private float Num = 0.2f;
public void DoSomeThing(){//注意,要宣告成Public
gameover = true;
Coins += 1;
Num += 0.1f;
//偵測是否有存回
Debug.Log (gameover);
Debug.Log (Coins);
Debug.Log (Num);
}
}
static class GameData{
public static bool gameover = false;
public static int Coins = 1;
public static float Num = 0.2f;
}
public class TestManager : MonoBehaviour {
private bool gameover;
private int Coins;
private float Num;
void Start () {
//載入外部全域參數
gameover = GameData.gameover;
Coins = GameData.Coins;
Num = GameData.Num;
}
void Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
gameover = true;
Coins += 1;
Num += 0.1f;
//將運算好的參數存回全域參數
GameData.gameover = gameover;
GameData.Coins = Coins;
GameData.Num = Num;
//偵測是否有存回
Debug.Log ("GameManager.gameover="+GameData.gameover);
Debug.Log ("GameManager.Coins="+GameData.Coins);
Debug.Log ("GameManager.Num="+GameData.Num);
}
}
}