C#篇-unity存储类成员数据

15 篇文章 0 订阅

代码很简单,就是利用FileStream数据存储到.dat文件

先是类数据结构和单例模式模板类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 为单例模式设计的模板类
/// </summary>
/// <typeparam name="T"></typeparam>
public class UnitySingleton<T> : MonoBehaviour
    where T : Component
{
    private static T _instance;
    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType(typeof(T)) as T;
                if (_instance == null)
                {
                    GameObject obj = new GameObject();
                    obj.hideFlags = HideFlags.HideAndDontSave;//隐藏实例化的new game object,下同  
                    _instance = obj.AddComponent(typeof(T)) as T;
                }
            }
            return _instance;
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class eme : UnitySingleton<eme> {

    public int attack;
    public int defence;
    public int speed;
    public int health;
    public string Fname;

    private void Start()
    {


    }

    private void Update()
    {
        if(Fname!=null)
        {
            DataStorage.Store(this);
        }
    }

}

接下来是数据存储和读取类

using System;
using System.IO;
using UnityEngine;

public class DataStorage
{
    public static void Store(eme e)
    {
        FileStream stream = new FileStream("data/"+eme.Instance.Fname + ".dat", FileMode.Create);//文件存储位置位于“工程文件/data”目录。
        StreamWriter writer = new StreamWriter(stream);
        writer.WriteLine(e.attack);
        writer.WriteLine(e.defence);
        writer.WriteLine(e.speed);
        writer.WriteLine(e.health);

        writer.Close();
    }



    public static eme Load(string name)
    {
        eme e = new eme();
        FileStream stream = new FileStream("data/" + name + ".dat", FileMode.Open);

        if(stream==null)
        {
            Debug.Log("找不到该数据文件");
            return null;
        }
        StreamReader reader = new StreamReader(stream);
        e.Fname =name;
        if (int.TryParse(reader.ReadLine(), out e.attack))
        {

        }
        else Debug.Log("attack获取失败");
        if (int.TryParse(reader.ReadLine(), out e.defence))
        {

        }
        else Debug.Log("defence获取失败");
        if (int.TryParse(reader.ReadLine(), out e.speed))
        {

        }
        else Debug.Log("speed获取失败");
        if(int.TryParse(reader.ReadLine(),out e.health))
        {

        }
        else Debug.Log("health获取失败");

        reader.Close();

        return e;
    }
}

测试文件

using System;
using System.Collections.Generic;
using UnityEngine;
public class Testcs:MonoBehaviour
{
    private void Start()
    {
        string name = eme.Instance.Fname;
        eme e = DataStorage.Load(name);
        Debug.Log("attack:"+e.attack);
        Debug.Log("defence:" + e.defence);
        Debug.Log("speed:" + e.speed);
        Debug.Log("health:" + e.health);
    }
}

给某个物体挂上eme脚本后,在inspect面板修改属性。接着给一个物体加上Testcs脚本可看到读取数据成功的信息
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值