2012年4月10日 星期二

使用 #pragma strict

簡單的在腳本頂部添加#pragma strict。
然後,unity將在腳本中禁用動態類型,強制使用靜態類型,如果一個類型未知。
Unity將報告編譯錯誤。那麼在這種情況下foo將在編譯時產生一個錯誤:

#pragma strict
function Start (){
     var foo = GetComponent(MyScript);
     //會出錯,必須改為 var foo:MyScript = GetComponent(MyScript);    
     foo.DoSomething();
}

公開變數、私有變數與全域變數

1.一般var的變數或public var 都是public(公開變數),是指從別的腳本也可以控制該變數
2.如果前面加上private為私有的變數,外部的腳本也讀不到
3.前面加上static為全域變數

創造了一個變數,名為someGlobal
// 'TheScriptName.js'
中的一個靜態變數
static var someGlobal = 5;

//
你可以在腳本內部像普通變數一樣讀取它
print(someGlobal);
someGlobal = 1;

也可以從另一個腳本讀取它,你需要使用這個腳本的名稱加上一個點和總體變數名。
print(TheScriptName.someGlobal);
TheScriptName.someGlobal = 10;

尋找物件的方法 各種的Find

尋找物件的方法有很多種
這邊我介紹幾種常用的

GameObject.Find("name");  //用名字尋找GameObject子物件
GameObject.FindWithTag("Tag");  //用Tag尋找物件(第一個物件)
GameObject.FindGameObjectsWithTag("Tag");  //用Tag尋找物件(全部物件)
Resources.FindObjectsOfTypeAll(typeof(colliderTest)) //尋找Objects

1.GameObject.Find
用名字尋找GameObject子物件
假如你的物件層級如下:





















腳本附加到theObject的物件裡

function Awake(){
      //取得 GameObject下的Capsule物件
      var Capsule:GameObject;
      Capsule=GameObject.Find("Capsule");
      print("Find="+playerer.name);

      //取得 GameObject下的Cylinder物件
      var Cylinder:GameObject;
      Cylinder=GameObject.Find("Cube/Cylinder");
      print("Find="+Cylinder.name);
}

2.GameObject.FindWithTag
新增Tag
















新增一個名稱叫 Tag

須先將GameObject加入Tag











於任一個GameObject裡加入腳本
可找到有Tag的物件,
function Awake(){
       var tagObject:GameObject;
       tagObject=GameObject.FindWithTag("Tag");
       print("FindWithTag="+tagObject.name);
}
如果同時有很多物件都是相同的Tag,FindWithTag只能找到第一個

3.GameObject.FindGameObjectsWithTag
如果想找到所有Tag的GameObject,必須使用FindGameObjectsWithTag
用法如下
function Awake(){
       var tagObject:GameObject[];
       //FindGameObjectsWithTag取得的是個GameObject陣列
       tagObject=GameObject.FindGameObjectsWithTag("Tag");
       //取得有幾個GameObject有"Tag"
       print("FindGameObjectsWithTag="+tagObject.Length);
       //取得所有GameObject有名稱
       for(var i:int=0;i<tagObject.Length;i++){
              print("FindGameObjectsName="+tagObject[i].name);
       }
}

4.Resources.FindObjectsOfTypeAll
使用Resources.FindObjectsOfTypeAll找到所有具有相同類或腳本名稱的物體

function Start(){
       //找尋Test這個腳本
       var TestObj:Object[]=Resources.FindObjectsOfTypeAll(typeof(Test));
       print(TestObj.Length);
       for(var i:int=0;i<TestObj.Length;i++){
              //我在Test裡加了一個Get_GameObject()的function 會回傳該腳本所屬的GameObject
              print(TestObj[i].Get_GameObject().name);
       }
}

2012年4月9日 星期一

關於OnTriggerStay

OnTriggerStay() 主要是用來知道觸發器碰到那些Collider
最常用的用途是射擊遊戲的子彈會碰到哪些敵人的Collider

首先相關的GameObject必須要有Collider這個屬性
作為觸發器的GameObject必須把 "Is Trigger"的屬性打開










於觸發器加入一個腳本 colliderTest.js

function OnTriggerStay  (other : Collider) {
        print(other.gameObject.name);
}

可判斷該觸發器碰到哪些Collider,再取得該物件的名字



如果想要碰觸到物件裡的某個腳本

function OnTriggerStay( other : Collider ) {
        // 於其他碰撞GameObject加入OtherScript
        // 取用OtherScript裡的DoSomething Function
        // 因為有可能OtherScript只加在幾個GameObject裡
        // 所以我們家上判斷碰到的GameObject必須有OtherScript
        if (other.GetComponent(OtherScript)) {
                other.GetComponent(OtherScript).DoSomething();
        }
}

theUnity3D部落格開張囉~~


theUnity3D部落格開張囉~~
這是一個分享討論Unity3D程式與功能的地方!!