Unity3D 相关项目代码
一、Application.PresistentDataPath
注意最后面是没有/的
public static string PresistentDataPathForEditor = "C:/Users/Administrator/AppData/LocalLow/DefaultCompany/工程名"; public static string PresistentDataPathForAndroid = "/mnt/sdcard/Android/data/包名/files";
2014-9-24日
有时候我们从Assert Stroe 导入一个包以后,下次创建工程可以再下面看到
这些目录是在 : C:\Users\Administrator\AppData\Roaming\Unity\Asset Store 之下
二、序列化与反序列化
[Serializable] public class CategoryBean { public string Name; static string FileName { get { return Application.persistentDataPath + "/category.dat"; } } public static void Load() { Global.Category = new CategoryBean(); if (File.Exists(FileName)) { try { using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read)) { BinaryFormatter b = new BinaryFormatter(); Global.Category = (CategoryBean)b.Deserialize(fs); } } catch (Exception ex) { Debuger.Log("Globals.load occurs an error:" + ex); } } else{ Debuger.Log("Local file is null"); } } public static void Save() { if (Global.Category == null) return; try { using (FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write)) { BinaryFormatter b = new BinaryFormatter(); b.Serialize(fs, Global.Category); } } catch (Exception ex) { Debuger.Log("Globals.Save occurs an error:" + ex); } } }
PS:
1)不能序列化类的静态字段,也不能序列静态类、抽象类和密封类。C#序列化标记只能对实例字段序列号或反序列化。
2)子类可以被序列化或反序列化。但子类的的静态字段仍然遵循1)点。
3)作为其他类中一个字段的类,如果不是静态类,可以被序列化或反序列化,被序列化的类仍然遵循1)。
public class Test : MonoBehaviour { public static List<Ser1> Ser1List = new List<Ser1>(); public static string PersistentFileName { get { return Application.persistentDataPath + "/" + "TEST" + ".dat"; } } void OnGUI() { if (GUI.Button(new Rect(0, 0, 100, 100), "SAVE")) { Ser1.SaveInSerializable(); } if (GUI.Button(new Rect(0, 200, 100, 100), "LOAD")){ Ser1.LoadFromSerializable(); Debug.Log(Ser1.initstring); } if (GUI.Button(new Rect(0, 400, 100, 100), "CHANGE")) { Ser1.initstring = "inittest"; Ser1List.Add(new Ser1() { Name = "test1" }); Ser1List.Add(new Ser1() { Name = "test2" }); Ser1List.Add(new Ser1() { Name = "test3" }); } } } [Serializable] public class Ser1 { public static string initstring = string.Empty; public string Name; public static void LoadFromSerializable() { try { if (File.Exists(Test.PersistentFileName)) { using (FileStream fs = new FileStream(Test.PersistentFileName, FileMode.Open, FileAccess.Read)) { BinaryFormatter b = new BinaryFormatter(); Test.Ser1List = (List<Ser1>)b.Deserialize(fs); } } } catch (Exception ex) { //Debuger.Log("Globals.load occurs an error:" + ex); throw new Exception("Globals.load occurs an error:" + ex); } } public static void SaveInSerializable( ) { try { using (FileStream fs = new FileStream(Test.PersistentFileName, FileMode.Create, FileAccess.Write)) { BinaryFormatter b = new BinaryFormatter(); b.Serialize(fs, Test.Ser1List); } } catch (Exception ex) { Debuger.Log("Globals.Save occurs an error:" + ex); } } }
三、嵌套序列化测试通过
public class Test : MonoBehaviour { void Awake() { MyPerson.Load(); foreach (MyPerson item in Global.MyPersonArray) { Debug.Log(item.name); foreach (Attribute item1 in item.list) { Debug.Log(item1.name); } } } void OnApplicationQuit() { MyPerson.Save(); } } [Serializable] public class MyPerson { public string name; public List<Attribute> list = new List<Attribute>(); public MyPerson() { name = "test"; list.Add(new Attribute()); list.Add(new Attribute()); } public static string file = Application.persistentDataPath + "/test.dat"; public static void Load() { Global.MyPersonArray = new List<MyPerson>(); Global.MyPersonArray.Add(new MyPerson()); Global.MyPersonArray.Add(new MyPerson()); if (File.Exists(file)) { try { using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) { BinaryFormatter b = new BinaryFormatter(); Global.MyPersonArray = (List<MyPerson>)b.Deserialize(fs); } } catch (Exception ex) { Debuger.Log("Globals.load occurs an error:" + ex); } } else { Debuger.Log("Local file is null"); } } public static void Save() { foreach (MyPerson item in Global.MyPersonArray) { item.name = "test5"; foreach (Attribute item1 in item.list) { item1.name = "Attribute5"; } } try { using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write)) { BinaryFormatter b = new BinaryFormatter(); b.Serialize(fs, Global.MyPersonArray); } } catch (Exception ex) { Debuger.Log("Globals.Save occurs an error:" + ex); } } } [Serializable] public class Attribute { public string name; public Attribute() { name = "Attribute"; } }
四、动态生成赋值
public class Test : MonoBehaviour { public Test1 tmp; void Awake() { Test1 create = Instantiate(tmp) as Test1; create.test = new GameObject("haha"); } } public class Test1 : MonoBehaviour { public GameObject test; void Awake() { //wait test != null StartCoroutine(Test()); } IEnumerator Test() { while (test==null) { Debug.Log("now test = = null"); yield return 0; } Debug.Log("now test ! = null"+test.name); } }
五、使用类来作为Dictionary的key
public class Test : MonoBehaviour { void Awake(){ Dictionary<Model_Links, int> usedLinks = new Dictionary<Model_Links, int>(new MyComparer()); usedLinks.Add(new Model_Links() { id = 1, title = "title1", body = "boby1" }, 1); usedLinks.Add(new Model_Links() { id = 2, title = "title2", body = "boby2" }, 2); usedLinks.Add(new Model_Links() { id = 3, title = "title3", body = "boby3" }, 3); usedLinks.Add(new Model_Links() { id = 4, title = "title4", body = "boby4" }, 4); //对比的类 Model_Links test = new Model_Links() { id = 1, title = "title2", body = "boby1" }; //测试是否存在 Debug.Log(usedLinks.ContainsKey(test)); } } public partial class Model_Links { public int id { get; set; } public string title { get; set; } public string body { get; set; } } public class MyComparer : IEqualityComparer<Model_Links> { public bool Equals(Model_Links x, Model_Links y) { return x.id == y.id && x.title == y.title && x.body == y.body; } public int GetHashCode(Model_Links obj) { return obj.id.GetHashCode() ^ obj.title.GetHashCode() ^ obj.body.GetHashCode(); } }
六、文件流操作提醒
if (!File.Exists(Global.VersionLogFileName)) { //Debuger.Log("first time"); FileStream fs = File.Create(Global.VersionLogFileName); //这里要记得关闭这个流,不然的话如果之后由写入操作会引发IO 异常 fs.Close(); return null; }
七、typeof(T) 与 GetType()的区别
简明表达区别:
同点:两者都是返回类型Type
异同:typeof(T),该T,就是一个类型如:class A{},定义了一个A类,如果想获取该A的Type值,就直接typeof(A)即可。
而GetType()是这样的,class A{},定义了一个A类,定对A类实例化成一个a: A a = new A();这时如果想获取该a的Type值,就直接用a的.GetType()即可。如:a.GetType();
所以用GetType之前必须实例化
八、
/// <summary> /// 判断输入的字符串是否只包含数字和英文字母 /// </summary> /// <param name="input"></param> /// <returns></returns> public static bool IsNumAndEnCh(string input) { string pattern = @"^[A-Za-z0-9]+$"; Regex regex = new Regex(pattern); return regex.IsMatch(input); }