Unity課程

Layer 系列

Layer 主要是大範圍分組 如 樹 人 之類的

Tag 算是小範圍分組 如這個敵人 是龍 是人 是蟲


Prefab 系列

用於快速複製相同物 使用apply all 可以一次改變所有同Prefab 


C#部分

[hideInInSpector]

可以隱藏參數(設定public的話會在物件中出現)


out & ref

out 可以不先設定初始值 out的意思是 最後會變成

也就是

void function(){

int a;

setA(out a); //回到這邊時 a = 20

}

int setA(out int a){

a = 20;

}


封裝進階

int _ATK = 10;

public int ATK{

    get{

    return _ATK;

    }

    set{

    _ATK = value;

    }

}

呼叫

Weaponclass Weapon = new Weaponclass()

Weapon.ATK = 30; // ATK=30

Weapon.ATK //get Weapon.ATK


public struct TestStruct{

    public int int_1;


    public TestStruct(int test){

    int_1 = test;

    }

}

呼叫後 由於飾struct 因此不會更改值

public class use{

    

    void use{

    TestStruct TS = new TS(10);

    TS.int_1 = 90; //結果TS還是等於10 因為只有傳值

    }

    

}

靜態類別通常用於場景轉換,用於存取上一狀態    


Unity 生命週期

Awake Void Awake()

Onable Void OnEnable()

Start Void Start()

FixUpdate 跟物理執行有關係 Void FixUpdate()

Update Void Update()

Yield 跟協同有關係

LateUpdate

刪除指令

Ondisable Void Ondisable()

Ondistroy Void Ondistroy()


Rigidbody剛體 互動放在FixUpdate中

//在start的時候取得發射物以及子彈

private Rigidbody rigidbody;

rigidbody = GetComponent<Rigidbody>();

private Shoot shootObj;

shootObj =  GetComponentInChildren<Shoot>();

//左右移動

float x =  Input.GetAxis("Horizontal");

float y =   Input.GetAxis("Vertical");

//按下滑鼠左鍵

if (Input.GetMouseButton(0))shootObj.shoot();

//設定力的方向

Vector3 force = new Vector3(0 * speed ,0,0* speed);

//先取得剛體

private Rigidbody shootRigibody;

shootRigibody = GetComponent<Rigidbody>();


shootRigibody.isKinematic = false;

shootRigibody.AddForce(shootSpeed);





留言

這個網誌中的熱門文章

無法載入檔案或組件 'System.IO.Compression' 或其相依性的其中之一。

MongoDB 入門

javascript 更改屬性及創建標籤