(2012-07-07 23:26:11)
1.使用C#的List等容器前要頭文件using System.Collections.Generic;
容器:(聲明--容器實例化--元素實例化--元素入容器)
聲明:public List<Transform> goals;
容器實例化:goals=new List<Transform>();
使用:goals.Add(enemy); //添加到List末尾,enemy作為元素也是要先實例化的
goals[0]; //獲取List索引位置的元素
goals.Count; //變量,得到List元素個數
goals.Clear(); //釋放List的元素,設置Count=0
/*
注意!作為public List<WeaponData> weaponData中的類WeaponData,必須是public的,否則常會有權限錯誤的提示:(貌似是容器實例的權限和容器模板類的權限必須遵循一定高低關系)Inconsistent accessibility: field type `System.Collections.Generic.List<DataBase.WeaponData>' is less accessible than field `DataBase.weaponData'。其中weaponData為DataBase文件DataBase類的變量,Weapon為DataBase文件另一個類.
其余詳情可以查閱msdn對於System.Collections.Generic 命名空間中List的解釋:(和Unity3D中類似)http://msdn.microsoft.com/zh-cn/library/0sbxh9x2
*/
數組:(聲明--數組實例化--數組元素實例化)
聲明: private Skill[] _skill;
數組實例化:_skill=new Skill[Enum.GetValues(typeof(SkillName)).Length];
元素實例化:for (int cnt = 0; cnt < _skill.Length; cnt++)
_skill[cnt] = new Skill();
①另外可以聲明數組后,利用函數返回值進行數組實例化,而且此時
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
②使用方式可以如:
foreach (GameObject enemy in go) { }
③數組屬於Array類,本身也有一些屬性,如_skill.Length;可以獲取數組長度。
④函數傳遞數組時,形參用(Skill[] new_skill)即可(內部用new_skill.Length獲取長度)。改變函數內形參的值,實參數組也會跟着改變。
2.無論是數組還是容器亦或者其它變量,都可以在MonoBehaviour子類中以public權限聲明后,不在腳本內實例化,而是在Unity3D軟件的檢視面板中賦值實現實例化。
對於GameObject,Texture2D等類型的變量(或數組以及容器),一般都是在檢視面板賦值。(但GO類型也可以用尋找GO等函數實例化,Texture2D類型則可以用C#硬盤讀取圖片的方式賦值)