//框架
using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
using System.Collections.Generic;
public class DatabaseFramework : MonoBehaviour {
public static DatabaseFramework instance;
void Awake(){
instance = this;
}
#region 全局数据库对象
private SqliteConnection con;
private SqliteCommand command;
private SqliteDataReader reader;
#endregion
#region 数据库框架-数据库打开关闭
//[]表示c#特性
[SerializeField]//序列化属性(将私有的属性显示到Inspector面板)
//数据库名称
private string databaseName;
/// <summary>
/// 打开数据库
/// </summary>
/// <param name="databaseName">Database name.</param>
public void OpenDatabase(string databaseName){
//如果用户传参数是,没有添加数据库文件后缀名
if (!databaseName.Contains(".sqlite")) {
//添加后缀名
databaseName += ".sqlite";
}
string path = "Data Source=" + Application.streamingAssetsPath + "/" + databaseName;
con = new SqliteConnection (path);//建立连接
command = con.CreateCommand ();//建立指令对象
con.Open ();//打开连接
}
/// <summary>
/// 关闭数据库
/// </summary>
public void CloseDatabase(){
con.Close ();
//reader = null;
//command = null;
//con = null;
}
#endregion
#region 数据库操作
/// <summary>
/// 执行增删改语句的方法
/// </summary>
public void InsertUpdateDelete(string query){
command.CommandText = query;//传入语句
command.ExecuteNonQuery ();//执行语句
}
/// <summary>
/// 查询单个数据
/// </summary>
/// <returns>object</returns>
/// <param name="query">Query.</param>
public object SelectSingData(string query){
command.CommandText = query;//传入语句
object result = command.ExecuteScalar ();//执行语句
return result;//返回结果
}
/// <summary>
/// 查询一些数据
/// </summary>
/// <returns>数据</returns>
public List<ArrayList> SelectAnyDate(string query){
//传入语句
command.CommandText = query;
//执行,接收
reader=command.ExecuteReader ();
//实例化存储结构
List<ArrayList> result = new List<ArrayList> ();
//逐行遍历
while (reader.Read ()) {
//当前行数据
ArrayList currentRowData = new ArrayList ();
//逐列遍历
for (int i = 0; i < reader.FieldCount; i++) {
//当前行当前列的值
object currentValue=reader.GetValue (i);
currentRowData.Add (currentValue);
}
//将当前的所有数据存放到List中
result.Add (currentRowData);
}
//关闭读取流
reader.Close ();
//返回结果
return result;
}
/// <summary>
/// 获取空的装备栏
/// </summary>
/// <returns>The empty equip box.</returns>
public string GetEmptyEquipBox(){
string checkEmpty="Select Equip01,Equip02 From PlayerTable";
//获取装备栏信息
List<ArrayList> msg=SelectAnyDate (checkEmpty);
//判断装备栏1中是否为空
if (msg [0] [0].ToString() == "") {
return "Equip01";
}
//否则判断装备栏2中是否为空
else if (msg [0] [1].ToString() == "") {
return "Equip02";
}
//否则装备栏1和2中都有装备
else {
return "";
}
}
/// <summary>
///添加装备
/// </summary>
/// <param name="equipID">装备ID.</param>
/// <param name="Box">装备栏.</param>
public void SetEquips(string equipID,string equipBox){
string setQuery = "Update PlayerTable Set "+equipBox+"='"+equipID+"'";
InsertUpdateDelete (setQuery);
}
/// <summary>
/// 设置钱数
/// </summary>
/// <param name="money">Money.</param>
public void SetMoney(float money){
string setQuery = "Update PlayerTable Set Gold=" + money.ToString ();
InsertUpdateDelete (setQuery);
}
/// <summary>
/// 获取装备花费
/// </summary>
/// <returns>The equip money.</returns>
/// <param name="equipID">Equip I.</param>
public float GetEquipMoney(string equipID){
//语句
string getQuery = "Select Cost From EquipTable Where Num='" + equipID + "'";
//执行
object result = SelectSingData (getQuery);
//返回
return System.Convert.ToSingle (result);
}
/// <summary>
/// 设置玩家的AD,AP
/// </summary>
/// <param name="ad">Ad.</param>
/// <param name="ap">Ap.</param>
public void SetPlyerADAP(float ad,float ap){
string setQuery = "Update PlayerTable Set AD=" + ad.ToString ()+",AP="+ap.ToString();
InsertUpdateDelete (setQuery);
}
/// <summary>
/// 获取装备AD,AP值
/// </summary>
/// <returns>The equip ADA.</returns>
/// <param name="equipID">装备ID.</param>
/// <param name="AD">AD.</param>
/// <param name="AP">AP.</param>
public void GetEquipADAP(string equipID, out float AD,out float AP){
//查询语句
string query = "Select AD,AP From EquipTable Where Num='" + equipID + "'";
//执行
List<ArrayList> result = SelectAnyDate (query);
AD = System.Convert.ToSingle(result [0] [0]);
AP = System.Convert.ToSingle(result [0] [1]);
}
public void PlayerDataReset(){
string query="Update PlayerTable Set AD=80,AP=20,Gold=8000,Equip01='',Equip02=''";
InsertUpdateDelete (query);
}
#endregion
}
//玩家信息
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class PlayerMsgInit : MonoBehaviour {
public Text playerName;//名称
public Text playerAD;//AD值
public Text playerAP;//AP值
public Text playerGold;//金币数量
public Image playerEquip01;//装备1
public Image playerEquip02;//装备2
public Sprite[] equips;//装备数组
void Start(){
playerName = transform.Find ("PlayerName").GetComponent<Text> ();
playerAD=transform.Find("PlayerAD").GetComponent<Text> ();
playerAP=transform.Find("PlayerAP").GetComponent<Text> ();
playerGold=transform.Find("Money/MoneyCount").GetComponent<Text> ();
playerEquip01 = transform.Find ("PlayerEquips/EquipBg01/MyEquip").GetComponent<Image> ();
playerEquip02 = transform.Find ("PlayerEquips/EquipBg02/MyEquip").GetComponent<Image> ();
//测试
//Debug.Log (playerName + "|" + playerAD + "|" + playerAP + "|" + playerEquip01 + "|" + playerEquip02);
MsgInit ();//执行玩家信息初始化
}
void Update(){
if (Input.GetKeyDown(KeyCode.Space)) {
//重置数据
DatabaseFramework.instance.OpenDatabase ("EquipDatabase");
DatabaseFramework.instance.PlayerDataReset ();
DatabaseFramework.instance.CloseDatabase ();
//重新加载
MsgInit ();
}
}
/// <summary>
/// 玩家信息初始化
/// </summary>
public void MsgInit(){
//打开数据库
DatabaseFramework.instance.OpenDatabase ("EquipDatabase");
//查询玩家信息
string query="Select * From PlayerTable";
List<ArrayList> msg=DatabaseFramework.instance.SelectAnyDate (query);
//关闭连接
DatabaseFramework.instance.CloseDatabase ();
//将数据同步到UI
playerName.text=msg[0][0].ToString();
playerAD.text=msg[0][1].ToString();
playerAP.text=msg[0][2].ToString();
playerGold.text=msg[0][3].ToString();
//如果玩家装备栏01中没有装备
if (msg [0] [4].ToString() == "") {
//隐藏装备图片
playerEquip01.gameObject.SetActive (false);
} else {
//显示图片
playerEquip01.gameObject.SetActive (true);
//装备图片
playerEquip01.sprite = GetSprite (msg [0] [4].ToString());
}
//如果玩家装备栏02中没有装备
if (msg [0] [5].ToString() == "") {
//隐藏装备图片
playerEquip02.gameObject.SetActive (false);
} else {
//显示图片
playerEquip02.gameObject.SetActive (true);
//装备图片
playerEquip02.sprite = GetSprite (msg [0] [5].ToString());
}
}
/// <summary>
/// 从装备数组中获取指定的装备图片
/// </summary>
/// <returns>The sprite.</returns>
/// <param name="id">装备图片ID.</param>
public Sprite GetSprite(string id){
//遍历装备数组
foreach (var item in equips) {
//如果是该装备
if (item.name==id) {
//返回
return item;
}
}
return null;//如果图片库中没有该装备,返回NULL
}
}
//买装备
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class BuyEuips : MonoBehaviour {
private PlayerMsgInit playerMsg;//玩家信息脚本
void Start(){
playerMsg = GetComponent<PlayerMsgInit> ();
}
/// <summary>
/// 购买装备按钮
/// </summary>
/// <param name="EquipID">Equip ID.</param>
public void OnBuyEquipButtonClick(string EquipID){
//数据库操作准备
DatabaseFramework.instance.OpenDatabase("EquipDatabase");//打开数据库
//1.获取该装备的花费
//获取装备ID:(EquipID)
// 通过装备ID在数据库中查询装备花费
//查询语句
string selectCost="Select Cost From EquipTable where Num='"+EquipID+"'";
//执行语句
object cost=DatabaseFramework.instance.SelectSingData(selectCost);
//2.检测当前玩家能否支付该装备的花费
float playerMoney=System.Convert.ToSingle(playerMsg.playerGold.text);
//如果玩家金钱大于等于装备花费
if (playerMoney>=System.Convert.ToSingle(cost)) {
//3.判断玩家装备栏中是否有空余位置
if (!playerMsg.playerEquip01.gameObject.activeSelf||
!playerMsg.playerEquip02.gameObject.activeSelf) {
//4.添加装备到玩家装备栏
//选择放置的装备栏
string equipBox = DatabaseFramework.instance.GetEmptyEquipBox ();
//设置到数据库
DatabaseFramework.instance.SetEquips (EquipID, equipBox);
//5.减去该装备的花费
float currentMoney=playerMoney-System.Convert.ToSingle(cost);
//设置到数据库
DatabaseFramework.instance.SetMoney (currentMoney);
//6.将该装备的属性信息(AD,AP)添加到玩家身上
//获取该装备的AD AP
float equipAD,equipAP;
DatabaseFramework.instance.GetEquipADAP (EquipID, out equipAD, out equipAP);
//获取该玩家的AD AP
float playerAD,playerAP;
playerAD = System.Convert.ToSingle(playerMsg.playerAD.text);
playerAP = System.Convert.ToSingle(playerMsg.playerAP.text);
//计算玩家新的AD AP
playerAD+=equipAD;
playerAP+=equipAP;
//设置到数据库
DatabaseFramework.instance.SetPlyerADAP(playerAD,playerAP);
//7.将数据信息更新到UI
playerMsg.MsgInit();
}
}
//8.关闭数据库
DatabaseFramework.instance.CloseDatabase();
}
}
//卖装备
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SellEquips : MonoBehaviour {
private PlayerMsgInit playMsg;//获取脚本
void Start(){
playMsg = GetComponent<PlayerMsgInit> ();
}
/// <summary>
/// 变卖装备
/// </summary>
/// <param name="equip">装备图片.</param>
public void OnSellButtonClick(Image equip){
//打开数据库
DatabaseFramework.instance.OpenDatabase ("EquipDatabase");
//先判断当前装备栏是1号装备栏还是2号装备栏
//获取装备栏背景对象名称(外面的框子)
string parentName=equip.gameObject.transform.parent.name;
parentName=parentName.Remove (5, 2);//删除‘Bg’字符
//设置当前装备栏为空
DatabaseFramework.instance.SetEquips("",parentName);
//获取当前装备的花费
float equipCost=DatabaseFramework.instance.GetEquipMoney(equip.sprite.name);
//半价出售装备
equipCost /= 2;
//获取当前玩家拥有的金钱
float playerMonry=System.Convert.ToSingle(playMsg.playerGold.text);
//更新玩家金钱
playerMonry += equipCost;
//更新数据库中的玩家金钱数
DatabaseFramework.instance.SetMoney (playerMonry);
//获取当前装备AD,AP
float equipAD,equipAP;
DatabaseFramework.instance.GetEquipADAP (equip.sprite.name, out equipAD, out equipAP);
//获取当前玩家AD,AP
float playerAD, playerAP;
playerAD=System.Convert.ToSingle (playMsg.playerAD.text);
playerAP=System.Convert.ToSingle (playMsg.playerAP.text);
//更新玩家AD,AP
playerAD -= equipAD;
playerAP -= equipAP;
//将玩家AD,AP更新到数据库
DatabaseFramework.instance.SetPlyerADAP(playerAD,playerAP);
//关闭数据库
DatabaseFramework.instance.CloseDatabase ();
//更新UI
playMsg.MsgInit();
}
}
买卖装备+Sqlite
最新推荐文章于 2024-06-05 23:04:35 发布