1. 程式人生 > >unity 基礎代碼

unity 基礎代碼

顏色 mouse [] ans get layer inpu ldo 動畫播放

1.通過名字找物體
GameObject.Find("Player")
2.通過名字尋找物體子集
transform.FindChild("物體子集名字")
3. 顯示和隱藏物體
gameObject.SetActive(true); ?
4.使用和關閉物體代碼
gameObject.GetComponent<代碼名字>().enable=true;
5.按下動畫事件 if (Input.GetMouseButton(0))
按下事件   if (Input.GetMouseButtonDown(0))
擡起事件   if (Input.GetMouseButtonUp(0))
6.銷毀一樣標簽的物體
GameObject [] objs = GameObject.FindGameObjectsWithTag("標簽名");
foreach(GameObject obj in objs){
Destroy(obj);
}
7.物體水平勻速移動
transform.Translate(-0.1f*Time.deltaTime,0,0);
8.<<是除以2,>>是乘以2
9.位置相關的: 5個和位置相關的Vector3類型變量
up表示世界坐標的Y軸方向
right表示世界坐標的X軸方向
forward表示世界坐標的Z軸方向
position?表示對象在世界坐標系中的位置
localPosition?表示對象相對父節點變換的坐標位置
10.改變物體顏色
obj.renderer.material.color=Color.red
11.2個Quaternion類型的變量
rotation?在世界坐標系中的旋轉角度,是一個Quaternion對象(四元數)
localRotation?相對於父節點的旋轉角度
12.父子節點相關的:
parent?變量表示Transform的父節點
Transform Find(string name)?根據名字查找子節點
bool IsChildOf(Transform node)?判斷該Transform是否某Transform的子節點
void DetachChildren()?解除所有子節點的父子關系
13.鼠標:
Input.mousePosition表示鼠標當前的像素位置(坐標系如何?以左下角為原點)

接下來這三個函數的參數,0左鍵,1右鍵,2中鍵
GetMouseButton?對應的鍵處於按下狀態時返回true
GetMouseButtonDown?對應的鍵被按下時返回true
GetMouseButtonUp?對應的鍵彈起時返回true
14.
OnMouseDown?當鼠標點擊到對象的時候回調
OnMouseDrag?當鼠標拖拽對象時調用,在Ignore Raycast層上無效
OnMouseEnter?當鼠標進入對象時調用
OnMouseExit?當鼠標離開對象時調用
OnMouseOver?當鼠標停留在對象上面時調用
OnMouseUpAsButton?鼠標在同一個對象上按下,並彈起時調用
OnMouseUp?跟樓上一樣
15.gameObject.transform.rotation
= new Quaternion (Player.transform.rotation.x, Player.transform.rotation.y, Player.transform.rotation.z, Player.transform.rotation.w);
gameObject和Player旋轉角度一樣!
16. 倆個物體之間的距離 float Distance
float Distance = Vector3.Destance(position1,position2);
17. 如果玩家和敵人的距離小於10,執行{}代碼
if( Vector3.Destance(敵人.position,玩家.position)<10f){
//? 執行相應代碼
}
18.Animation動畫
01.倒回播放 Animation.Rewind();
02.停止所有動畫 Animation.Stop();
停止動畫 Animation.Stop(“動畫名字”);
03.使用動畫循環模式 Animation.WropMode= WropMode.loop;
04.等待動畫播放完成 animation.Play();???? yiel WaitForSeconds (animation.Clip.Length);
05.打印動畫長度 print(animation["動畫名字"].Length);
19.數組
foreach(Texture2D[] ta in 當前數組){
數組長度+=ta.Length; //??加載數組的長度
}

unity 基礎代碼