Unity_常用的一些方法記錄
- 1.通過物體名字查找游戲物體
- 2.查找物件中的組件
- 3.呼叫其他物件下組件中的方法
- 4.列印資料
- 5.獲得/設定物體的locaiton、rotation、scale
- 6.兩點間的距離
- 7.unity與web間(webgl/webplayer)的傳輸:傳出、傳入
- 8.lerp插值運算
- 9.修改物體的材質
- 10.將變數暴露給編輯器,方便在編輯器中手動修改
1.通過物體名字查找游戲物體
GameObject.Find(“Cube”);//查找名為Cube的游戲物體
GameObject.Find(“GameObject/Canvas/Text”);//可以指定路徑位置
2.查找物件中的組件
GameObject.GetComponent<組件名稱>();
3.呼叫其他物件下組件中的方法
GameObject.Find(“腳本所在的物體的名字”).SendMessage(“函式名”); //能呼叫public和private型別函式
GameObject.Find(“腳本所在的物體的名字”).GetComponent<腳本名>().函式名(); //只能呼叫public型別函式
4.列印資料
Debug.Log(“列印內容”);
5.獲得/設定物體的locaiton、rotation、scale
- 位置:
Vector3 player_postion = this.transform.position;// 獲取Player變數指定的物件的(X,Y,Z)
// 獲取X,Y,Z值
float x = player_postion.x;
float y = player_postion.y;
float z = player_postion.z;
//設定位置
// 1.直接賦值
this.transform.position = player_postion;
// 2.在某GameObject的基礎上加
this.transform.position = new Vector3(player_postion.x, player_postion.y + 7.79F, player_postion.z - 15);
//或者是
this.transform.position = player_postion + new Vector3(0, 7.79F, -15);
- 旋轉:
this.transform.eulerAngles //獲得旋轉
this.transform.localEulerAngles=new Vector3(float Pitch,float Yaw,float Roll); //設定絕對旋轉
- 縮放:
this.transform.localScale //獲得縮放
this.transform.localScale = new Vector3(x, y,z);//修改縮放
6.兩點間的距離
Vector3.Distance(a,b) //a和b是兩個點的坐標position值
7.unity與web間(webgl/webplayer)的傳輸:傳出、傳入
- 傳出:
在unity中寫如下呼叫陳述句:
Application.ExternalCall( “SayHello”, “The game says hello!” );
在html中定義函式如下
function SayHello(args){
alert(args);
}
- 傳入:
在unity中定義函式如下,并將帶有FunctionName函式的腳本系結到Main Camera中
HTML中寫:
SendMessage(“Main Camera”, “FunctionName”, 引數);//向unity中的Main Camera物件下的FunctionName函式傳入引數
SendMessage方法只能傳遞一個引數,不然會報錯,所以通常傳遞多個引數的時候,將資料組成json資料,以string的形式傳入
8.lerp插值運算
從A點過渡到B點
Transform.position = Vector3.Lerp(A點坐標, B點坐標, alpha);//alpha過渡值:0時取A,1時取B,將alpha從0過渡到1即可
9.修改物體的材質
this.GetComponent().material = 新材質;
10.將變數暴露給編輯器,方便在編輯器中手動修改
C#中將變數設定為公開:
public flaot f;
public Vector3 position;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/182958.html
標籤:其他
上一篇:Mac OS啟動Kafka
下一篇:PTA題目:順序表---插入結點
