Unity3D 初识序列化相反序列化

来源:互联网 发布:java必看书籍知乎 编辑:程序博客网 时间:2024/06/05 02:36

    private void SaveObject<T>(T t)    {        string name                 = string.Format("path{0}.dat", t.ToString());        FileStream fs               = new FileStream(name, FileMode.OpenOrCreate);        BinaryFormatter formatter   = new BinaryFormatter();        try        {            formatter.Serialize(fs, t);        }        catch (Exception ex)        {            Debug.Log(ex.Message);        }        finally        {            fs.Close();        }    }    private T LoadObject<T>()  where  T : new()    {        T t                         = new T();        string name                 = string.Format("path{0}.dat", t.ToString());        FileStream fs               = new FileStream(name, FileMode.Open);        BinaryFormatter formatter   = new BinaryFormatter();        try        {           t =  (T)formatter.Deserialize(fs);        }        catch (Exception ex)        {            Debug.Log(ex.Message);        }        finally        {            fs.Close();                   }        return t;    }




原创粉丝点击