项目02《游戏-09-开发》Unity3D
基于 项目02《游戏-08-开发》Unity3D ,
本次任务是做抽卡界面,获取的卡片增添在背包中,并在背包中可以删除卡片,
首先在Canvas下创建一个空物体,命名为LotteryPanel,作为抽卡界面,
在右上角的锚点处设置为拉伸模式,这样他的宽高就变成1920 * 1080,
首先给这个界面添加一个关闭按钮,
十连抽按键,
ctrl + d 复制一份按钮改为单抽,
然后创建一个Image子物体命名为LotteryItem并复制十份,
拖拽星级预制体,
修改位置,
添加颜色,
然后将其余9个LotteryItem删除,重新复制到10个,
再创建一个文件夹Lottery用来存放关于抽卡的预制体,
此时我们已经有了两个界面,一个是抽卡界面,一个是背包界面,
为了将这个两个界面联系起来,再做个主界面,
接下来调整位置,
添加好主界面之后,找到MainGame.cs脚本进行修改:
在PackageLocalData.cs脚本中增添方法:
using System.Collections.Generic;
using UnityEngine;
public class PackageLocalData{
static PackageLocalData _instance;
public static PackageLocalData Instance{
get{
if (_instance == null)
_instance = new PackageLocalData();
return _instance;
}
}
//存
public List<PackageLocalItem> items;
public void SavaPackage(){
string inventoryJson = JsonUtility.ToJson(this);
PlayerPrefs.SetString("PackageLocalData", inventoryJson);
PlayerPrefs.Save();
}
//取
public List<PackageLocalItem> LoadPackage(){
if (items != null)
return items;
if (PlayerPrefs.HasKey("PackageLocalData")){
string inventoryJson = PlayerPrefs.GetString("PackageLocalData");
PackageLocalData packageLocalData = JsonUtility.FromJson<PackageLocalData>(inventoryJson);
items = packageLocalData.items;
return items;
}
else{
items = new List<PackageLocalItem>();
return items;
}
}
//添加抽卡阶段
public void SavePackage(){
string inventoryJson = JsonUtility.ToJson(this);
PlayerPrefs.SetString("PackageLocalData", inventoryJson);
PlayerPrefs.Save();
}
}
[System.Serializable]
public class PackageLocalItem{
public string uid;
public int id;
public int num;
public int level;
public bool isNew;
public override string ToString(){
return string.Format($"[id] {id} [num] {num}");
}
}
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using UnityEngine;
using static UIManager;
public class MainGame : MonoBehaviour{
public static Player player;
void Awake(){
player = GameObject.Find("Player").GetComponent<Player>();
//背包系统
_instance = this;
DontDestroyOnLoad(gameObject);
}
//背包系统
static MainGame _instance;
PackageTable packageTable;
public static MainGame Instance{
get{
return _instance;
}
}
void Start(){
//UIManager.Instance.OpenPanel(UIConst.PackagePanel);
}
//对静态数据加载
public PackageTable GetPackageTable(){
if (packageTable == null)
packageTable = Resources.Load<PackageTable>("TableData/PackageTable");
return packageTable;
}
//对动态数据加载
public List<PackageLocalItem> GetPackageLocalData(){
return PackageLocalData.Instance.LoadPackage();
}
//根据ID去表格中拿到指定的数据
public PackageTableItem GetPackageItemById(int id){
List<PackageTableItem> packageDataList = GetPackageTable().dataList;
foreach (PackageTableItem item in packageDataList){
if (item.id == id)
return item;
}
return null;
}
//根据uid去本地数据中拿到动态数据
public PackageLocalItem GetPackageLocalItemByUId(string uid){
List<PackageLocalItem> packageDataList = GetPackageLocalData();
foreach (PackageLocalItem item in packageDataList){
if (item.uid == uid)
return item;
}
return null;
}
public List<PackageLocalItem> GetSortPackageLocalData(){
List<PackageLocalItem> localItems = PackageLocalData.Instance.LoadPackage();
localItems.Sort(new PackageItemComparer());//添加
return localItems;
}
public class PackageItemComparer : IComparer<PackageLocalItem>{
public int Compare(PackageLocalItem a, PackageLocalItem b){
PackageTableItem x = MainGame.Instance.GetPackageItemById(a.id);
PackageTableItem y = MainGame.Instance.GetPackageItemById(b.id);
//首先按star从大到小排序
int starComparison = y.star.CompareTo(x.star);
//如果star相同,则按id从大到小排序
if (starComparison == 0){
int idComparison = y.id.CompareTo(x.id);
if (idComparison == 0)
return b.level.CompareTo(a.level);
return idComparison;
}
return starComparison;
}
}
//添加抽卡阶段字段
public class GameConst {
//武器类型
public const int PackageTypeWeapon = 1;
//食物类型
public const int PackageTypeFood = 2;
}
//添加抽卡阶段
//根据类型获取配置的表格数据
public List<PackageTableItem> GetPackageTableByType(int type){
List<PackageTableItem> packageItems = new List<PackageTableItem>();
foreach (PackageTableItem packageItem in GetPackageTable().dataList){
if (packageItem.type == type)
packageItems.Add(packageItem);
}
return packageItems;
}
//添加抽卡阶段具体逻辑 随机抽卡,获得一件武器
public PackageLocalItem GetLotteryRandom1(){
List<PackageTableItem> packageItems = GetPackageTableByType(GameConst.PackageTypeWeapon);
int index = Random.Range(0, packageItems.Count);
PackageTableItem packageItem = packageItems[index];
PackageLocalItem packageLocalItem = new(){
uid = System.Guid.NewGuid().ToString(),
id = packageItem.id,
num = 1,
level = 1,
isNew = CheckWeaponIsNew(packageItem.id),
};
PackageLocalData.Instance.items.Add(packageLocalItem);
PackageLocalData.Instance.SavePackage();
return packageLocalItem;
}
public bool CheckWeaponIsNew(int id) {
foreach (PackageLocalItem packageLocalItem in GetPackageLocalData()) {
if (packageLocalItem.id == id)
return false;
}
return true;
}
//随机抽卡 十连抽
public List<PackageLocalItem> GetLotteryRandom10(bool sort = false) {
//随机抽卡
List<PackageLocalItem> packageLocalItems = new();
for (int i = 0; i < 10; i++) {
PackageLocalItem packageLocalItem = GetLotteryRandom1();
packageLocalItems.Add(packageLocalItem);
}
//武器排序
if (sort)
packageLocalItems.Sort(new PackageItemComparer());
return packageLocalItems;
}
}
下一步写整个抽卡界面的代码逻辑:
首先添加一个脚本LotteryPanel.cs
然后绑定脚本,
双击LotteryPanel.cs脚本修改代码:
using UnityEngine;
using UnityEngine.UI;
public class LotteryPanel : BasePanel{
Transform UIClose;
Transform UICenter;
Transform UILottery10;
Transform UILottery1;
GameObject LotteryCellPrefab;
protected override void Awake(){
base.Awake();
InitUI();
InitPrefab();
}
void InitUI() {
UIClose = transform.Find("TopRight/Close");
UICenter = transform.Find("Center");
UILottery10 = transform.Find("Bottom/Lottery10");
UILottery1 = transform.Find("Bottom/Lottery1");
UILottery10.GetComponent<Button>().onClick.AddListener(OnLottert10Btn);
UILottery1.GetComponent<Button>().onClick.AddListener(OnLottert1Btn);
UIClose.GetComponent<Button>().onClick.AddListener (OnClose);
}
void OnClose(){
print(">>>>>>>> OnClose");
}
void OnLottert1Btn(){
print(">>>>>>>> OnLottert1Btn");
}
void OnLottert10Btn(){
print(">>>>>>>> OnLottert10Btn");
}
void InitPrefab() {
LotteryCellPrefab = Resources.Load("Prefab/Panel/Lottery/LotteryItem") as GameObject;
}
}
修改UIManager.cs脚本:
using System.Collections.Generic;
using UnityEngine;
public class UIManager{
static UIManager _instance;
Transform _uiRoot;
//路径配置字典
Dictionary<string, string> pathDict;
//预制体缓存字典
Dictionary<string, GameObject> prefabDict;
//已打开界面的缓存字典
public Dictionary<string, BasePanel> panelDict;
public static UIManager Instance{
get{
if (_instance == null)
_instance = new UIManager();
return _instance;
}
}
public Transform UIRoot{
get{
if (_uiRoot == null){
if (GameObject.Find("Canvas"))
_uiRoot = GameObject.Find("Canvas").transform;
else
_uiRoot = new GameObject("Canvas").transform;
};
return _uiRoot;
}
}
UIManager(){
InitDicts();
}
void InitDicts(){
prefabDict = new Dictionary<string, GameObject>();
panelDict = new Dictionary<string, BasePanel>();
pathDict = new Dictionary<string, string>(){
{ UIConst.PackagePanel,"Package/PackagePanel"},//**
//添加抽卡路径
{ UIConst.LotteryPanel,"Lottery/LotteryPanel"},
};
}
public BasePanel GetPanel(string name){
BasePanel panel = null;
//检查是否已打开
if (panelDict.TryGetValue(name, out panel))
return panel;
return null;
}
public BasePanel OpenPanel(string name){
BasePanel panel = null;
//检查是否已打开
if (panelDict.TryGetValue(name, out panel)){
Debug.Log($"界面已打开 {name}");
return null;
}
//检查路径是否配置
string path = "";
if (!pathDict.TryGetValue(name, out path)){
Debug.Log($"界面名称错误 或未配置路径 {name}");
return null;
}
//使用缓存的预制体
GameObject panelPrefab = null;
if (!prefabDict.TryGetValue(name, out panelPrefab)){
string realPath = "Prefabs/Panel/" + path;
panelPrefab = Resources.Load<GameObject>(realPath) as GameObject;
prefabDict.Add(name, panelPrefab);
}
//打开界面
GameObject panelObject = GameObject.Instantiate(panelPrefab, UIRoot, false);
panel = panelObject.GetComponent<BasePanel>();
panelDict.Add(name, panel);
panel.OpenPanel(name);
return panel;
}
//关闭界面
public bool ClosePanel(string name){
BasePanel panel = null;
if (!panelDict.TryGetValue(name, out panel)){
Debug.LogError($"界面未打开 {name}");
return false;
}
panel.ClosePanel();
return true;
}
public class UIConst{
//配置常量
public const string PackagePanel = "PackagePanel";//**
//添加抽卡阶段
public const string LotteryPanel = "LotteryPanel";
}
}
配置完成之后就可以在MainGame.cs脚本中打开这个界面,
运行游戏点击十连抽和单抽就会做出响应,
为了将背包与抽卡界面关联起来,先写主页面脚本,
创建新脚本MainPanel.cs脚本,
绑定脚本,
双击MainPanel.cs修改代码:
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class MainPanel : BasePanel{
Transform UILottery;
Transform UIPackage;
Transform UIQuitBtn;
protected override void Awake(){
base.Awake();
InitUI();
}
void InitUI(){
UILottery = transform.Find("Top/LotteryBtn");
UIPackage = transform.Find("Top/PackageBtn");
UIQuitBtn = transform.Find("BottomLeft/QuitBtn");
UILottery.GetComponent<Button>().onClick.AddListener(OnBtnLottery);
UIPackage.GetComponent<Button>().onClick.AddListener(OnBtnPackage);
UIQuitBtn.GetComponent<Button>().onClick.AddListener(OnQuitGame);
}
void OnQuitGame(){
print(">>>>> OnQuitGame");
EditorApplication.isPlaying = false;
Application.Quit();
}
void OnBtnPackage(){
print(">>>>> OnBtnPackage");
UIManager.Instance.OpenPanel(UIManager.UIConst.PackagePanel);
ClosePanel();
}
void OnBtnLottery(){
print(">>>>> OnBtnLottery");
UIManager.Instance.OpenPanel(UIManager.UIConst.LotteryPanel);
ClosePanel();
}
}
接着修改UIManager.cs脚本:
using System.Collections.Generic;
using UnityEngine;
public class UIManager{
static UIManager _instance;
Transform _uiRoot;
//路径配置字典
Dictionary<string, string> pathDict;
//预制体缓存字典
Dictionary<string, GameObject> prefabDict;
//已打开界面的缓存字典
public Dictionary<string, BasePanel> panelDict;
public static UIManager Instance{
get{
if (_instance == null)
_instance = new UIManager();
return _instance;
}
}
public Transform UIRoot{
get{
if (_uiRoot == null){
if (GameObject.Find("Canvas"))
_uiRoot = GameObject.Find("Canvas").transform;
else
_uiRoot = new GameObject("Canvas").transform;
};
return _uiRoot;
}
}
UIManager(){
InitDicts();
}
void InitDicts(){
prefabDict = new Dictionary<string, GameObject>();
panelDict = new Dictionary<string, BasePanel>();
pathDict = new Dictionary<string, string>(){
{ UIConst.PackagePanel,"Package/PackagePanel"},//**
//添加抽卡路径
{ UIConst.LotteryPanel,"Lottery/LotteryPanel"},
//添加主页面转换路径
{ UIConst.MainPanel,"MainPanel"},
};
}
public BasePanel GetPanel(string name){
BasePanel panel = null;
//检查是否已打开
if (panelDict.TryGetValue(name, out panel))
return panel;
return null;
}
public BasePanel OpenPanel(string name){
BasePanel panel = null;
//检查是否已打开
if (panelDict.TryGetValue(name, out panel)){
Debug.Log($"界面已打开 {name}");
return null;
}
//检查路径是否配置
string path = "";
if (!pathDict.TryGetValue(name, out path)){
Debug.Log($"界面名称错误 或未配置路径 {name}");
return null;
}
//使用缓存的预制体
GameObject panelPrefab = null;
if (!prefabDict.TryGetValue(name, out panelPrefab)){
string realPath = "Prefabs/Panel/" + path;
panelPrefab = Resources.Load<GameObject>(realPath) as GameObject;
prefabDict.Add(name, panelPrefab);
}
//打开界面
GameObject panelObject = GameObject.Instantiate(panelPrefab, UIRoot, false);
panel = panelObject.GetComponent<BasePanel>();
panelDict.Add(name, panel);
panel.OpenPanel(name);
return panel;
}
//关闭界面
public bool ClosePanel(string name){
BasePanel panel = null;
if (!panelDict.TryGetValue(name, out panel)){
Debug.LogError($"界面未打开 {name}");
return false;
}
panel.ClosePanel();
return true;
}
public class UIConst{
//配置常量
public const string PackagePanel = "PackagePanel";//**
//添加抽卡阶段
public const string LotteryPanel = "LotteryPanel";
//添加抽卡的主界面阶段
public const string MainPanel = "MainPanel";
}
}
最后修改MainGame.cs脚本:
再修改LotteryPanel.cs脚本:
再修改PackagePanel.cs脚本:
调整资源包位置,
运行游戏即可在背包中进行切换了,
接下来继续修改LotteryPanel.cs脚本:
先不写十连抽函数更重要的是去写更新单抽的逻辑脚本:
首先创建一个脚本LotteryCell.cs脚本,
绑定脚本,
双击LotteryCell.cs修改脚本:
using UnityEngine;
using UnityEngine.UI;
public class LotteryCell : MonoBehaviour{
Transform UIImage;
Transform UIStars;
Transform UINew;
PackageLocalItem packageLocalItem;
PackageTableItem packageTableItem;
LotteryPanel uiParent;
void Awake(){
InitUI();
}
void InitUI(){
UIImage = transform.Find("Center/Image");
UIStars = transform.Find("Bottom/Stars");
UINew = transform.Find("Top/New");
UINew.gameObject.SetActive(false);
}
public void Refresh(PackageLocalItem packageLocalItem, LotteryPanel uiParent) {
//数据初始化
this.packageLocalItem = packageLocalItem;
this.packageTableItem = MainGame.Instance.GetPackageItemById(this.packageLocalItem.id);
this.uiParent = uiParent;
//刷新UI信息
RefreshImage();
}
void RefreshImage() {
Texture2D t = (Texture2D)Resources.Load(this.packageTableItem.imagePath);
Sprite temp = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0, 0));
UIImage.GetComponent<Image>().sprite = temp;
}
public void RefreshStars() {
for (int i = 0; i < UIStars.childCount; i++) {
Transform star = UIStars.GetChild(i);
if (this.packageTableItem.star > i)
star.gameObject.SetActive(true);
else
star.gameObject.SetActive(false);
}
}
}
为了方便拓展的一个排序模式,修改PackagePanel.cs脚本
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public enum PackageMode{
normal,
delete,
sort,
}
public class PackagePanel : BasePanel{
Transform UIMenu;
Transform UIMenuWeapon;
Transform UIMenuFood;
Transform UITabName;
Transform UICloseBtn;
Transform UICenter;
Transform UIScrollView;
Transform UIDetailPanel;
Transform UILeftBtn;
Transform UIRightBtn;
Transform UIDeletePanel;
Transform UIDeleteBackBtn;
Transform UIDeleteInfoText;
Transform UIDeleteConfirmBtn;
Transform UIBottomMenus;
Transform UIDeleteBtn;
Transform UIDetailBtn;
//添加
public GameObject PackageUIItemPrefab;
//添加一列用来容纳所有被选中的物品uid
public List<string> deleteChooseUid;
//添加删除属性
public PackageMode curMode = PackageMode.normal;
public void AddChooseDeleteUid(string uid) {
this.deleteChooseUid ??= new List<string>();
if(!this.deleteChooseUid.Contains(uid))
this.deleteChooseUid.Add(uid);
else
this.deleteChooseUid.Remove(uid);
RefreshDeletePanel();
}
//添加删除选中项
void RefreshDeletePanel(){
RectTransform scrollContent = UIScrollView.GetComponent<ScrollRect>().content;
foreach(Transform cell in scrollContent){
PackageCell packageCell = cell.GetComponent<PackageCell>();
//todo:物品刷新选中状态
//packageCell.RefreshDeleteState();
}
}
//添加 表示当前选中的物品时哪一个uid
string _chooseUid;
public string ChooseUid {
get { return _chooseUid; }
set {
_chooseUid = value;
RefreshDetail();
}
}
void RefreshDetail() {
//找到uid对应的动态数据
PackageLocalItem localItem = MainGame.Instance.GetPackageLocalItemByUId(ChooseUid);
//刷新详情界面
UIDetailPanel.GetComponent<PackageDetail>().Refresh(localItem, this);
}
override protected void Awake(){
base.Awake();
InitUI();
}
//添加1
void Start(){
RefreshUI();
}
//添加1
void RefreshUI(){
RefreshScroll();
}
//添加1
void RefreshScroll(){
//清理滚动容器中原本的物品
RectTransform scrollContent = UIScrollView.GetComponent<ScrollRect>().content;
for (int i = 0; i < scrollContent.childCount; i++)
Destroy(scrollContent.GetChild(i).gameObject);
//获取本地数据的方法拿到自己身上背包数据 并且根据背包数据初始化滚动容器
foreach (PackageLocalItem localData in MainGame.Instance.GetSortPackageLocalData()){
Transform PackageUIItem = Instantiate(PackageUIItemPrefab.transform, scrollContent) as Transform;
PackageCell packageCell = PackageUIItem.GetComponent<PackageCell>();
//添加2
packageCell.Refresh(localData, this);
}
}
void InitUI(){
InitUIName();
InitClick();
}
void InitUIName(){
UIMenu = transform.Find("TopCenter/Menu");
UIMenuWeapon = transform.Find("TopCenter/Menus/Weapon");
UIMenuFood = transform.Find("TopCenter/Menus/Food");
UITabName = transform.Find("LeftTop/TabName");
UICloseBtn = transform.Find("RightTop/Close");
UICenter = transform.Find("Center");
UIScrollView = transform.Find("Center/Scroll View");
UIDetailPanel = transform.Find("Center/DetailPanel");
UILeftBtn = transform.Find("Left/Button");
UIRightBtn = transform.Find("Right/Button");
UIDeletePanel = transform.Find("Bottom/DeletePanel");
UIDeleteBackBtn = transform.Find("Bottom/DeletePanel/Back");
UIDeleteInfoText = transform.Find("Bottom/DeletePanel/InfoText");
UIDeleteConfirmBtn = transform.Find("Bottom/DeletePanel/ConfirmBtn");
UIBottomMenus = transform.Find("Bottom/BottomMenus");
UIDeleteBtn = transform.Find("Bottom/BottomMenus/DeleteBtn");
UIDetailBtn = transform.Find("Bottom/BottomMenus/DetailBtn");
UIDeletePanel.gameObject.SetActive(false);
UIBottomMenus.gameObject.SetActive(true);
}
void InitClick(){
UIMenuWeapon.GetComponent<Button>().onClick.AddListener(OnClickWeapon);
UIMenuFood.GetComponent<Button>().onClick.AddListener(OnClickFood);
UICloseBtn.GetComponent<Button>().onClick.AddListener(OnClickClose);
UILeftBtn.GetComponent<Button>().onClick.AddListener(OnClickLeft);
UIRightBtn.GetComponent<Button>().onClick.AddListener(OnClickRight);
UIDeleteBackBtn.GetComponent<Button>().onClick.AddListener(OnDeleteBack);
UIDeleteConfirmBtn.GetComponent<Button>().onClick.AddListener(OnDeleteConfirm);
UIDeleteBtn.GetComponent<Button>().onClick.AddListener(OnDelete);
UIDetailBtn.GetComponent<Button>().onClick.AddListener(OnDetail);
}
void OnDetail(){
print(">>>>>>> OnDetail()");
}
//进入删除模式 ; 左下角删除按钮
void OnDelete(){
print(">>>>>>> OnDelete()");
curMode = PackageMode.delete;
UIDeletePanel.gameObject.SetActive(true);
}
//确认删除
void OnDeleteConfirm(){
print(">>>>>>> OnDeleteConfirm()");
if (this.deleteChooseUid == null)
return;
if (this.deleteChooseUid.Count == 0)
return;
MainGame.Instance.DeletePackageItems(this.deleteChooseUid);
//删除完成后刷新整个背包页面
RefreshUI();
}
//退出删除模式
void OnDeleteBack(){
print(">>>>>>> OnDeleteBack()");
curMode = PackageMode.normal;
UIDeletePanel.gameObject.SetActive(false);
//重置选中的删除列表
deleteChooseUid = new List<string>();
//刷新选中状态
RefreshDeletePanel();
}
void OnClickRight(){
print(">>>>>>> OnClickRight()");
}
void OnClickLeft(){
print(">>>>>>> OnClickLeft()");
}
void OnClickWeapon(){
print(">>>>>>> OnClickWeapon()");
}
void OnClickFood(){
print(">>>>>>> OnClickFood()");
}
void OnClickClose(){
ClosePanel();
UIManager.Instance.OpenPanel(UIManager.UIConst.MainPanel);
}
}
这里会出现报红,修改MainGame.cs脚本添加删除方法即可:
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using UnityEngine;
using static UIManager;
public class MainGame : MonoBehaviour{
public static Player player;
void Awake(){
player = GameObject.Find("Player").GetComponent<Player>();
//背包系统
_instance = this;
DontDestroyOnLoad(gameObject);
}
//背包系统
static MainGame _instance;
PackageTable packageTable;
public static MainGame Instance{
get{
return _instance;
}
}
void Start(){
UIManager.Instance.OpenPanel(UIConst.MainPanel);
}
//对静态数据加载
public PackageTable GetPackageTable(){
if (packageTable == null)
packageTable = Resources.Load<PackageTable>("TableData/PackageTable");
return packageTable;
}
//对动态数据加载
public List<PackageLocalItem> GetPackageLocalData(){
return PackageLocalData.Instance.LoadPackage();
}
//根据ID去表格中拿到指定的数据
public PackageTableItem GetPackageItemById(int id){
List<PackageTableItem> packageDataList = GetPackageTable().dataList;
foreach (PackageTableItem item in packageDataList){
if (item.id == id)
return item;
}
return null;
}
//根据uid去本地数据中拿到动态数据
public PackageLocalItem GetPackageLocalItemByUId(string uid){
List<PackageLocalItem> packageDataList = GetPackageLocalData();
foreach (PackageLocalItem item in packageDataList){
if (item.uid == uid)
return item;
}
return null;
}
public List<PackageLocalItem> GetSortPackageLocalData(){
List<PackageLocalItem> localItems = PackageLocalData.Instance.LoadPackage();
localItems.Sort(new PackageItemComparer());//添加
return localItems;
}
public class PackageItemComparer : IComparer<PackageLocalItem>{
public int Compare(PackageLocalItem a, PackageLocalItem b){
PackageTableItem x = MainGame.Instance.GetPackageItemById(a.id);
PackageTableItem y = MainGame.Instance.GetPackageItemById(b.id);
//首先按star从大到小排序
int starComparison = y.star.CompareTo(x.star);
//如果star相同,则按id从大到小排序
if (starComparison == 0){
int idComparison = y.id.CompareTo(x.id);
if (idComparison == 0)
return b.level.CompareTo(a.level);
return idComparison;
}
return starComparison;
}
}
//添加抽卡阶段字段
public class GameConst {
//武器类型
public const int PackageTypeWeapon = 1;
//食物类型
public const int PackageTypeFood = 2;
}
//添加抽卡阶段
//根据类型获取配置的表格数据
public List<PackageTableItem> GetPackageTableByType(int type){
List<PackageTableItem> packageItems = new List<PackageTableItem>();
foreach (PackageTableItem packageItem in GetPackageTable().dataList){
if (packageItem.type == type)
packageItems.Add(packageItem);
}
return packageItems;
}
//添加抽卡阶段具体逻辑 随机抽卡,获得一件武器
public PackageLocalItem GetLotteryRandom1(){
List<PackageTableItem> packageItems = GetPackageTableByType(GameConst.PackageTypeWeapon);
int index = Random.Range(0, packageItems.Count);
PackageTableItem packageItem = packageItems[index];
PackageLocalItem packageLocalItem = new(){
uid = System.Guid.NewGuid().ToString(),
id = packageItem.id,
num = 1,
level = 1,
isNew = CheckWeaponIsNew(packageItem.id),
};
PackageLocalData.Instance.items.Add(packageLocalItem);
PackageLocalData.Instance.SavePackage();
return packageLocalItem;
}
public bool CheckWeaponIsNew(int id) {
foreach (PackageLocalItem packageLocalItem in GetPackageLocalData()) {
if (packageLocalItem.id == id)
return false;
}
return true;
}
//随机抽卡 十连抽
public List<PackageLocalItem> GetLotteryRandom10(bool sort = false) {
//随机抽卡
List<PackageLocalItem> packageLocalItems = new();
for (int i = 0; i < 10; i++) {
PackageLocalItem packageLocalItem = GetLotteryRandom1();
packageLocalItems.Add(packageLocalItem);
}
//武器排序
if (sort)
packageLocalItems.Sort(new PackageItemComparer());
return packageLocalItems;
}
//添加删除背包道具方法
public void DeletePackageItems(List<string> uids) {
foreach(string uid in uids)
DeletePackageItem(uid,false);
PackageLocalData.Instance.SavePackage();
}
public void DeletePackageItem(string uid, bool needSave = true) {
PackageLocalItem packageLocalItem = GetPackageLocalItemByUId(uid);
if (packageLocalItem == null)
return;
PackageLocalData.Instance.items.Remove(packageLocalItem);
if (needSave)
PackageLocalData.Instance.SavePackage();
}
}
修改PackageCell.cs脚本:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class PackageCell : MonoBehaviour,IPointerClickHandler,IPointerEnterHandler,IPointerExitHandler{
Transform UIIcon;
Transform UIHead;
Transform UINew;
Transform UISelect;
Transform UILevel;
Transform UIStars;
Transform UIDeleteSelect;
//添加
Transform UISelectAni;
Transform UIMouseOverAni;
//动态数据
PackageLocalItem packageLocalData;
//静态数据
PackageTableItem packageTableItem;
//父物体也就是PackagePanel本身
PackagePanel uiParent;
void Awake(){
InitUIName();
}
void InitUIName(){
UIIcon = transform.Find("Top/Icon");
UIHead = transform.Find("Top/Head");
UINew = transform.Find("Top/New");
UILevel = transform.Find("Bottom/LevelText");
UIStars = transform.Find("Bottom/Stars");
UISelect = transform.Find("Select");
UIDeleteSelect = transform.Find("DeleteSelect");
//添加
UIMouseOverAni = transform.Find("MouseOverAni");
UISelectAni = transform.Find("SelectAni");
UIDeleteSelect.gameObject.SetActive(false);
//添加
UIMouseOverAni.gameObject.SetActive(false);
UISelectAni.gameObject.SetActive(false);
}
//刷新
public void Refresh(PackageLocalItem packageLocalData, PackagePanel uiParent){
//数据初始化
this.packageLocalData = packageLocalData;
this.packageTableItem = MainGame.Instance.GetPackageItemById(packageLocalData.id);
this.uiParent = uiParent;
//等级信息
UILevel.GetComponent<Text>().text = "Lv." + this.packageLocalData.level.ToString();
//是否是新获得?
UINew.gameObject.SetActive(this.packageLocalData.isNew);
Debug.Log("ImagePath: " + this.packageTableItem.imagePath);
//物品的图片
Texture2D t = (Texture2D)Resources.Load(this.packageTableItem.imagePath);
if (t != null){
Sprite temp = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0, 0));
// 继续处理 Sprite 对象
UIIcon.GetComponent<Image>().sprite = temp;
}
else{
// 处理纹理加载失败的情况
Debug.LogError("Failed to load texture.");
}
//刷新星级
RefreshStars();
}
//刷新星级
public void RefreshStars(){
for (int i = 0; i < UIStars.childCount; i++){
Transform star = UIStars.GetChild(i);
if (this.packageTableItem.star > i)
star.gameObject.SetActive(true);
else
star.gameObject.SetActive(false);
}
}
public void OnPointerClick(PointerEventData eventData){
//if (this.uiParent.ChooseUid == this.packageLocalData.uid)
// return;
//根据点击设置最新的uid 进而刷新详情界面
//this.uiParent.ChooseUid = this.packageLocalData.uid;
//UISelectAni.gameObject.SetActive(true);
//UISelectAni.GetComponent<Animator>().SetTrigger("In");
//添加删除方法:
if(this.uiParent.curMode == PackageMode.delete)
this.uiParent.AddChooseDeleteUid(this.packageLocalData.uid);
if (this.uiParent.ChooseUid == this.packageLocalData.uid)
return;
//根据点击设置最新的uid -> 进而刷新详情界面
this.uiParent.ChooseUid = this.packageLocalData.uid;
UISelectAni.gameObject.SetActive(true);
UISelectAni.GetComponent<Animator>().SetTrigger("In");
}
public void OnPointerEnter(PointerEventData eventData){
UIMouseOverAni.gameObject.SetActive(true);
UIMouseOverAni.GetComponent<Animator>().SetTrigger("In");
}
public void OnPointerExit(PointerEventData eventData){
Debug.Log($"OnPointerExit {eventData.ToString()}");
}
//添加删除方法
public void RefershDeleteState() {
if (this.uiParent.deleteChooseUid.Contains(this.packageLocalData.uid))
this.UIDeleteSelect.gameObject.SetActive(true);
else
this.UIDeleteSelect.gameObject.SetActive(false);
}
}
最后修改PackagePanel.cs脚本:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public enum PackageMode{
normal,
delete,
sort,
}
public class PackagePanel : BasePanel{
Transform UIMenu;
Transform UIMenuWeapon;
Transform UIMenuFood;
Transform UITabName;
Transform UICloseBtn;
Transform UICenter;
Transform UIScrollView;
Transform UIDetailPanel;
Transform UILeftBtn;
Transform UIRightBtn;
Transform UIDeletePanel;
Transform UIDeleteBackBtn;
Transform UIDeleteInfoText;
Transform UIDeleteConfirmBtn;
Transform UIBottomMenus;
Transform UIDeleteBtn;
Transform UIDetailBtn;
//添加
public GameObject PackageUIItemPrefab;
//添加一列用来容纳所有被选中的物品uid
public List<string> deleteChooseUid;
//添加删除属性
public PackageMode curMode = PackageMode.normal;
public void AddChooseDeleteUid(string uid) {
this.deleteChooseUid ??= new List<string>();
if(!this.deleteChooseUid.Contains(uid))
this.deleteChooseUid.Add(uid);
else
this.deleteChooseUid.Remove(uid);
RefreshDeletePanel();
}
//添加删除选中项
void RefreshDeletePanel(){
RectTransform scrollContent = UIScrollView.GetComponent<ScrollRect>().content;
foreach(Transform cell in scrollContent){
PackageCell packageCell = cell.GetComponent<PackageCell>();
//todo: 物品刷新选中状态
packageCell.RefershDeleteState();
}
}
//添加 表示当前选中的物品时哪一个uid
string _chooseUid;
public string ChooseUid {
get { return _chooseUid; }
set {
_chooseUid = value;
RefreshDetail();
}
}
void RefreshDetail() {
//找到uid对应的动态数据
PackageLocalItem localItem = MainGame.Instance.GetPackageLocalItemByUId(ChooseUid);
//刷新详情界面
UIDetailPanel.GetComponent<PackageDetail>().Refresh(localItem, this);
}
override protected void Awake(){
base.Awake();
InitUI();
}
//添加1
void Start(){
RefreshUI();
}
//添加1
void RefreshUI(){
RefreshScroll();
}
//添加1
void RefreshScroll(){
//清理滚动容器中原本的物品
RectTransform scrollContent = UIScrollView.GetComponent<ScrollRect>().content;
for (int i = 0; i < scrollContent.childCount; i++)
Destroy(scrollContent.GetChild(i).gameObject);
//获取本地数据的方法拿到自己身上背包数据 并且根据背包数据初始化滚动容器
foreach (PackageLocalItem localData in MainGame.Instance.GetSortPackageLocalData()){
Transform PackageUIItem = Instantiate(PackageUIItemPrefab.transform, scrollContent) as Transform;
PackageCell packageCell = PackageUIItem.GetComponent<PackageCell>();
//添加2
packageCell.Refresh(localData, this);
}
}
void InitUI(){
InitUIName();
InitClick();
}
void InitUIName(){
UIMenu = transform.Find("TopCenter/Menu");
UIMenuWeapon = transform.Find("TopCenter/Menus/Weapon");
UIMenuFood = transform.Find("TopCenter/Menus/Food");
UITabName = transform.Find("LeftTop/TabName");
UICloseBtn = transform.Find("RightTop/Close");
UICenter = transform.Find("Center");
UIScrollView = transform.Find("Center/Scroll View");
UIDetailPanel = transform.Find("Center/DetailPanel");
UILeftBtn = transform.Find("Left/Button");
UIRightBtn = transform.Find("Right/Button");
UIDeletePanel = transform.Find("Bottom/DeletePanel");
UIDeleteBackBtn = transform.Find("Bottom/DeletePanel/Back");
UIDeleteInfoText = transform.Find("Bottom/DeletePanel/InfoText");
UIDeleteConfirmBtn = transform.Find("Bottom/DeletePanel/ConfirmBtn");
UIBottomMenus = transform.Find("Bottom/BottomMenus");
UIDeleteBtn = transform.Find("Bottom/BottomMenus/DeleteBtn");
UIDetailBtn = transform.Find("Bottom/BottomMenus/DetailBtn");
UIDeletePanel.gameObject.SetActive(false);
UIBottomMenus.gameObject.SetActive(true);
}
void InitClick(){
UIMenuWeapon.GetComponent<Button>().onClick.AddListener(OnClickWeapon);
UIMenuFood.GetComponent<Button>().onClick.AddListener(OnClickFood);
UICloseBtn.GetComponent<Button>().onClick.AddListener(OnClickClose);
UILeftBtn.GetComponent<Button>().onClick.AddListener(OnClickLeft);
UIRightBtn.GetComponent<Button>().onClick.AddListener(OnClickRight);
UIDeleteBackBtn.GetComponent<Button>().onClick.AddListener(OnDeleteBack);
UIDeleteConfirmBtn.GetComponent<Button>().onClick.AddListener(OnDeleteConfirm);
UIDeleteBtn.GetComponent<Button>().onClick.AddListener(OnDelete);
UIDetailBtn.GetComponent<Button>().onClick.AddListener(OnDetail);
}
void OnDetail(){
print(">>>>>>> OnDetail()");
}
//进入删除模式 ; 左下角删除按钮
void OnDelete(){
print(">>>>>>> OnDelete()");
curMode = PackageMode.delete;
UIDeletePanel.gameObject.SetActive(true);
}
//确认删除
void OnDeleteConfirm(){
print(">>>>>>> OnDeleteConfirm()");
if (this.deleteChooseUid == null)
return;
if (this.deleteChooseUid.Count == 0)
return;
MainGame.Instance.DeletePackageItems(this.deleteChooseUid);
//删除完成后刷新整个背包页面
RefreshUI();
}
//退出删除模式
void OnDeleteBack(){
print(">>>>>>> OnDeleteBack()");
curMode = PackageMode.normal;
UIDeletePanel.gameObject.SetActive(false);
//重置选中的删除列表
deleteChooseUid = new List<string>();
//刷新选中状态
RefreshDeletePanel();
}
void OnClickRight(){
print(">>>>>>> OnClickRight()");
}
void OnClickLeft(){
print(">>>>>>> OnClickLeft()");
}
void OnClickWeapon(){
print(">>>>>>> OnClickWeapon()");
}
void OnClickFood(){
print(">>>>>>> OnClickFood()");
}
void OnClickClose(){
ClosePanel();
UIManager.Instance.OpenPanel(UIManager.UIConst.MainPanel);
}
}
修改LotteryPanel.cs脚本:
using System.Collections.Generic;
using UnityEditor.Build.Content;
using UnityEngine;
using UnityEngine.UI;
public class LotteryPanel : BasePanel{
Transform UIClose;
Transform UICenter;
Transform UILottery10;
Transform UILottery1;
GameObject LotteryCellPrefab;
protected override void Awake(){
base.Awake();
InitUI();
InitPrefab();
}
void InitUI() {
UIClose = transform.Find("TopRight/Close");
UICenter = transform.Find("Center");
UILottery10 = transform.Find("Bottom/Lottery10");
UILottery1 = transform.Find("Bottom/Lottery1");
UILottery10.GetComponent<Button>().onClick.AddListener(OnLottert10Btn);
UILottery1.GetComponent<Button>().onClick.AddListener(OnLottert1Btn);
UIClose.GetComponent<Button>().onClick.AddListener (OnClose);
}
void OnClose(){
print(">>>>>>>> OnClose");
ClosePanel();
UIManager.Instance.OpenPanel(UIManager.UIConst.MainPanel);
}
void OnLottert1Btn(){
print(">>>>>>>> OnLottert1Btn");
//销毁原本的卡片
for (int i = 0; i < UICenter.childCount; i++)
Destroy(UICenter.GetChild(i).gameObject);
//抽卡获得一张新的物品
PackageLocalItem item = MainGame.Instance.GetLotteryRandom1();
Transform LotteryCellTran = Instantiate(LotteryCellPrefab.transform, UICenter) as Transform;
// 对卡片做信息展示刷新
LotteryCell lotteryCell = LotteryCellTran.GetComponent<LotteryCell>();
lotteryCell.Refresh(item, this);
}
void OnLottert10Btn(){
print(">>>>>>>> OnLottert10Btn");
List<PackageLocalItem> packageLocalItems = MainGame.Instance.GetLotteryRandom10(sort: true);
for (int i = 0; i < UICenter.childCount; i++)
Destroy(UICenter.GetChild(i).gameObject);
foreach (PackageLocalItem item in packageLocalItems){
Transform LotteryCellTran = Instantiate(LotteryCellPrefab.transform, UICenter) as Transform;
// 对卡片做信息展示刷新
LotteryCell lotteryCell = LotteryCellTran.GetComponent<LotteryCell>();
lotteryCell.Refresh(item, this);
}
}
void InitPrefab() {
LotteryCellPrefab = Resources.Load("Prefabs/Panel/Lottery/LotteryItem") as GameObject;
}
}
End.