unity學習總結知識大全

---date: 2017-07-17 13:01status: publictitle:

---游戲物體添加tag值,獲取taggameObject.tag//設置游戲對象的tag值gameObject.tag="Player"

調用transform組件的三種方式(1) gameObject.GetCompenent()(2) gameObject.transform(3) transformGameObject:

游戲對象類兩種獲取游戲對象的方式(1) 通過名字獲取:GameObject.Find(“”)(2) tag值來獲取: GameObject.FindGameObjectWithTag(“”)//設置游戲物體的位置t1.position = new Vector3(2,3,4);

獲取一個組件:游戲物體.GetComponent<組件名>()

腳本生命周期【1】 Awake: 加載的時候調用【2】 OnEnable: 激活的時候調用【3】 Start: 腳本開始運行的時候調用【4】 Update: 每幀都調用【5】 LateUpdate: Update執行結束之后調用【6】 OnDisable: 腳本不激活的時候調用【7】 OnDestory: 腳本銷毀的時候調用

Time.delteTime: 上一次LateUpdate結束到這次Update開始的時間間隔監聽鍵盤的輸入

1 Input.GetKey(KeyCode):某個按鍵持續按下的時候出發

2 Input.GetKeyDown(KeyCode):某個按鍵按下的時候觸發

3 Input.GetkeyUp(KeyCode):

某個按鍵彈起的時候觸發監聽鼠標的輸入1 Input.GetMouseButtonDown():

鼠標按下2 Input.GetMouseButtonUp():

鼠標彈起3 Inpup.GetMouseButton():

鼠標持續點擊字母數字小數字鍵盤鼠標KeyCode.WKeyCode.Alpha10:KeyCode.Keypad00:

左鍵KeyCode.AKeyCode.Alpha21:KeyCode.Keypad11:

右鍵KeyCode.SKeyCode.Alpha32:KeyCode.Keypad22:

中鍵……………………小數字鍵盤為: KeyCode.Keypad1上下左右鍵為KeyCode.UpArrow/DownArrow/LeftArrow/RightArrow

獲取水平方向跟豎直方向的虛擬軸偏移量float vertical = Input.GetAxis("Vertical");float horizontal = Input.GetAxis("Horizontal");

剛體組件RigidBody:剛體組件,用來模仿現實世界的物理效果(參數:質量,空氣阻力,角阻力,約束(位置跟旋轉))給剛體組件設置一個速度:velocity施加爆炸力作用:Rigidbody對象.AddExplosionForce(force,position,radius)

預設體生成預設體:將場景的游戲物體拖拽到Asset當中即生成預設體根據預設體生成游戲對象:GameObject.Instantiate(prefab,position,rotattion)? Quaternion.identity:旋轉角度都為

碰撞事件發生條件:兩個物體必須要有碰撞器至少其中要有一個剛體組件兩個物體要有相對位移//碰撞事件剛發生的時候void OnCollisionEnter(Collision other){? ? print("OnCollisionEnter");}//碰撞事件持續的時候void OnCollisionStay(Collision other){? ? print("OnCollisionStay");}//碰撞事件結束的時候void OnCollisionExit(Collision other){? ? print("OnCollisionExit");}

觸發器觸發事件:觸發事件也是在觸發的過程當中回調某些函數發生條件:兩個物體必須有碰撞器,至少其中一個為觸發器(勾選上IsTrigger)要有剛體組件相對位移

//觸發事件剛發生的時候void OnTriggerEnter(Collider other){? ? print("OnTriggerEnter");}

//觸發事件持續的時候void OnTriggerStay(Collider other){? ? ? ? print("OnTriggerStay"); }

//觸發事件結束的時候void OnTriggerExit(Collider other){? ? ? ? print("OnTriggerExit");}

實現物體位移、旋轉的兩種方式:

//獲取水平方向跟豎直方向的虛擬軸偏移量float vertical = Input.GetAxis("Vertical");float horizontal = Input.GetAxis("Horizontal")

;//位移1transform.Translate(Vector3.forward*speed*vertical*Time.deltaTime);

//位移2transform.position +=Vector3(x,y,z);

//旋轉transform.Rotate(Vector3.up*rotateSpeed*horizontal*Time.deltaTime);

限制游戲物體的坐標范圍例:x軸[-4.5,4.5] Y[-4.5,4.3]Vector3 tempPos = transform.position;tempPos.x = Mathf.Clamp(tempPos.x,-4.5f,4.5f);tempPos.y = Mathf.Clamp(tempPos.y,-4.5f,3.1f);transform.position = tempPos;

銷毀對象Destroy(this.gameObject);

音頻組件:AudioSourceAudioSource shootAS = gameObject.GetComponent();

//播放

shootAS.Play();

UI界面

//屏幕寬度:Screen.width

//屏幕高度:Screen.height

//在屏幕顯示的區域:

Rect scoreRect = new Rect(x,y,width,height);

//文本

GUI.Label(Rect,String);

GUI.Label(Rect,String,GUIStyle);

//文本樣式

GUIStyle textStyle = new GUIStyle();

textStyle.normal.textColor = Color.white;

textStyle.fontSize = 25;

textStyle.alignment = TextAnchor.MiddleCenter;


獲取子物體的父物體對象

子物體.tansform.parent.gameObject


獲得父物體的子物體對象

父物體.transform.FindChild("子物體name");


Unity UI控件

//給按鈕添加一個監聽事件

ReStartBtn.onClick.AddListener(ReStartBtnClickListener);

//按鈕點擊響應的方法

public void ReStartBtnClickListener(){

//print("重新開始游戲");

//重新加載整個場景

SceneManager.LoadScene(0);

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容