蛋疼的时候写三消游戏(一)

现在手机上最火的几种游戏类型:塔防,三消,三国题材,于是有很多游戏就把这些揉一块了。。

《英雄三三杀》

《逆转三国》都算这一类了吧。。

三消的还是经典的有意思一些:

细数iPhone上那些经典的三消游戏

http://www.appifan.com/topic-232

以前在电脑上看过一种三消,它的移动方式是拖动走一整条的,当时觉得挺有意思,现在有了Unity应该可以方便的实现出来,

所以要自己试试了。

我先用photoshop画出三类的可消方块:

接下来要准备的就是U3D里的东西了,因为想做成2D的,我还是图个方便用NGUI吧。

先想个大概的界面布局:

注意NGUI的坐标中心点是在屏幕中心了,所以定义坐标时要自己转换了。

竖的红线的左边做为界面的显示,如分数,时间等。右边就是游戏的主要操作区域了。

我喜欢先把大概的显示搞定,再写游戏逻辑,这样比较先有成就感 嘿嘿。

后面到时可以加一些背景图,其实这边物体的长宽是不相等的。。我为了能自适应屏幕大小,偷懒就不搞成正方形了。。

代码如下:

using UnityEngine;
using System.Collections;

/// <summary>
/// FileName: UIManager.cs
/// Author: Star
/// Date: 12/12/16
/// Description: Manager of game UI
/// </summary>
public class UIManager : MonoBehaviour {
    
    public GameObject m_ItemPrefab;
    public GameObject m_GamePanel;
    
    Vector3 m_TopLeft = Vector3.zero;
    Vector3 m_GameBoardTopLeft = Vector3.zero;
    Vector3 m_GameBoardBottomRight = Vector3.zero;
    
    float m_LeftPercent = 0.2f;
    
    int NumRow = 10;
    int NumCol = 5;
    
    float m_fItemWidth = 0;
    float m_fItemHeight = 0;
    
    

    // Use this for initialization
    void Start () {
        
        m_TopLeft = new Vector3(-Screen.width/2, Screen.height/2, 0);
        m_GameBoardTopLeft = m_TopLeft + new Vector3(Screen.width * m_LeftPercent, 0, 0);
        m_GameBoardBottomRight = new Vector3(Screen.width/2, -Screen.height/2, 0);
        
        m_fItemWidth = Screen.width * (1-m_LeftPercent) / NumCol;
        m_fItemHeight = Screen.height / NumRow;
        
        InitItems();
    }
    
    void InitItems()
    {
        GameObject go = null;
        UISprite img = null;
        
        for (int i = 0; i < NumRow; i++)
        {
            for (int j = 0; j < NumCol; j++)
            {
                go = NGUITools.AddChild(m_GamePanel, m_ItemPrefab);
                go.transform.localPosition = new Vector3(j * m_fItemWidth + m_fItemWidth/2,
                    -i * m_fItemHeight - m_fItemHeight/2, 0) + m_GameBoardTopLeft;
                img = go.transform.FindChild("ItemImg").GetComponent<UISprite>();
                img.gameObject.transform.localScale = new Vector3(m_fItemWidth, m_fItemHeight, 0);
            }
        }
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}
原文地址:https://www.cnblogs.com/gameprogram/p/2820709.html