【Unity學習筆記】掌握MoneBehavior中的重要屬性、方法
一、重要屬性
1-1.獲取自己依附的GameObject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
private void Start()
{
//Mono里已經封裝好了屬性gameObject
//可以通過gameObject屬性來獲取
//(this.是可以省略的,為了便於理解 在前面加上this)
//列印出它的名字
print(this.gameObject.name);
}
}
1-2.獲取自己依附的GameObject的位置資訊
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
private void Start()
{
//使用transform.出來
//(this.是可以省略的,為了便於理解 在前面加上this)
//獲取坐標
print(this.transform.position);
//獲取角度(歐拉角)
print(this.transform.eulerAngles);
//獲取縮放
print(this.transform.lossyScale);
//這種寫法和上面是一樣的
print(this.gameObject.transform);
}
}
1-3.設置腳本的 激活 與 失活
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
private void Start()
{
//(this.是可以省略的,為了便於理解 在前面加上this)
//激活
this.enabled = true;
//失活
this.enabled = false;
}
}
1-4.獲取別的遊戲對象的gameObject和transform
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
//因為兩個物體掛的都是Lesson3腳本
//所以聲明一個Lesson3類型的變數
public Lesson3 otherLesson3;
private void Start()
{
//獲取別的腳本對象依附的gameobject和transform資訊
//(this.是可以省略的,為了便於理解 在前面加上this)
print(otherLesson3.gameObject.name);
print(otherLesson3.transform.position);
}
}
列印結果:
二、重要方法
主要內容:得到依附遊戲對象上掛載的其它腳本
只要得到了場景中遊戲物體 或 它身上依附的腳本,那麼 就可以得到它的一切資訊
2-1.得到和自己依附在同一遊戲物體上的另一個腳本(很少用)
現有一個Lesson3物體,上面掛載了兩個腳本
腳本Lesson3程式碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
private void Start()
{
//因為我們知道要獲取的腳本是Lesson3_Test,所以直接使用Lesson3_Test類型的變數去接收它的返回值
Lesson3_Test t = null;
//得到和自己依附在同一遊戲物體上的另一個腳本
//1.根據腳本名獲取(很少用)
// 需要用里氏替換原則把返回值 as 成Lesson3_Test
// 如果沒找到""里的那個腳本,就默認返回null
t = this.GetComponent("Lesson3_Test") as Lesson3_Test;
print(t);
//2.根據Type獲取(反射的知識)
// 需要用里氏替換原則把返回值 as 成Lesson3_Test
t = this.GetComponent(typeof(Lesson3_Test)) as Lesson3_Test;
print(t);
//3.通過泛型獲取(用的最多,因為不用as了)
// 默認就返回Lesson3_Test類型,不需要as轉換了
t = this.GetComponent<Lesson3_Test>();
print(t);
}
}
運行:
2-2.得到自己掛載的多個腳本
現有一個Lesson3物體,上面掛載了兩個腳本
Lesson3腳本的程式碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
private void Start()
{
//1.用數組得到Lesson3上掛載的所有腳本
//需要用一個Lesson3類型的數組去接收返回值
Lesson3[] array = this.GetComponents<Lesson3>();
print(array.Length);
//2.用List得到Lesson3上掛載的所有腳本
//聲明一個Lesson3類型的List
List<Lesson3> list = new List<Lesson3>();
//用這個List去存獲取的結果
this.GetComponents<Lesson3>(list);
print(list.Count);
}
}
運行:
2-3.得到子對象掛載的腳本
(它默認也會找自己身上是否掛載了該腳本)
(兒子的兒子也會去找)
現有:
Lesson3腳本程式碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
private void Start()
{
//因為我們知道要獲取的腳本是Lesson3_Test,所以直接使用Lesson3_Test類型的變數去接收它的返回值
Lesson3_Test t = null;
//得到單個
//使用GetComponentInChildren<>來獲取子對象掛載的某個腳本
//()中的參數:是否檢測失活的,不填默認false
t = this.GetComponentInChildren<Lesson3_Test>(true);
print(t);
//得到多個
//使用GetComponentsInChildren<>來獲取子對象掛載的多個腳本
//方法一:用數組
//用一個Lesson3_Test類型的數組去接收返回值
Lesson3_Test[] array = this.GetComponentsInChildren<Lesson3_Test>(true);
print(array.Length);
//方法二:用List得到Lesson3的子對象上掛載的所有腳本
//聲明一個Lesson3類型的List
List<Lesson3_Test> list = new List<Lesson3_Test>();
//把獲取到的子對象的腳本裝入list
this.GetComponentsInChildren<Lesson3_Test>(true,list);
print(list.Count);
}
}
運行:
2-4.得到父對象掛載的腳本
(它默認也會找自己身上是否掛載了該腳本)
(父親的父親也會去找)
現有:
Lesson3腳本的程式碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
private void Start()
{
//因為我們知道要獲取的腳本是Lesson3_Test,所以直接使用Lesson3_Test類型的變數去接收它的返回值
Lesson3_Test t = null;
//得到單個
//使用GetComponentInParent<>來獲取父對象掛載的某個腳本
t = this.GetComponentInParent<Lesson3_Test>();
print(t);
//得到多個
//使用GGetComponentsInParent<>來獲取父對象掛載的多個腳本
//方法一:用數組
//用一個Lesson3_Test類型的數組去接收返回值
Lesson3_Test[] array = this.GetComponentsInParent<Lesson3_Test>();
print(array.Length);
//方法二:用List存Lesson3的父對象上掛載的所有腳本
//聲明一個Lesson3類型的List
List<Lesson3_Test> list = new List<Lesson3_Test>();
//把獲取到的父對象的腳本裝入list
this.GetComponentsInParent<Lesson3_Test>(true,list);
print(list.Count);
}
}
運行:
2-5.嘗試獲取腳本
之前在得單個腳本的時候,有可能沒得到 為null
為了保險起見,往往得到腳本後會先判斷它不為空 再進行邏輯處理
所以提供了一個更加安全的方法this.TryGetComponent<>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
private void Start()
{
//因為我們知道要獲取的腳本是Lesson3_Test,所以直接使用Lesson3_Test類型的變數去接收它的返回值
Lesson3_Test t = null;
//this.TryGetComponent<>會有一個bool型的返回值
//通過out把得到的腳本裝進t
if (this.TryGetComponent<Lesson3_Test>(out t))
{
print("成功獲取了");
}
}
}