一、重要屬性
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("成功獲取了");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/500080.html
標籤:其他
上一篇:變頻器的四大組成部分和作業原理
