Project(工程、項目):工程是把游戲開發當前所需要的資源歸類管理用的。
Console控制台:日志、報錯、調試,右上角,消息過濾
Assets:資源,存儲游戲中一切用到的資源
Library:臨時庫文件
ProjectSettings:項目設置、項目控制
Temp:臨時文件夾
UnityPackageManager:資源包

工程遷移:
Assets、ProjectSettings、UnityPackageManager,這三個文件夾必須拷貝走
如果報錯,先刪掉Library和Temp,再重新打開Unity
dependencies關聯資源,引用資源

project面板:管理所有項目資源和文件
Inspector:當前選中游戲物體細節
程序=數據結構+算法
數據結構:存儲數據的結構,棧、堆、鏈
List的核心是數組,
.unity后綴名:場景文件
.unitypackage后綴名:資源包
偏好設置
MonoDevelop:微軟出品,早年為了實現跨平台(Mac),后來VS2017支持Mac

組件的概念:將一些具有功能性的代碼封裝起來,封裝成一個類,當這個類掛到游戲物體身上去的時候,就相當於對此類的實例化,變成了具有這個功能的對象實體,那么這個游戲物體也就具有了這個對象所具有的功能。
組件的掛載相當於創建對象,添加功能性的對象。
游戲物體:
從表現層來看:場景視圖中或者層級視圖中能被看到的或者能被選中的都叫游戲物體。
從面向對象的角度來看:游戲物體是一個或者多個組件的集合。
Transform是基本組件,即使GameObject空物體也要有基本組件。坐標系,朝向,形體,
Position:單位米
Rotation:單位Unity(歐拉角,角度)和C#(四元素,弧度)
右手坐標系(數學運算)和左手坐標系(幾何變換):拇指X軸右,食指Y軸上,中指Z軸前
Scale:單位倍數
Reset:重置還原
MonoBehaviour:是每個腳本派生的類的基類。 Start、Updata(生命周期函數、事件函數):在特定的時間點被游戲引擎被動調用的函數叫做生命周期函數。 Unity分為編輯內存和運行內存 第一步實例化所有游戲物體的組件 第二步激活所有游戲物體的組件 Awake():在此組件被實例化的時候執行一次 OnEnable():在此組件被激活的時候執行一次 OnDisable():在此組件被失活的時候執行一次 Start():在此組件對象第一次被激活的時候執行一次(在游戲開始的時候被執行一次,用來初始化) Updata():每一畫面刷新幀被調用一次(在游戲物體和腳本組件被激活的情況下) FixedUpdate():每一物理幀更新刷新一次(物理幀是固定幀0.02秒) LateUpdate():每一畫面刷新幀被調用一次,只不過比Updata要晚(常用於攝像機跟隨,防抖)OnDestroy():在此組件被銷毀(remove)的時候執行一次(游戲運行時,在運行內存中銷毀,編輯內存仍在) 幀(Frame):屏幕刷新的表示方式 幀率(FPS):幀率 = 幀數 / 時間 (單位時間內畫面刷新的幀數) 肉眼:24 手游:30~60 PC/主機游戲:60~120 VR:120以上
collapse折疊調用,顯示調用次數
整個游戲物體的激活與失活,游戲物體被激活失活時,會同步激活失活游戲物體的組件
單個物體組件的激活與失活
Mesh網格,正方形4個頂點2個三角面,平面10x10米,面片1x1米
Snap settings 吸附
給物體添加圖標
Unity3D 快捷鍵 當前軸心旋轉:Alt+鼠標左鍵 移動場景:鼠標滾輪鍵 / Q 縮放場景:使用鼠標滾輪鍵 / Alt+鼠標右鍵 居中所選物:F 飛行瀏覽:鼠標右鍵+WSAD(按Shift加速),QE上下 Edit菜單 播放:Ctrl+P,暫停:Ctrl+Shift+P,單幀預覽:Ctrl+Alt+P Assets菜單 刷新:Ctrl+R GameObject菜單 新建空對象:Ctrl+Shift+N,移動物體到視圖中心點:Ctrl+Alt+F,移動物體與視圖對齊:Ctrl+Shift+F Component菜單 添加組件:Ctrl+Shift+A 近距離查看游戲對象 在Hierarchy視圖中選擇游戲對象,然后在Scene視圖中按快捷鍵“F”來近距離查看該游戲對象。 游戲對象不在主攝像頭中 Hierarchy中雙擊選擇需要顯示的游戲對象,再單擊Main Camera選中,最后Ctrl+Shift+F鍵盤即可。 旋轉視圖 Alt鍵 + 鼠標左鍵 可以任意拖動鼠標來旋轉視圖。如果x,y,z坐標軸恢復不了,可以先單擊X軸(Y、Z)再鼠標右鍵選擇“Free” 最后通過這個來調試。 縮放視圖:滾動鼠標中鍵可以縮放整體視圖。 變換工具欄:拖動工具:Q 移動工具:W 旋轉工具: E 縮放工具:R 按住Ctrl鍵移動物體會以一定的增量來移動物體 Edit-Snap Setting 柵格和捕捉設置 Ctrl+Shift 使物體在另一個物體上移動,物體在被相交的物體上移動 頂點吸附 選中物體,按鍵盤V鍵 就可以看到物體的頂點:可以快速使兩個物體定位 關閉頂點吸附:Shift+V
using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable]//類和結構都需要被序列化后,才可以在檢視面板顯示 public class Student { public string name; public int age; public override string ToString() { return string.Format("[name:{0},age:{1}]",name ,age); } }
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable] public class Unity1803 : IEnumerable { List<Student> students;//我沒有加public,老師加了public public Unity1803() { students = new List<Student>() { new Student() { name = "張三",age= 20}, new Student() { name = "李四",age= 18}, new Student() { name = "王二",age= 22}, new Student() { name = "趙六",age= 21}, new Student() { name = "田七",age= 20}, }; } public IEnumerator GetEnumerator() { //返回的對象 必須實現了IEnumerator接口 Unity1803IEnumerator ie = new Unity1803IEnumerator(students); return ie; //簡便寫法 //yield是C#為了簡化遍歷操作實現的語法糖,我們知道如果要要某個類型支持遍歷就必須要實現系統接口IEnumerable,這個接口后續實現比較繁瑣要寫一大堆代碼才能支持真正的遍歷功能。 //for (int i = 0; i < students.Count; i++) //{ // yield return students[i]; //} } public class Unity1803IEnumerator : IEnumerator { List<Student> students; int index = -1; public Unity1803IEnumerator(List<Student> students) { this.students = students; } public object Current { get { return students[index]; } } //是否移出了集合的范圍 返回true 表示當前的指針還在范圍內 false則是在范圍外 public bool MoveNext() { index++; return index < students.Count; } public void Reset() { index = -1; } } }
Unity復習
UnityEngine.Object是System.Object的子類,Unity默認繼承UnityEngine.Object,C#默認繼承System.Object
組件掛載的要求
1、繼承MonoBehaviour類
2、文件名與類名一致
3、掛載時,所有腳本代碼沒有編譯錯誤
4、掛載的腳本不能是抽象類
using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable]//類和結構都需要被序列化后,才可以在檢視面板顯示 public class Enemy { public string name; public int atk; public int hp; }
using System.Collections; using System.Collections.Generic; using UnityEngine; public enum EEnemyType { 戰士, 法師 } public class NewBehaviourScript : MonoBehaviour { //以檢視面板為准 public int intNum = 0; //私有變量不會在檢視面板顯示 //特性:[]標簽的形式 //可以把私有的字段在檢視面板中顯示出來 [SerializeField]//序列化字段 Attribute 反射(程序集.exe .all) private float floatNum = 0.0f; [Multiline(4)]//讓字符串多行顯示 public string str = ""; [Header("戰士的類型")]//說明文本 public EEnemyType enemyType = EEnemyType.戰士; [Header("XX的數量")] public int[] nums; [Header("XX的數量")] public List<int> listArray; public Student student; public List<Enemy> enemyList; public Enemy enemy = new Enemy(); public Unity1803 unity = new Unity1803(); private void Awake() { //Debug.LogFormat("Int :{0}", intNum); //Debug.Log("Float :" + floatNum); //Debug.Log("Str : " + str); //Debug.Log("EnemyType :" + enemyType); ////Debug.Log("Awake"); ////Debug.LogWarning("Awake"); ////Debug.LogError("Awake"); //foreach (var item in nums) //{ // Debug.Log("item :" + item); //} } private void OnEnable() { Debug.Log("this is OnEnable"); } // Use this for initialization void Start() { Debug.Log("this is Start");//輸出 // 掛載的條件 // 1. 繼承 MonoBehaviour // 2. 文件名與類名相同 // 3. 掛載時,所有的代碼無編譯錯誤 // 4. 掛載的腳本不能是抽象類 int num = Random.Range(1,55); Debug.Log(num); int[] nums = new int[] { 1, 2, 3, 5, 7, 8, 9 }; List<int> list = new List<int>() {11,22,33,66,7,88,9 }; foreach (var item in nums) { Debug.Log("item:" + item); } foreach (var item in list) { Debug.Log("item:" + item); } //IEnumerable 枚舉器:其實就是一種機制,具有可以逐個訪問某個集合每個元素的功能 {a,b,c,d} //IEnumerator 迭代器:實現了上面這種機制的具體的 (一個或多個)方法 Unity1803 unity = new Unity1803(); foreach (var item in unity) { Debug.Log("item:" + item); } } // Update is called once per frame void Update() { Debug.Log("this is Update"); } private void FixedUpdate() { Debug.Log("this is FixedUpdate"); } private void LateUpdate() { Debug.Log("this is LateUpdate"); } private void OnDisable() { Debug.Log("this is OnDisable"); } private void OnDestroy() { Debug.Log("this is OnDestroy"); } }
OnPreCull: 在相機剔除場景之前調用此函數。相機可見的對象取決於剔除。OnPreCull 函數調用發生在剔除之前。 OnBecameVisible/OnBecameInvisible: 在對象對於相機可見/不可見時調用此函數。 OnWillRenderObject: 如果對象可見,則為每個相機調用一次此函數。 OnPreRender: 在相機開始渲染場景之前調用此函數。 OnRenderObject: 在完成所有常規場景渲染后調用此函數。此時,可使用 GL 類或 Graphics.DrawMeshNow 繪制自定義幾何圖形。 OnPostRender: 在相機完成場景渲染后調用此函數。 OnRenderImage(僅限專業版): 在完成場景渲染后調用此函數,以便對屏幕圖像進行后處理。 OnGUI: 在每幀上多次調用此函數,以響應 GUI 事件。程序首先將處理 Layout 和 Repaint 事件,然后再處理每個輸入事件的 Layout 和 keyboard/鼠標事件。 OnDrawGizmos: 用於在場景視圖中繪制小圖示 (Gizmos),以實現可視化目的。