Unity3D 初识序列化相反序列化

    private void SaveObject(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()  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;
    }




你可能感兴趣的:(Unity3D,初识序列化相反序列化)