Unity中储存聊天记录——数据本地持久化方案之SQLite数据库

还是项目的聊天系统,最后的一个需求:储存聊天记录。

思路

就是存数据呗,本地持久化存储呗。数据本地持久化方案有很多种,比较常用的是存PlayerPrefs,json或者xml,像存聊天记录这种,我第一判断是存json。因为PlayerPrefs是Unity自带的以Key,Value形式保存的,适合简单的数据存储。Json较为轻量级,读取速度较快,适合做游戏的存档读档功能,也可以做数据配置。excel转json这种, XML较为重量级,通过Excel配置数据,可读性良好,策划配表方便,适合大量的数据配置需求。
当然,最后的选择是项目大佬推荐的一种方案,SQLite数据库,没具体测试细节,不做优劣对比。
参考方案:地址

准备工作

开发环境:Unity2019.4
SQLite插件下载地址:SQLite插件下载地址
数据库查看工具下载地址:数据库查看工具下载地址

在这里插入图片描述

代码部分

代码部分需要两个东西,一个是数据结构的类,一个是增删改查的接口类。

//数据结构类
using System;
using SQLite4Unity3d;
public class MsgTable
{
    
    
	//设置主键 自动增长 (如果需要删除功能的话必须设置主键,主键区别于你需要存的数值,主键设置自动增长的话不需要自己管理赋值,主键不可重复) 
    [PrimaryKey, AutoIncrement]   public int Id  {
    
     get; set; }
    public string Name   {
    
     get; set; }
    public int    Age    {
    
     get; set; }
    public float  Height {
    
     get; set; }
    public float  Weight {
    
     get; set; }
    
    /// <summary>
    /// 重写ToString函数,方便控制台打印
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
    
    
        return string.Format("[Person: Id={0}, Name={1},  Age={2}, Height={3}],Weight={4}]", Id, Name, Age, Height, Weight);
    }
}
//增删改查的接口类。
using System;
using System.Collections;
using System.Collections.Generic;
using SQLite4Unity3d;
using UnityEngine;

public class LocalDataMgr : MonoSingleton<LocalDataMgr>
{
    
    
    /// <summary>
    /// 数据库连接
    /// </summary>
    public SQLiteConnection Connection;

    /// <summary>
    /// 本地数据库名称
    /// </summary>
    private new string name = "LoaclDatabase";

    // Start is called before the first frame update
    void Awake()
    {
    
    
        DontDestroyOnLoad(gameObject);
        
        //这里说明一下,SQLite数据库不仅可以作为持久化数据的一种方案,也可以作为基础数据的一种来使用,储存在不同的位置可适用不同的场景
        
        // Application.persistentDataPath  || Application.streamingAssetsPath
        Connection = new SQLiteConnection(Application.persistentDataPath + "/" + name + ".db",
            SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
        Connection.CreateTable<MsgTable>(); //创建/链接表
 		//这里说明一下,我们定义不同的数据结构的话,可以通过链接不同类型的表来管理不同的表格,当然,增删改查的话也需要传对应的类型,但同一类型的数据结构只能存在一个
		//Connection.CreateTable<TestTable>(); //创建/链接表
    }

	//向MsgTable表里添加一组数据
    public void AddMsgData(string Name, Int32 Age , Int32 Height , Int32 Weight)
    {
    
    
        var localmsg = new MsgTable()
        {
    
    
            Name = Name,
            Age = Age ,
            Height = Height ,
            Weight = Weight
        };
        Connection.Insert(localmsg);
    }

	//在MsgTable表里删除所有Age值的数据,也可以匹配多个字符来查找一个或一些数据
    public void DeleteMsgDataByID(Int64 Age)
    {
    
    
        var data = Connection.Table<MsgTable>().Where(_ => _.Age == Age );
        foreach (var VARIABLE in data)
        {
    
    
            Connection.Delete(VARIABLE); //删除
        }
    }
	//删除所有MsgTable表里的数据
    public void DeleteAllLocalData()
    {
    
    
        Connection.DeleteAll<MsgTable>();
    }

	//获取所有MsgTable表里的数据
    public List<MsgTable> GetLocalMsgData()
    {
    
    
        var datas = Connection.Table<MsgTable>();
        List<MsgTable> msglist = new List<MsgTable>();
        foreach (var VARIABLE in datas)
        {
    
    
            msglist.Add(VARIABLE);
        }
        return msglist;
    }

以上是SQLite数据库的用法。

查看数据

在这里插入图片描述

以上。

猜你喜欢

转载自blog.csdn.net/qq_39860954/article/details/125285068
今日推荐