一、 C# 腳本的執(zhí)行入口函數(shù)
在 C# 腳本中控制 游戲物體 GameObject 運(yùn)動 , 要先獲取該物體 , 然后 修改其 Transform 組件的屬性 ;
在 游戲開始運(yùn)行后 , 會自動執(zhí)行 游戲物體 GameObject 上的 C# 組件代碼 , 程序入口是 MonoBehaviour#Start() 函數(shù) ;
在 C# 腳本中 , 主要的內(nèi)容都在 Start() 函數(shù) 中實(shí)現(xiàn) ;
using System.Collections.Generic;
using UnityEngine;
public class BehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Unity 腳本入口 , 啟動加載時調(diào)用");
}
// Update is called once per frame
void Update()
{
}
}
二、 獲取當(dāng)前游戲物體及物體名稱
在 C# 腳本中 , 游戲物體類型是 GameObject , 可以通過調(diào)用 this.gameObject
獲取當(dāng)前 C# 腳本附著的物體 , 代碼如下 :
// 獲取當(dāng)前組件附著的 游戲物體 GameObject
GameObject gameObject = this.gameObject;
獲取 游戲物體 GameObject 的名稱 , 調(diào)用 GameObject 類的 name 屬性 , 即可獲取當(dāng)前物體的名稱 ;
// 獲取當(dāng)前組件附著的 游戲物體 GameObject 名稱
string name = gameObject.name;
完整代碼如下 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// 打印日志
Debug.Log("Unity 腳本入口 , 啟動加載時調(diào)用");
// 獲取當(dāng)前組件附著的 游戲物體 GameObject
GameObject gameObject = this.gameObject;
// 獲取當(dāng)前組件附著的 游戲物體 GameObject 名稱
string name = gameObject.name;
Debug.Log("C# 腳本附著游戲物體的名稱 : " + name);
}
// Update is called once per frame
void Update()
{
}
}
回到 Unity 編輯器 , 會自動編譯 修改后的 C# 腳本 ;
該 C# 腳本 已經(jīng)被附著到了 立方體上 , 在 Unity 編輯器 工具欄 中 , 點(diǎn)擊 運(yùn)行按鈕 , 即可執(zhí)行該 C# 腳本 ;
三、 獲取游戲物體的 Transform 組件數(shù)據(jù)
獲取 游戲物體 GameObject 的 Transform 組件 , 調(diào)用 GameObject 對象的 transform 屬性 即可 ;
// 獲取當(dāng)前組件附著的 游戲物體 GameObject 的 Transform 組件
Transform transform = gameObject.transform;
打印 Transform 組件的 位置 , 旋轉(zhuǎn)量 , 縮放倍數(shù) 屬性 :
// 獲取 Transform 組件的 位置 , 旋轉(zhuǎn)量 , 縮放倍數(shù)
Debug.Log("C# 腳本附著游戲物體的 Transform 組件數(shù)據(jù) 位置 : " + transform.position
+ " , 旋轉(zhuǎn)量 : " + transform.rotation + " , 縮放倍數(shù) : " + transform.localScale);
完整代碼如下 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// 打印日志
Debug.Log("Unity 腳本入口 , 啟動加載時調(diào)用");
// 獲取當(dāng)前組件附著的 游戲物體 GameObject
GameObject gameObject = this.gameObject;
// 獲取當(dāng)前組件附著的 游戲物體 GameObject 名稱
string name = gameObject.name;
Debug.Log("C# 腳本附著游戲物體的名稱 : " + name);
// 獲取當(dāng)前組件附著的 游戲物體 GameObject 的 Transform 組件
Transform transform = gameObject.transform;
// 獲取 Transform 組件的 位置 , 旋轉(zhuǎn)量 , 縮放倍數(shù)
Debug.Log("C# 腳本附著游戲物體的 Transform 組件數(shù)據(jù) 位置 : " + transform.position
+ " , 旋轉(zhuǎn)量 : " + transform.rotation + " , 縮放倍數(shù) : " + transform.localScale);
}
// Update is called once per frame
void Update()
{
}
}
回到 Unity 編輯器后 , 會自動編譯上述修改后的 C# 腳本代碼 ;
點(diǎn)擊 Unity 編輯器 工具欄 右側(cè)的 運(yùn)行按鈕 , 即可運(yùn)行該 C# 組件 ;
四、UnityEngine 命名空間簡介
Unity 中的 C# 腳本 , 都是 繼承 MonoBehaviour 類 ;
public class BehaviourScript : MonoBehaviour
{
}
MonoBehaviour 類是在 UnityEngine 命名空間 下定義的 ;
using UnityEngine;
在本博客中所有用到的關(guān)于 Unity 的類 , 如文章來源:http://www.zghlxwxcb.cn/news/detail-411315.html
- Debug
- GameObject
- Transform
都定義在 UnityEngine 命名空間中 ;文章來源地址http://www.zghlxwxcb.cn/news/detail-411315.html
到了這里,關(guān)于【Unity3D】Unity 腳本 ③ ( C# 腳本的執(zhí)行入口函數(shù) | 獲取當(dāng)前游戲物體及物體名稱 | 獲取游戲物體的 Transform 組件數(shù)據(jù) | UnityEngine 命名空間簡介 )的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!