Unity存储类成员数据(C#篇)

发表于2018-07-03
评论0 2.9k浏览
Unity可以使用的脚本有C#,下面就和大家介绍下Unity中的存储类成员数据。代码很简单,就是利用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脚本可看到读取数据成功的信息。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引