
【千锋合集】史上最全Unity3D全套教程|匠心之作_哔哩哔哩_bilibili
UI搭建
数据库配置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mono.Data.Sqlite;public class SQLFrame{
#region SingleTon
private static SQLFrame instance;
public static SQLFrame GetInstance() {
if (instance == null) {
instance = new SQLFrame();
}
return instance;
}
private SQLFrame() {} //防止外界去new一个SQLFrame
#endregion
//连接字符串
private string conStr;
private SqliteConnection con;
private SqliteCommand command;
private SqliteDataReader reader;
///
/// 打开数据库
///
///
public void OpenDatabase(string databaseName) {
//如果名称中不包含后缀,自动添加
if (!databaseName.EndsWith(".sqlite")) {
databaseName += ".sqlite";
}
#if UNITY_EDITOR
conStr = "Data Source =" + Application.streamingAssetsPath +"/" +databaseName;
#endif
con = new SqliteConnection(conStr);
//打开连接
con.Open();
//创建指令对象
command = con.CreateCommand();
}
///
/// 关闭数据库
///
public void CloseDatabase() {
if (reader != null)
{
reader.Close();
reader = null;
}
if (command != null)
{
command.Dispose();
command = null;
}
if (con != null)
{
con.Close();
con = null;
}
}
///
/// 执行非查询类SQL语句
///
///
public int DontSelect(string query) {
//赋值SQL语句
command.CommandText = query;
//执行SQL语句
return command.ExecuteNonQuery();
}
public int Insert(string query) {
return DontSelect(query);
}
public int Update(string query)
{
return DontSelect(query);
}
public int Delete(string query)
{
return DontSelect(query);
}
///
/// 查询单个数据
///
///
///
public object SelectSingleData(string query) {
//赋值SQL语句
command.CommandText = query;
//执行SQL语句,返回结果
return command.ExecuteScalar();
}
///
/// 查询多个数据
///
///
public List SelectMultipleData(string query) {
//赋值SQL语句
command.CommandText = query;
//执行
reader = command.ExecuteReader();
//一行多列:ArrayList
//多行多列:List
//实例化结果对象
List result = new List();
while (reader.Read()) {
//声明一个ArrayList存储改行的所有数据
ArrayList currentRow = new ArrayList();
//遍历改行所有的列
for (int i = 0; i < reader.FieldCount; i++) {
//将当前列的数据添加到集合
currentRow.Add(reader.GetValue(i));
}
//将当前行的数据放到List里面
result.Add(currentRow);
}
//关闭读取器
reader.Close();
//返回结果
return result;
}
}
数据库代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;public class EquipShopFrame : SQLFrame {
#region SingleTon
private static EquipShopFrame instance;
public static new EquipShopFrame GetInstance() //盖掉父类方法
{
if (instance == null)
{
instance = new EquipShopFrame();
}
return instance;
}
private EquipShopFrame() { } //防止外界去new一个SQLFrame
#endregion
//SQl语句
//private string sqlQuery;
private string query;
///
/// 买装备流程
///
///
public void BuyEquip(string equipName) {
#region 买装备流程
//1、查看要买的装备价值多少钱
//2、查看当前召唤师有多少钱
//3、判断钱够不够买装备
//4、如果不够,拒绝购买
//5、如果够,购买装备
//5.1、获取装备属性加成
//5.2、获取英雄当前属性值
//5.3、属性加成与当前英雄的属性相加
//5.4、更新到英雄表格中
//5.5、获取英雄的装备存储字符串
//5.6、拼接字符串,形成新的英雄装备存储记录
//5.7、将新的英雄装备信息存储到数据库
//5.8、将装备的花费扣除,更新英雄的金钱
#endregion
}
public void SellEquip(string equipName) {}
///
/// 设置英雄属性信息
///
///
///
public void SetHeroEquips(string heroName, string heroEquips) {
//编写SQL语句
query = "Update HeroTable Set HeroEquips='" + heroEquips + "'Where HeroName='"+heroName+"'";
//执行更新操作
Update(query);
}
///
/// 获取英雄装备信息
///
///
public string GetHeroEquips(string heroName) {
//编写SQL语句
query = "Select HeroEquips From HeroTable Where HeroName='"+heroName+"'";
//执行
object result=SelectSingleData(query);
if (result==null) {
return null;
}
//返回结果
return result.ToString();
}///
/// 设置英雄的属性信息
///
///
private void SetHeroProperties(string heroName,int[] properties) {
//编写SQL语句
query = "Update HeroTable Set HeroAD="+properties[0]
+",HeroAP="+properties[1]+",HeroAR="+properties[2]+",HeroSR="+properties[3]+" Where HeroName'" + heroName + "'";
//执行SQL语句
Update(query);
}
///
/// 获取英雄此时的属性值
///
///
///
public int[] GetHeroProperties(string heroName) {
//编写SQL语句
query= "Select * From HeroTable Where HeroName='" + heroName + "'";
//执行SQL语句
List result=SelectMultipleData(query);
//实例化属性数组
int[] properties = new int[4];
//循环获取值
for (int i = 0; i < properties.Length; i++) {
//转换数据并存储
properties[i] = Convert.ToInt32(result[0][i + 2]);
}
//返回结果
return properties;
}
///
/// 获取当前装备的属性加成
///
///
///
public int[] GetEquipProperties(string equipName) {
//编写SQL语句
query = "Select * From ShopTable Where EquipName='" + equipName + "'";
//执行SQL语句
List result = SelectMultipleData(query);
//实例化属性数组
int[] properties = new int[4];
//遍历赋值
for (int i = 0; i < properties.Length; i++) {
//转换数据并存储
properties[i] = System.Convert.ToInt32(result[0][i + 2]);
}
//返回结果
return properties;
}
///
/// 获取英雄剩余金钱
///
///
///
public int GetHeroMoney(string heroName) {
//编写SQL语句
query = "Select HeroMoney From HeroTable Where HeroName='" + heroName + "'";
//执行SQL语句
object money = SelectSingleData(query);
//返回整型结果
return System.Convert.ToInt32(money);
}
///
/// 设置英雄金钱
///
public void SetHeroMoney(string heroName,int heroMoney) {
//编写SQL语句
query = "Update HeroTable Set HeroMoney="+ heroMoney.ToString()+ "Where HeroName='" + heroName + "'";
//执行SQL语句
Update(query);
}
///
/// 获取某个装备值多少钱
///
///
///
public int GetEquipMoney(string equipName) {
//编写SQL语句
query = "Select EquipMoney From ShopTable Where EquipName='"+equipName+"'";
//执行SQL语句
object money=SelectSingleData(query);
//返回整型结果
return System.Convert.ToInt32(money);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;public class EquipShopFrame : SQLFrame {
#region SingleTon
private static EquipShopFrame instance;
public static new EquipShopFrame GetInstance() //盖掉父类方法
{
if (instance == null)
{
instance = new EquipShopFrame();
}
return instance;
}
private EquipShopFrame() { } //防止外界去new一个SQLFrame
#endregion
//SQl语句
//private string sqlQuery;
private string query;
///
/// 买装备流程
///
///
public void BuyEquip(string equipName,string heroName) {
#region 买装备流程
//1、查看要买的装备价值多少钱
int equipMoney = GetEquipMoney(equipName);
//2、查看当前召唤师有多少钱
int heroMoney = GetHeroMoney(heroName);
//3、判断钱够不够买装备
if (equipMoney > heroMoney) {
//4、如果不够,拒绝购买
Debug.Log("余额不足,无法购买!");
return;
}//5、如果够,购买装备
//5.1、获取装备属性加成
int[] equipProperties = GetEquipProperties(equipName);
//5.2、获取英雄当前属性值
int[] heroProperties = GetHeroProperties(heroName);
//5.3、属性加成与当前英雄的属性相加
int[] newHeroProperties=PropertiesOpration(heroProperties,equipProperties,1);
//5.4、更新到英雄表格中
SetHeroProperties(heroName,newHeroProperties);
//5.5、获取英雄的装备存储字符串
string equips=GetHeroEquips(heroName);
//5.6、拼接字符串,形成新的英雄装备存储记录
equips += equipName + "|";
//5.7、将新的英雄装备信息存储到数据库
SetHeroEquips(heroName, equips);
//5.8、将装备的花费扣除,更新英雄的金钱
SetHeroMoney(heroName,heroMoney - equipMoney);
#endregion
}
public void SellEquip(string equipName, string heroName) {
//1、查看要买的装备价值多少钱
int equipMoney = GetEquipMoney(equipName);
//2、查看当前召唤师有多少钱
int heroMoney = GetHeroMoney(heroName);
//3、扣钱
heroMoney += equipMoney / 2;
//4、英雄的money更新到数据库
SetHeroMoney(heroName, heroMoney);
//5、获取装备属性信息
int[] equipProperties = GetEquipProperties(equipName);
//6、获取英雄属性信息
int[] heroProperties = GetHeroProperties(heroName);
//7、英雄属性中移除该装备的属性加成
int[] newHeroProperties=PropertiesOpration(heroProperties, equipProperties, 2);
//8、更新英雄属性到数据库
SetHeroProperties(heroName, newHeroProperties);
//9、获取英雄装备字符串
string equips = GetHeroEquips(heroName);
//10、获取装备在字符串中的位置
int equipIndex= equips.LastIndexOf(equipName);
//11、移除装备字符串
equips=equips.Remove(equipIndex, equipName.Length+1);
//12、更新数据库
SetHeroEquips(heroName, equips);}
///
/// 设置英雄属性信息
///
///
///
public void SetHeroEquips(string heroName, string heroEquips) {
//编写SQL语句
query = "Update HeroTable Set HeroEquips='" + heroEquips + "'Where HeroName='"+heroName+"'";
//执行更新操作
Update(query);
}
///
/// Propertieses the opration
///
///
///
/// 1 for Add, 2 for Remove.
///
public int[] PropertiesOpration(int[] heroPpt, int[] equipPpt,int oprationID) {
//实例化结果数组
int[] result = new int[heroPpt.Length];
if (oprationID == 1) {
for (int i = 0; i < result.Length; i++) {
//属性相加
result[i] = heroPpt[i] + equipPpt[i];
}
}
else if(oprationID==2) {
for (int i = 0; i < result.Length; i++)
{
//移除装备属性加成
result[i] = heroPpt[i] - equipPpt[i];
}
}
return result;
}
///
/// 获取英雄装备信息
///
///
public string GetHeroEquips(string heroName) {
//编写SQL语句
query = "Select HeroEquips From HeroTable Where HeroName='"+heroName+"'";
//执行
object result=SelectSingleData(query);
if (result==null) {
return null;
}
//返回结果
return result.ToString();
}///
/// 设置英雄的属性信息
///
///
private void SetHeroProperties(string heroName,int[] properties) {
//编写SQL语句
query = "Update HeroTable Set HeroAD="+properties[0]
+",HeroAP="+properties[1]+",HeroAR="+properties[2]+",HeroSR="+properties[3]+" Where HeroName='" + heroName + "'";
//执行SQL语句
Update(query);
}
///
/// 获取英雄此时的属性值
///
///
///
public int[] GetHeroProperties(string heroName) {
//编写SQL语句
query= "Select * From HeroTable Where HeroName='" + heroName + "'";
//执行SQL语句
List result=SelectMultipleData(query);
//实例化属性数组
int[] properties = new int[4];
//循环获取值
for (int i = 0; i < properties.Length; i++) {
//转换数据并存储
properties[i] = Convert.ToInt32(result[0][i + 2]);
}
//返回结果
return properties;
}
///
/// 获取当前装备的属性加成
///
///
///
public int[] GetEquipProperties(string equipName) {
//编写SQL语句
query = "Select * From ShopTable Where EquipName='" + equipName + "'";
//执行SQL语句
List result = SelectMultipleData(query);
//实例化属性数组
int[] properties = new int[4];
//遍历赋值
for (int i = 0; i < properties.Length; i++) {
//转换数据并存储
properties[i] = System.Convert.ToInt32(result[0][i + 2]);
}
//返回结果
return properties;
}
///
/// 获取英雄剩余金钱
///
///
///
public int GetHeroMoney(string heroName) {
//编写SQL语句
query = "Select HeroMoney From HeroTable Where HeroName='" + heroName + "'";
//执行SQL语句
object money = SelectSingleData(query);
//返回整型结果
return System.Convert.ToInt32(money);
}
///
/// 设置英雄金钱
///
public void SetHeroMoney(string heroName,int heroMoney) {
//编写SQL语句
query = "Update HeroTable Set HeroMoney="+ heroMoney.ToString()+ " Where HeroName='" + heroName + "'";
//执行SQL语句
Update(query);
}
///
/// 获取某个装备值多少钱
///
///
///
public int GetEquipMoney(string equipName) {
//编写SQL语句
query = "Select EquipMoney From ShopTable Where EquipName='"+equipName+"'";
//执行SQL语句
object money=SelectSingleData(query);
//返回整型结果
return System.Convert.ToInt32(money);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class ShopView : MonoBehaviour {
#region UI Component
//商店装备的预设体
private GameObject shopEquipPrefab;
//商店窗口
private Transform shopWindow;#endregion
//装备商店单例
EquipShopFrame eFrame;
private void Awake()
{
eFrame = EquipShopFrame.GetInstance();
UIComponentInit();
}
private void Start()
{
//打开数据库
eFrame.OpenDatabase("ShopDatabase");
//初始化商店
ShopInit();
}
///
/// UI组件初始化
///
private void UIComponentInit() {
shopWindow = transform.Find("ShopWindow");
shopEquipPrefab = Resources.Load("Prefabs/ShopEquip");
}
///
/// 商品初始化
///
private void ShopInit() {
//获取商店所有装备的名称
string[] equips=eFrame.GetShopEquips();
//遍历生成所有商店装备
for (int i=0;i
//生成装备对象
GameObject crtEquip=Instantiate(shopEquipPrefab);
//找到存放装备图片的格子
Transform box=shopWindow.GetChild(i);
//设置为格子的子对象
crtEquip.transform.SetParent(box);
//设置本地坐标
crtEquip.transform.localPosition = Vector3.zero;
//设置缩放
crtEquip.transform.localScale = Vector3.one;
//获取装备图片【Sprite】
Sprite equipSpr=Resources.Load("Textures/"+equips[i]);
//更改装备图片
crtEquip.GetComponent().sprite = equipSpr;
}}
private void OnApplicationQuit()
{
//关闭数据库
eFrame.CloseDatabase();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShopEquip : MonoBehaviour {
//按钮组件
private Button shopEquipBtn;
///
/// 商店装备初始化
///
public void ShopEquipInit() {
//获取按钮组件
shopEquipBtn = GetComponent
public void OnShopEquipButtonClick() {
//购买该装备
Debug.Log("ok");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShopView : MonoBehaviour {
#region UI Component
//商店装备的预设体
private GameObject shopEquipPrefab;
//商店窗口
private Transform shopWindow;
#endregion
//装备商店单例
EquipShopFrame eFrame;
private void Awake()
{
eFrame = EquipShopFrame.GetInstance();
UIComponentInit();
}
private void Start()
{
//打开数据库
eFrame.OpenDatabase("ShopDatabase");
//初始化商店
ShopInit();
}
///
/// UI组件初始化
///
private void UIComponentInit() {
shopWindow = transform.Find("ShopWindow");
shopEquipPrefab = Resources.Load
}
///
/// 商品初始化
///
private void ShopInit() {
//获取商店所有装备的名称
string[] equips=eFrame.GetShopEquips();
//遍历生成所有商店装备
for (int i=0;i
//生成装备对象
GameObject crtEquip=Instantiate(shopEquipPrefab);
//找到存放装备图片的格子
Transform box=shopWindow.GetChild(i);
//设置为格子的子对象
crtEquip.transform.SetParent(box);
//设置本地坐标
crtEquip.transform.localPosition = Vector3.zero;
//设置缩放
crtEquip.transform.localScale = Vector3.one;
//获取装备图片【Sprite】
Sprite equipSpr=Resources.Load
//更改装备图片
crtEquip.GetComponent
//给装备按钮添加点击事件
crtEquip.GetComponent
}
}
private void OnApplicationQuit()
{
//关闭数据库
eFrame.CloseDatabase();
}
}
代码汇总:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;public class BagEquip : MonoBehaviour {
private Button bagEquipBtn;
private string heroName;
private string equipName;
private Action updateHeroMsg;///
/// 背包装备初始化
///
public void BagEquipInit(string heroName, Action updateHeroMsg) {
this.heroName = heroName;
this.updateHeroMsg = updateHeroMsg;bagEquipBtn = GetComponent
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;public class ShopEquip : MonoBehaviour {
//按钮组件
private Button shopEquipBtn;
private string equipName;
private string heroName;
//更新英雄信息事件
private Action updateHeroMsg;///
/// 商店装备初始化
///
public void ShopEquipInit(string heroName, Action updateHeroMsg) {
//设置英雄名称
this.heroName = heroName;
//设置更新事件
this.updateHeroMsg = updateHeroMsg;
//获取按钮组件
shopEquipBtn = GetComponent
public void OnShopEquipButtonClick() {
//购买该装备
//进行数据库操作
EquipShopFrame.GetInstance().BuyEquip(equipName, heroName);
//将数据库的最新数据更新到UI
if (updateHeroMsg != null) {
updateHeroMsg(); }
}}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class ShopView : MonoBehaviour {
#region UI Component
//商店装备的预设体
private GameObject shopEquipPrefab;
//背包装备的预设体
private GameObject bagEquipPrefab;
//商店窗口
private Transform shopWindow;
//背包窗口
private Transform bagWindow;
//玩家名称文本
private Text heroNameText;
//四个属性文本
private Text[] propertiesText;
//英雄金钱文本
private Text heroMoneyText;
#endregion//装备商店单例
EquipShopFrame eFrame;
//英雄名称
private string heroName;
private void Awake()
{
eFrame = EquipShopFrame.GetInstance();
UIComponentInit();
}
private void Start()
{
//打开数据库
eFrame.OpenDatabase("ShopDatabase");
//设置英雄名称文本
SetHeroNameText();
//初始化商店
ShopInit();
//初始化英雄信息
UpdateHeroMsg();
}
///
/// UI组件初始化
///
private void UIComponentInit() {
shopWindow = transform.Find("ShopWindow");
bagWindow = transform.Find("BagWindow");
heroNameText = transform.GetChild(3).GetComponent();
//找到四个属性文本框的父对象
Transform propertiesParent = transform.Find("HeroPropertiesText");
//实例化属性文本数组
propertiesText = new Text[4];
//遍历获取属性的文本组件
for (int i = 0; i < propertiesText.Length; i++) {
propertiesText[i] = propertiesParent.GetChild(i).GetComponent();
}
//获取英雄金钱文本
heroMoneyText = transform.Find("Moneyicon/Text").GetComponent();
//从Resources中加载两个预设体
shopEquipPrefab = Resources.Load("Prefabs/ShopEquip");
bagEquipPrefab=Resources.Load("Prefabs/BagEquip");
}
///
/// 商品初始化
///
private void ShopInit() {
//获取商店所有装备的名称
string[] equips=eFrame.GetShopEquips();
//遍历生成所有商店装备
for (int i=0;i
//生成装备对象
GameObject crtEquip=Instantiate(shopEquipPrefab);
//找到存放装备图片的格子
Transform box=shopWindow.GetChild(i);
//设置为格子的子对象
crtEquip.transform.SetParent(box);
//设置本地坐标
crtEquip.transform.localPosition = Vector3.zero;
//设置缩放
crtEquip.transform.localScale = Vector3.one;
//获取装备图片【Sprite】
Sprite equipSpr=Resources.Load("Textures/"+equips[i]);
//更改装备图片
crtEquip.GetComponent().sprite = equipSpr;
//给装备按钮添加点击事件
crtEquip.GetComponent().ShopEquipInit(heroName,UpdateHeroMsg);
}}
///
/// 设置英雄名称文本
///
private void SetHeroNameText() {
//获取英雄名称
heroName = eFrame.GetHeroName();
//设置英雄名称
heroNameText.text = heroName;
}
///
/// 设置英雄属性信息
///
private void SetHeroProperties() {
//获取英雄属性信息
int[] properties = eFrame.GetHeroProperties(heroName);
//设置到UI
for (int i = 0; i < propertiesText.Length; i++) {
propertiesText[i].text = properties[i].ToString();
}
}
private void SetHeroMoney() {
//获取英雄的金钱数字
int money = eFrame.GetHeroMoney(heroName);
//设置到UI
heroMoneyText.text = money.ToString();
}
//清空英雄背包装备
private void ClearHeroBagEquips() {
for (int i = 0; i < bagWindow.childCount; i++) {
Transform bagBox=bagWindow.GetChild(i);
if (bagBox.childCount != 0&&bagBox.name!="Header") {
//将背包格子中的装备销毁掉
Destroy(bagBox.GetChild(0).gameObject);
}
}
}//设置英雄背包装备
private void SetHeroBagEquips() {
//获取背包中所有装备名称
string[] equips=eFrame.GetHeroEquipsArray(heroName);
//遍历所有的装备名称
for (int i = 0; i < equips.Length; i++) {
if (equips[i] == "")
//跳过他
continue;
//生成背包装备
GameObject bagEquip=Instantiate(bagEquipPrefab);
//获取父对象
Transform bagBox = bagWindow.GetChild(i);
//设置父对象
bagEquip.transform.SetParent(bagBox);
//设置本地坐标
bagEquip.transform.localPosition = Vector3.zero;
//设置缩放
bagEquip.transform.localScale = Vector3.one;
//获取装备图片
Sprite equipSpr=Resources.Load("Textures/"+equips[i]);
//设置图片
bagEquip.GetComponent().sprite=equipSpr;
//初始化背包装备
bagEquip.GetComponent().BagEquipInit(heroName,UpdateHeroMsg);
}
}
///
/// 更新玩家信息
///
private void UpdateHeroMsg()
{
//设置英雄属性文本
SetHeroProperties();
//设置英雄金钱文本
SetHeroMoney();
//清理英雄的背包
ClearHeroBagEquips();
//设置英雄背包装备
SetHeroBagEquips();
}private void OnApplicationQuit()
{
//关闭数据库
eFrame.CloseDatabase();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mono.Data.Sqlite;public class SQLFrame{
#region SingleTon
private static SQLFrame instance;
public static SQLFrame GetInstance() {
if (instance == null) {
instance = new SQLFrame();
}
return instance;
}
protected SQLFrame() {} //防止外界去new一个SQLFrame
#endregion
//连接字符串
private string conStr;
private SqliteConnection con;
private SqliteCommand command;
private SqliteDataReader reader;
///
/// 打开数据库
///
///
public void OpenDatabase(string databaseName) {
//如果名称中不包含后缀,自动添加
if (!databaseName.EndsWith(".sqlite")) {
databaseName += ".sqlite";
}
#if UNITY_EDITOR
conStr = "Data Source =" + Application.streamingAssetsPath +"/" + databaseName;
#endif
con = new SqliteConnection(conStr);
//打开连接
con.Open();
//创建指令对象
command = con.CreateCommand();
}
///
/// 关闭数据库
///
public void CloseDatabase() {
if (reader != null)
{
reader.Close();
reader = null;
}
if (command != null)
{
command.Dispose();
command = null;
}
if (con != null)
{
con.Close();
con = null;
}
}
///
/// 执行非查询类SQL语句
///
///
public int DontSelect(string query) {
//赋值SQL语句
command.CommandText = query;
//执行SQL语句
return command.ExecuteNonQuery();
}
public int Insert(string query) {
return DontSelect(query);
}
public int Update(string query)
{
return DontSelect(query);
}
public int Delete(string query)
{
return DontSelect(query);
}
///
/// 查询单个数据
///
///
///
public object SelectSingleData(string query) {
//赋值SQL语句
command.CommandText = query;
//执行SQL语句,返回结果
return command.ExecuteScalar();
}
///
/// 查询多个数据
///
///
public List SelectMultipleData(string query) {
//赋值SQL语句
command.CommandText = query;
//执行
reader = command.ExecuteReader();
//一行多列:ArrayList
//多行多列:List
//实例化结果对象
List result = new List();
while (reader.Read()) {
//声明一个ArrayList存储改行的所有数据
ArrayList currentRow = new ArrayList();
//遍历改行所有的列
for (int i = 0; i < reader.FieldCount; i++) {
//将当前列的数据添加到集合
currentRow.Add(reader.GetValue(i));
}
//将当前行的数据放到List里面
result.Add(currentRow);
}
//关闭读取器
reader.Close();
//返回结果
return result;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;public class EquipShopFrame : SQLFrame {
#region SingleTon
private static EquipShopFrame instance;
public static new EquipShopFrame GetInstance() //盖掉父类方法
{
if (instance == null)
{
instance = new EquipShopFrame();
}
return instance;
}
private EquipShopFrame() { } //防止外界去new一个SQLFrame
#endregion
//SQl语句
//private string sqlQuery;
private string query;
///
/// 买装备流程
///
///
public void BuyEquip(string equipName,string heroName) {
#region 买装备流程
//1、查看要买的装备价值多少钱
int equipMoney = GetEquipMoney(equipName);
//2、查看当前召唤师有多少钱
int heroMoney = GetHeroMoney(heroName);
//3、判断钱够不够买装备
if (equipMoney > heroMoney) {
//4、如果不够,拒绝购买
Debug.Log("余额不足,无法购买!");
return;
}//5、如果够,购买装备
//5.1、获取装备属性加成
int[] equipProperties = GetEquipProperties(equipName);
//5.2、获取英雄当前属性值
int[] heroProperties = GetHeroProperties(heroName);
//5.3、属性加成与当前英雄的属性相加
int[] newHeroProperties=PropertiesOpration(heroProperties,equipProperties,1);
//5.4、更新到英雄表格中
SetHeroProperties(heroName,newHeroProperties);
//5.5、获取英雄的装备存储字符串
string equips=GetHeroEquips(heroName);
//5.6、拼接字符串,形成新的英雄装备存储记录
equips += equipName + "|";
//5.7、将新的英雄装备信息存储到数据库
SetHeroEquips(heroName, equips);
//5.8、将装备的花费扣除,更新英雄的金钱
SetHeroMoney(heroName,heroMoney - equipMoney);
#endregion
}
public void SellEquip(string equipName, string heroName) {
//1、查看要买的装备价值多少钱
int equipMoney = GetEquipMoney(equipName);
//2、查看当前召唤师有多少钱
int heroMoney = GetHeroMoney(heroName);
//3、扣钱
heroMoney += equipMoney / 2;
//4、英雄的money更新到数据库
SetHeroMoney(heroName, heroMoney);
//5、获取装备属性信息
int[] equipProperties = GetEquipProperties(equipName);
//6、获取英雄属性信息
int[] heroProperties = GetHeroProperties(heroName);
//7、英雄属性中移除该装备的属性加成
int[] newHeroProperties=PropertiesOpration(heroProperties, equipProperties, 2);
//8、更新英雄属性到数据库
SetHeroProperties(heroName, newHeroProperties);
//9、获取英雄装备字符串
string equips = GetHeroEquips(heroName);
//10、获取装备在字符串中的位置
int equipIndex= equips.LastIndexOf(equipName);
//11、移除装备字符串
equips=equips.Remove(equipIndex, equipName.Length+1);
//12、更新数据库
SetHeroEquips(heroName, equips);}
///
/// 设置英雄属性信息
///
///
///
public void SetHeroEquips(string heroName, string heroEquips) {
//编写SQL语句
query = "Update HeroTable Set HeroEquips='" + heroEquips + "'Where HeroName='"+heroName+"'";
//执行更新操作
Update(query);
}
///
/// Propertieses the opration
///
///
///
/// 1 for Add, 2 for Remove.
///
public int[] PropertiesOpration(int[] heroPpt, int[] equipPpt,int oprationID) {
//实例化结果数组
int[] result = new int[heroPpt.Length];
if (oprationID == 1) {
for (int i = 0; i < result.Length; i++) {
//属性相加
result[i] = heroPpt[i] + equipPpt[i];
}
}
else if(oprationID==2) {
for (int i = 0; i < result.Length; i++)
{
//移除装备属性加成
result[i] = heroPpt[i] - equipPpt[i];
}
}
return result;
}
///
/// 获取英雄装备数组
///
///
///
public string[] GetHeroEquipsArray(string heroName) {
//获取装备字符串
string equips = GetHeroEquips(heroName);
//拆分字符串
return equips.Split(new char[] { '|' });
}
///
/// 获取英雄装备信息
///
///
public string GetHeroEquips(string heroName) {
//编写SQL语句
query = "Select HeroEquips From HeroTable Where HeroName='"+heroName+"'";
//执行
object result=SelectSingleData(query);
if (result==null) {
return null;
}
//返回结果
return result.ToString();
}///
/// 设置英雄的属性信息
///
///
private void SetHeroProperties(string heroName,int[] properties) {
//编写SQL语句
query = "Update HeroTable Set HeroAD="+properties[0]
+",HeroAP="+properties[1]+",HeroAR="+properties[2]+",HeroSR="+properties[3]+" Where HeroName='" + heroName + "'";
//执行SQL语句
Update(query);
}
///
/// 获取英雄此时的属性值
///
///
///
public int[] GetHeroProperties(string heroName) {
//编写SQL语句
query= "Select * From HeroTable Where HeroName='" + heroName + "'";
//执行SQL语句
List result=SelectMultipleData(query);
//实例化属性数组
int[] properties = new int[4];
//循环获取值
for (int i = 0; i < properties.Length; i++) {
//转换数据并存储
properties[i] = Convert.ToInt32(result[0][i + 2]);
}
//返回结果
return properties;
}
///
/// 获取当前装备的属性加成
///
///
///
public int[] GetEquipProperties(string equipName) {
//编写SQL语句
query = "Select * From ShopTable Where EquipName='" + equipName + "'";
//执行SQL语句
List result = SelectMultipleData(query);
//实例化属性数组
int[] properties = new int[4];
//遍历赋值
for (int i = 0; i < properties.Length; i++) {
//转换数据并存储
properties[i] = System.Convert.ToInt32(result[0][i + 2]);
}
//返回结果
return properties;
}
///
/// 获取英雄剩余金钱
///
///
///
public int GetHeroMoney(string heroName) {
//编写SQL语句
query = "Select HeroMoney From HeroTable Where HeroName='" + heroName + "'";
//执行SQL语句
object money = SelectSingleData(query);
//返回整型结果
return Convert.ToInt32(money);
}
///
/// 设置英雄金钱
///
public void SetHeroMoney(string heroName,int heroMoney) {
//编写SQL语句
query = "Update HeroTable Set HeroMoney="+ heroMoney.ToString()+ " Where HeroName='" + heroName + "'";
//执行SQL语句
Update(query);
}
///
/// 获取某个装备值多少钱
///
///
///
public int GetEquipMoney(string equipName) {
//编写SQL语句
query = "Select EquipMoney From ShopTable Where EquipName='"+equipName+"'";
//执行SQL语句
object money=SelectSingleData(query);
//返回整型结果
return System.Convert.ToInt32(money);
}
///
/// 获取商店中的所有装备
///
///
public string[] GetShopEquips() {
//编写SQL语句
query = "Select EquipName From ShopTable";
//执行语句
List result=SelectMultipleData(query);
//实例化装备数组
string[] equips = new string[result.Count];
//遍历存储装备名称
for (int i=0;i
equips[i] = result[i][0].ToString();
}
//返回
return equips;
}
public string GetHeroName() {
//编写sql语句
query = "Select HeroName From HeroTable";
//执行sql语句
return SelectSingleData(query).ToString();
}
}