一、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(, , , ), "SAVE")) {
Ser1.SaveInSerializable();
}
if (GUI.Button(new Rect(, , , ), "LOAD")){
Ser1.LoadFromSerializable();
Debug.Log(Ser1.initstring);
}
if (GUI.Button(new Rect(, , , ), "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 ;
}
Debug.Log("now test ! = null"+test.name);
}
}

五、使用类来作为Dictionary的key

http://social.msdn.microsoft.com/forums/windowsapps/zh-cn/0b3ab162-7e2a-401e-842d-3e5dc93de228/c-dictionarytt-contains

public class Test : MonoBehaviour {

    void Awake(){
Dictionary<Model_Links, int> usedLinks = new Dictionary<Model_Links, int>(new MyComparer());
usedLinks.Add(new Model_Links() { id = , title = "title1", body = "boby1" }, );
usedLinks.Add(new Model_Links() { id = , title = "title2", body = "boby2" }, );
usedLinks.Add(new Model_Links() { id = , title = "title3", body = "boby3" }, );
usedLinks.Add(new Model_Links() { id = , title = "title4", body = "boby4" }, ); //对比的类
Model_Links test = new Model_Links() { id = , 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);
}

  

Unity3D 相关项目代码的更多相关文章

  1. .NET 项目代码风格要求

    原文:http://kb.cnblogs.com/page/179593/ 项目代码风格要求 PDF版下载:项目代码风格要求V1.0.pdf 代码风格没有正确与否,重要的是整齐划一,这是我拟的一份&l ...

  2. [Asp.net 5] DependencyInjection项目代码分析4-微软的实现(4)

    这个系列已经写了6篇,链接地址如下: [Asp.net 5] DependencyInjection项目代码分析 [Asp.net 5] DependencyInjection项目代码分析2-Auto ...

  3. 循序渐进开发WinForm项目(2)--项目代码的分析

    随笔背景:在很多时候,很多入门不久的朋友都会问我:我是从其他语言转到C#开发的,有没有一些基础性的资料给我们学习学习呢,你的框架感觉一下太大了,希望有个循序渐进的教程或者视频来学习就好了. 其实也许我 ...

  4. .Net 项目代码风格要求小结

    代码风格没有正确与否,重要的是整齐划一,这是我拟的一份<.Net 项目代码风格要求>,供大家参考. 1. C# 代码风格要求1.1注释 类型.属性.事件.方法.方法参数,根据需要添加注释. ...

  5. .NET之美——.Net 项目代码风格要求

    .Net 项目代码风格要求 PDF版下载:项目代码风格要求V1.0.pdf 代码风格没有正确与否,重要的是整齐划一,这是我拟的一份<.Net 项目代码风格要求>,供大家参考. 1. C# ...

  6. Unity3d 合作开发项目

    Unity3d  合作开发项目    交流群:63438968  本人:灰太龙 项目的合作开发是至关重要的,第一个问题就是自适应分辨率的问题! 综合考虑了一下,我们采用了IGUI插件,这个插件有以下几 ...

  7. C#项目代码风格要求(转)

    代码风格没有正确与否,重要的是整齐划一,这是我拟的一份<项目代码风格要求>,供大家参考. PDF版下载:项目代码风格要求V1.0.pdf 1. C# 代码风格要求 1.1注释 类型.属性. ...

  8. Net 项目代码风格

    .Net 项目代码风格要求   .Net 项目代码风格要求 PDF版下载:项目代码风格要求V1.0.pdf 代码风格没有正确与否,重要的是整齐划一,这是我拟的一份<.Net 项目代码风格要求&g ...

  9. Webpack 2 视频教程 017 - Webpack 2 中分离打包项目代码与组件代码

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

随机推荐

  1. jQuery增加删除修改tab导航特效

    HTML:         <div class="container iden_top">                <ul>             ...

  2. sql中更新数据库用到declare @a in

    declare @a in update TB_Class set @a=1,name='李小龙' where ID=1 这样就可以像更新哪个就更新哪个了 例如ibatisnet中需要更新的时候: & ...

  3. HTML5-draggable(拖放)

                                                   <!DOCTYPE html> <html class="no-js" ...

  4. SignalR 2.0 系列: 开始使用SignalR 2.0

    这是微软官方SignalR 2.0教程Getting Started with ASP.NET SignalR 2.0系列的翻译,这里是第四篇:开始使用SignalR 2.0 原文:Getting S ...

  5. VMware Workstation中linux(centos)与windows7共享文件夹

    引用网站有: http://www.jb51.net/LINUXjishu/161994.html http://www.cnblogs.com/xiehy/archive/2011/12/19/22 ...

  6. PHP 登录完成后如何跳转上一访问页面

    访问网站页面时,有的页面需要授权才能访问,这时候就会要求用户登录,跳转到登录页面login.php,怎么实现登录后返回到刚才访问的页面 项目需求 访问网站页面时,有的页面需要授权才能访问,这时候就会要 ...

  7. 【linux】iptables 开启80端口

    经常使用CentOS的朋友,可能会遇到和我一样的问题.开启了防火墙导致80端口无法访问,刚开始学习centos的朋友可以参考下. 经常使用CentOS的朋友,可能会遇到和我一样的问题.最近在Linux ...

  8. MongoDB Long/Int(长整型)的自增长主键 解决方案

    今朝有幸尝芒果,发现自增长ID类型有多种,唯独没有Long/Int. 一思路:1. 自建一个Collection(表,假设名为:IdentityEntity,其中字段:_id, Key, Value, ...

  9. SQL Server 2012 BI 学习 第一天

    了解数据源,数据源视图,多维数据集,维度 数据源:一个数据库或者其它数据链接,SSAS不支持使用模拟功能来处理 OLAP 对象.模拟信息选择“使用服务帐户” 数据源视图:DSV是元数据的单个统一视图, ...

  10. hexo-github 博客搭建

    安装nodejs 从官网下载系统对应的源码 wget -qO- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | ...