Sqlite

实现一个简单的从背包点击装备,从数据库读取数据进行装备属性加成。(这是本人随意做的小demo,只是希望能给一个例子进行拓展,大神请回避!不喜勿喷。。。)

首先写一个数据库脚本:

using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
using System.Collections.Generic;

public class Database : MonoBehaviour {
    public static Database _Instance;
    //数据库连接对象
    SqliteConnection con;
    #region 回调方法
    void Awake(){
        _Instance = this;
    }
    void Start () {
        string path = "Data source="
            +Application.streamingAssetsPath
            +"/User.sqlite";
        Debug.Log(path);
        con = new SqliteConnection (path);
    }
    void OnDisable(){
        string sql = "update hero set ad=72" +
            ",ap=0,armor=32" +
            ",Spellresistance=33 where name='EZ'";
        ExecuteNonQuery (sql);
    }
    #endregion
    //打开数据库
    void OpenDatabase(){
        if(con == null){
            Debug.Log ("连接失败");
            return;
        }
        con.Open ();
    }
    //关闭数据库
    void CloseDatabase(){
        if(con == null){
            Debug.Log ("连接失败");
            return;
        }
        con.Close ();
    }

    /// <summary>
    /// 非查询操作(增,删,改)
    /// </summary>
    /// <param name="sql">Sql.</param>
    public void ExecuteNonQuery(string sql){
        OpenDatabase ();
        SqliteCommand cmd = con.CreateCommand ();
        cmd.CommandText = sql;
        cmd.ExecuteNonQuery ();
        cmd.Dispose ();
        CloseDatabase ();
    }

    /// <summary>
    /// 查询操作
    /// </summary>
    /// <returns>The query.</returns>
    /// <param name="sql">Sql.</param>
    public List<ArrayList> ExecuteQuery(string sql){
        OpenDatabase ();
        SqliteCommand cmd = con.CreateCommand ();
        cmd.CommandText = sql;
        SqliteDataReader reader = cmd.ExecuteReader ();
        //
        List<ArrayList> list = new List<ArrayList> ();
        while (reader.Read()) {
            ArrayList arr = new ArrayList ();
            for (int i = 0; i < reader.FieldCount; i++) {
                arr.Add (reader.GetValue (i));
            }
            list.Add (arr);
        }
        cmd.Dispose ();
        reader.Close ();
        CloseDatabase ();
        return list;
    }

    //根据表和条件(Name)查询一条数据
    //select * from equip where name = 'WJZR'
    /// <summary>
    /// 
    /// </summary>
    /// <param name="Table">表名</param>
    /// <param name="Name">英雄名</param>
    /// <returns></returns>
    public float[] QueryByName(string Table, string Name){
        string sql = "select * from "+Table +" where name ="+"'"+Name+"'";
        List<ArrayList> list = ExecuteQuery (sql);
        
        ArrayList arr = list [0];
        float[] result = new float[arr.Count - 1];
        //名字不要了 i = 1
        for (int i = 1; i < arr.Count; i++) {
            result [i - 1] = float.Parse (arr[i].ToString());
        }
        return result;
    }
}
 

 

再写一个显示属性脚本:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class DisplayProperty : MonoBehaviour {
    Text[] texts;//显示属性的控件集合
    void Start () {
        texts = new Text[transform.childCount];
        for (int i = 0; i < transform.childCount; i++) {
            texts [i] = transform.GetChild (i).GetComponent<Text>();
        }
        UpdateDisplayHeroProperty ();
    }
    //更新hero属性的显示
    void UpdateDisplayHeroProperty(){
        float[] hero = Database._Instance
            .QueryByName ("hero","EZ");
        string[] prefixText = new string[] {
            "AD: ", "AP: ", "护甲: ", "魔抗: "
        };
        for (int i = 0; i < hero.Length; i++) {
            texts [i].text = prefixText[i] + hero [i];
        }
    }

    //修改Hero属性
    public void UpdateHeroProperty(bool Add,string image){
        float[] hero = Database._Instance
            .QueryByName ("hero","EZ");
        float[] equip = Database._Instance
            .QueryByName ("equip",image);
        //存放计算的结果
        float[] result = new float[hero.Length];
        if (Add) {
            for (int i = 0; i < hero.Length; i++) {
                result [i] = hero [i] + equip [i];
            }
        } else {
            for (int i = 0; i < hero.Length; i++) {
                result [i] = hero [i] - equip [i];
            }
        }
        //更新数据库 hero
        string sql = "update hero set ad ="
                     + result [0] + ",ap=" + result [1] + ",armor=" +
                     result [2] + ",Spellresistance=" + result [3]
                     + " where name = 'EZ'";
      
        Database._Instance.ExecuteNonQuery (sql);
        //将hero的属性显示到UI上
        UpdateDisplayHeroProperty ();
    }
}
 

 

然后是我们的背包脚本:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Bag : MonoBehaviour {
    int currentEquipCount = 0;//当前装备的个数
    Image[] myImages;
    Sprite defaultSprite;//默认的图片
    void Start(){
        myImages = new Image[transform.childCount];
        for (int i = 0; i < transform.childCount; i++) {
            myImages [i] = transform.GetChild
                (i).GetComponent<Image>();
        }
        defaultSprite = myImages [0].sprite;
    }
    //添加装备
    public void AddEquip(Image image){
        //判断装备是否添加满了
        if(currentEquipCount < transform.childCount){
            currentEquipCount++;
            //修改背包
            for (int i = 0; i < myImages.Length; i++) {
                //
                if(myImages[i].sprite == defaultSprite){
                    myImages [i].sprite = image.sprite;
                    break;
                }
            }
            //更新数据
            GameObject.Find("Property")
                .GetComponent<DisplayProperty>()
                .UpdateHeroProperty
                (true,image.sprite.name);
        }
    }
    //丢弃装备
    public void DropEquip(Image image){
        if(image.sprite != defaultSprite){
            
            currentEquipCount--;

            GameObject.Find ("Property").
            GetComponent<DisplayProperty>()
                .UpdateHeroProperty
                (false,image.sprite.name);
            
            image.sprite = defaultSprite;
        }
    }
}
 

最后加一个商城脚本:这里只是简易商城---实现物品存放和点击

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Store : MonoBehaviour {
    //装备点击事件,把每个装备的image组件作为参数,待用。
    public void AddEquip(Image image){
        //调用Bag脚本添加装备的方法
        GameObject.Find ("Bag")
            .GetComponent<Bag>().AddEquip(image);
    }
}

添加装备前:

添加装备后:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值