2D游戏之五子棋(1)显示棋盘,棋子

2D游戏之五子棋(1)显示棋盘,棋子_第1张图片
2D游戏之五子棋(1)显示棋盘,棋子_第2张图片

2D五子棋游戏
资源预处理:
1.都是2d图片,不需要勾选inspector面板的Generate Mip Maps,会造成图片模糊
2.Filter Mode把默认的过滤方式调成点过滤(point)
图片按照2d的方式调整好了

棋盘会出现线不整齐的现象,由于屏幕分辨率造成

图像的匹配工作:
1.调整摄像机模式:
Render Mode弄成camera
2.关于缩放:
Canvas Scale的UI Scale Mode换成Scale with Screen Size(按照屏幕的方式来缩放)
3.分辨率是1280*720

4.Match是1 就是按照高来匹配

关于棋子:用button组件制作
每个棋子挂Cross脚本
代码如下:

using UnityEngine;
using UnityEngine.UI;

// 每个交叉点逻辑
public class Cross : MonoBehaviour {

    // 位置
    public int GridX;
    public int GridY;

    public MainLoop mainLoop;

    void Start () {
        GetComponent

棋牌面板挂脚本Broad,代码如下:

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

public class Board : MonoBehaviour {

    public GameObject CrossPrefab;

    // 交叉点大小
    const float CrossSize = 40;

    // 15个交叉
    public const int CrossCount = 15;

    // 棋盘大小
    public const int Size = 560;
    public const int HalfSize = Size / 2;

    // 存储每个交叉点按钮信息
    Dictionary<int, Cross> _crossMap = new Dictionary<int, Cross>();

    static int MakeKey( int x, int y )
    {
        return x * 10000 + y;
    }

    public void Reset( )
    {
        // 删除棋盘上的所有对象
        foreach( Transform child in gameObject.transform )
        {
            GameObject.Destroy(child.gameObject);
        }

        var mainLoop = GetComponent();

        _crossMap.Clear();

        for (int x = 0; x < Board.CrossCount; x++)
        {
            for (int y = 0; y < Board.CrossCount; y++)
            {
                var crossObj = GameObject.Instantiate(CrossPrefab);

                // 归属于本层对象下
                crossObj.transform.SetParent(gameObject.transform);

                // 复位缩放
                crossObj.transform.localScale = Vector3.one;

                // 设置位置
                var pos = crossObj.transform.localPosition;
                pos.x = -Board.HalfSize + x * CrossSize;
                pos.y = -Board.HalfSize + y * CrossSize;
                pos.z = 1;
                crossObj.transform.localPosition = pos;

                // 记录格子信息
                var cross = crossObj.GetComponent();
                cross.GridX = x;
                cross.GridY = y;
                cross.mainLoop = mainLoop;

                _crossMap.Add(MakeKey(x, y ), cross);


            }
        }
    }



    void Start () {

        Reset();
    }

    public Cross GetCross(int gridX, int gridY )
    {        
        Cross cross;
        if ( _crossMap.TryGetValue( MakeKey(gridX, gridY), out cross ) )
        {
            return cross;
        }

        return null;
    }


}

你可能感兴趣的:(unity3d,unity,2d,五子棋)