(游戏)卡牌记忆01_启动屏界面: 传送门
(游戏)卡牌记忆02_主菜单界面: 传送门
(游戏)卡牌记忆03_选关卡界面: 传送门
(游戏)卡牌记忆04_游戏界面 : 未完
游戏界面效果未完~
实现过程
初步创建主菜单界面,背景自适应1920*1080分辨率
左上角创建两个文本Text显示游戏关卡信息
LevelLabel固定文本:“关卡:” LevelText:"(动态生成关卡信息)"
LevelLabel固定在场景的左上角
LevelText固定在LevelLabel 文本右侧
界面右边放置三个Button按钮
Gameplay场景中间创建Panel控件
制作卡牌预设体
Gamearea创建一个GameObject子对象,子对象下创建三个Image控件
bg(Image): 背景(为了统一所有卡牌背景尺寸)
icon(Image): 游戏主题类型图片
mark(Image): 卡牌翻过来后把卡牌锁上(默认下次点击不能再次翻面)
创建卡牌预设体脚本,将脚本绑定到预设体上
动态制作棋牌
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; //GameManager用来管理游戏状态,资源等整体性 public class GameManager : MonoBehaviour { public GameObject cardPreb; private LevelInfo levelInfo; //本关卡数据信息 int levelID; private void Awake() { levelInfo = DataMgr.Instance().levelInfo; levelID = DataMgr.Instance().levelId; } // Use this for initialization void Start () { levelInfo = DataMgr.Instance().levelInfo; InitBoard(); } // Update is called once per frame void Update () { } //初始化棋盘 void InitBoard() { //求游戏区域的宽和高 GameObject gameAreaObj = GameObject.Find("GameArea"); RectTransform rectTrans = gameAreaObj.GetComponent<RectTransform>(); float gameWidth = rectTrans.rect.width; float gameHeight = rectTrans.rect.height; //获取关卡信息 int row = levelInfo.row; int col = levelInfo.col; //根据关卡的行列信息初始化位置 float spaceingW = gameWidth / col / 10; float spaceingH = gameHeight / row / 10; float cellW = (gameWidth - spaceingW * (col + 1)) / col; float cellH = (gameHeight - spaceingH * (row + 1)) / row; float cellSize = Mathf.Min(cellW, cellH); //最终求出正方形卡牌尺寸 float spacingX = (gameWidth - cellSize * col) / (col + 1); //求出水平和垂直方向实际间隙 float spacingY = (gameHeight - cellSize * row) / (row + 1); int count = row * col; //创建所有卡牌 for(int i = 0; i < count; i++) { int row2 = i / col; int col2 = i % col; GameObject cardObj = Instantiate(cardPreb,rectTrans); //通过预制体创建卡牌对象 cardObj.name = "Card" + i.ToString(); RectTransform cardTrans = cardObj.GetComponent<RectTransform>(); cardTrans.anchoredPosition = new Vector2(spacingX+(spacingX + cellSize) * col2,-spacingY - (spacingY+cellSize)*row2); cardTrans.sizeDelta = new Vector2(cellSize, cellSize); //设置卡牌单元的宽高 } } }
添加游戏管理器,绑定到GameManager(GameObject)上
初始化棋盘
动态创建棋牌会出现一个小问题,三行四列和四行五列动态创建卡牌后每张卡牌之间间隙不同,所以要求出卡牌水平和垂直之间的间隙
void InitBoard() { //求游戏区域的宽和高 GameObject gameAreaObj = GameObject.Find("GameArea"); RectTransform rectTrans = gameAreaObj.GetComponent<RectTransform>(); float gameWidth = rectTrans.rect.width; float gameHeight = rectTrans.rect.height; //获取关卡信息 int row = levelInfo.row; int col = levelInfo.col; //根据关卡的行列信息初始化位置 float spaceingW = gameWidth / col / 10; float spaceingH = gameHeight / row / 10; float cellW = (gameWidth - spaceingW * (col + 1)) / col; float cellH = (gameHeight - spaceingH * (row + 1)) / row; float cellSize = Mathf.Min(cellW, cellH); //最终求出正方形卡牌尺寸 float spacingX = (gameWidth - cellSize * col) / (col + 1); //求出水平和垂直方向实际间隙 float spacingY = (gameHeight - cellSize * row) / (row + 1); int count = row * col; //创建所有卡牌 for(int i = 0; i < count; i++) { int row2 = i / col; int col2 = i % col; GameObject cardObj = Instantiate(cardPreb,rectTrans); //通过预制体创建卡牌对象 cardObj.name = "Card" + i.ToString(); RectTransform cardTrans = cardObj.GetComponent<RectTransform>(); cardTrans.anchoredPosition = new Vector2(spacingX+(spacingX + cellSize) * col2,-spacingY - (spacingY+cellSize)*row2); cardTrans.sizeDelta = new Vector2(cellSize, cellSize); //设置卡牌单元的宽高 } }
卡牌数据关联