新建一个Project,导入图片素材和声音文件,把图片的Texture Type都修改为Sprite(2D and UI)【1】。新建一个命名为Background的GameObject,为之添加背景素材图片【2】。再新建一个命名为GameController的GameObject,为之添加GameController脚本和AudioSource组件。把消除素材图片都做成预设体(Prefabs)【3】,顺便再Copy多一个预设体,重命名为Gemstone,把Sprite设为空(None),为之添加Gemstone脚本和BoxCollider组件。

    【1】                                             【3】

【2】

GameController.sc脚本

 using System.Collections;  

 public class GameController : MonoBehaviour {
public Gemstone gemstone;
public int rowNum=;//宝石列数
public int columNum=;//宝石行数
public ArrayList gemstoneList;//定义列表
private Gemstone currentGemstone;
private ArrayList matchesGemstone;
public AudioClip match3Clip;
public AudioClip swapClip;
public AudioClip erroeClip;
// Use this for initialization
void Start () {
gemstoneList = new ArrayList ();//新建列表
matchesGemstone = new ArrayList ();
for (int rowIndex=; rowIndex<rowNum; rowIndex++) {
ArrayList temp=new ArrayList();
for(int columIndex=;columIndex<columNum;columIndex++){
Gemstone c=AddGemstone(rowIndex,columIndex);
temp.Add(c); }
gemstoneList.Add(temp);
}
if (CheckHorizontalMatches () || CheckVerticalMatches ()) {//开始检测匹配消除
RemoveMatches();
}
}
public Gemstone AddGemstone(int rowIndex,int columIndex){//生成宝石
Gemstone c = Instantiate (gemstone)as Gemstone;
c.transform.parent = this.transform;//生成宝石为GameController子物体
c.GetComponent<Gemstone>().RandomCreateGemstoneBg();
c.GetComponent<Gemstone>().UpdatePosition(rowIndex,columIndex);
return c;
} // Update is called once per frame
void Update () { }
public void Select(Gemstone c){
//Destroy (c.gameObject);
if (currentGemstone == null) {
currentGemstone = c;
currentGemstone.isSelected=true;
return;
} else {
if(Mathf.Abs(currentGemstone.rowIndex-c.rowIndex)+Mathf.Abs(currentGemstone.columIndex-c.columIndex)==){
//ExangeAndMatches(currentGemstone,c);
StartCoroutine(ExangeAndMatches(currentGemstone,c));
}else{
this.gameObject.GetComponent<AudioSource>().PlayOneShot(erroeClip);
}
currentGemstone.isSelected=false;
currentGemstone=null;
}
}
IEnumerator ExangeAndMatches(Gemstone c1,Gemstone c2){//实现宝石交换并且检测匹配消除
Exchange(c1,c2);
yield return new WaitForSeconds (0.5f);
if (CheckHorizontalMatches () || CheckVerticalMatches ()) {
RemoveMatches ();
} else {
Exchange(c1,c2);
}
}
bool CheckHorizontalMatches(){//实现检测水平方向的匹配
bool isMatches = false;
for (int rowIndex=; rowIndex<rowNum; rowIndex++) {
for (int columIndex=; columIndex<columNum-; columIndex++) {
if ((GetGemstone (rowIndex, columIndex).gemstoneType == GetGemstone (rowIndex, columIndex + ).gemstoneType) && (GetGemstone (rowIndex, columIndex).gemstoneType == GetGemstone (rowIndex, columIndex + ).gemstoneType)) {
//Debug.Log ("发现行相同的宝石");
AddMatches(GetGemstone(rowIndex,columIndex));
AddMatches(GetGemstone(rowIndex,columIndex+));
AddMatches(GetGemstone(rowIndex,columIndex+));
isMatches = true;
}
}
}
return isMatches;
}
bool CheckVerticalMatches(){//实现检测垂直方向的匹配
bool isMatches = false;
for (int columIndex=; columIndex<columNum; columIndex++) {
for (int rowIndex=; rowIndex<rowNum-; rowIndex++) {
if ((GetGemstone (rowIndex, columIndex).gemstoneType == GetGemstone (rowIndex + , columIndex).gemstoneType) && (GetGemstone (rowIndex, columIndex).gemstoneType == GetGemstone (rowIndex + , columIndex).gemstoneType)) {
//Debug.Log("发现列相同的宝石");
AddMatches(GetGemstone(rowIndex,columIndex));
AddMatches(GetGemstone(rowIndex+,columIndex));
AddMatches(GetGemstone(rowIndex+,columIndex));
isMatches=true;
}
}
}
return isMatches;
}
void AddMatches(Gemstone c){
if (matchesGemstone == null)
matchesGemstone = new ArrayList ();
int Index = matchesGemstone.IndexOf (c);//检测宝石是否已在数组当中
if (Index == -) {
matchesGemstone.Add(c);
}
}
void RemoveMatches(){//删除匹配的宝石
for (int i=; i<matchesGemstone.Count; i++) {
Gemstone c=matchesGemstone[i]as Gemstone;
RemoveGemstone(c);
}
matchesGemstone = new ArrayList ();
StartCoroutine (WaitForCheckMatchesAgain ());
}
IEnumerator WaitForCheckMatchesAgain(){//连续检测匹配消除
yield return new WaitForSeconds (0.5f);
if (CheckHorizontalMatches () || CheckVerticalMatches ()) {
RemoveMatches();
}
}
void RemoveGemstone(Gemstone c){//删除宝石
//Debug.Log("删除宝石");
c.Dispose ();
this.gameObject.GetComponent<AudioSource> ().PlayOneShot (match3Clip);
for (int i=c.rowIndex+; i<rowNum; i++) {
Gemstone temGemstone=GetGemstone(i,c.columIndex);
temGemstone.rowIndex--;
SetGemstone(temGemstone.rowIndex,temGemstone.columIndex,temGemstone);
//temGemstone.UpdatePosition(temGemstone.rowIndex,temGemstone.columIndex);
temGemstone.TweenToPostion(temGemstone.rowIndex,temGemstone.columIndex);
}
Gemstone newGemstone = AddGemstone (rowNum, c.columIndex);
newGemstone.rowIndex--;
SetGemstone (newGemstone.rowIndex, newGemstone.columIndex, newGemstone);
//newGemstone.UpdatePosition (newGemstone.rowIndex, newGemstone.columIndex);
newGemstone.TweenToPostion (newGemstone.rowIndex, newGemstone.columIndex);
}
public Gemstone GetGemstone(int rowIndex,int columIndex){//通过行号和列号,获取对应位置的宝石
ArrayList temp = gemstoneList [rowIndex]as ArrayList;
Gemstone c = temp [columIndex]as Gemstone;
return c;
}
public void SetGemstone(int rowIndex,int columIndex,Gemstone c){//设置所对应行号和列号的宝石
ArrayList temp = gemstoneList [rowIndex]as ArrayList;
temp [columIndex] = c;
}
public void Exchange(Gemstone c1,Gemstone c2){//实现宝石交换位置
this.gameObject.GetComponent<AudioSource> ().PlayOneShot (swapClip);
SetGemstone (c1.rowIndex, c1.columIndex, c2);
SetGemstone (c2.rowIndex, c2.columIndex, c1);
//交换c1,c2的行号
int tempRowIndex;
tempRowIndex = c1.rowIndex;
c1.rowIndex = c2.rowIndex;
c2.rowIndex = tempRowIndex;
//交换c1,c2的列号
int tempColumIndex;
tempColumIndex = c1.columIndex;
c1.columIndex = c2.columIndex;
c2.columIndex = tempColumIndex; //c1.UpdatePosition (c1.rowIndex, c1.columIndex);
//c2.UpdatePosition (c2.rowIndex, c2.columIndex);
c1.TweenToPostion (c1.rowIndex, c1.columIndex);
c2.TweenToPostion (c2.rowIndex, c2.columIndex);
}

为GameController添加声音源文件和Gemstone脚本

Gemstone.cs脚本

 using System.Collections;  

 public class Gemstone : MonoBehaviour {  

     public float xOffset = -4.5f;//x方向的偏移
public float yOffset = -2.0f;//y方向的偏移
public int rowIndex = ;
public int columIndex = ;
public GameObject[] gemstoneBgs;//宝石数组
public int gemstoneType;//宝石类型
private GameObject gemstoneBg;
private GameController gameController;
private SpriteRenderer spriteRenderer;
public bool isSelected{
set{
if(value){
spriteRenderer.color=Color.red;
}else{
spriteRenderer.color=Color.white;
}
}
}
// Use this for initialization
void Start () {
gameController = GameObject.Find ("GameController").GetComponent<GameController> ();
spriteRenderer = gemstoneBg.GetComponent<SpriteRenderer> ();
} // Update is called once per frame
void Update () { }
public void UpdatePosition(int _rowIndex,int _columIndex){//宝石的位置
rowIndex = _rowIndex;
columIndex = _columIndex;
this.transform.position = new Vector3 (columIndex + xOffset, rowIndex + yOffset, );
}
public void TweenToPostion(int _rowIndex,int _columIndex){//调用iTween插件实现宝石滑动效果
rowIndex = _rowIndex;
columIndex = _columIndex;
iTween.MoveTo (this.gameObject, iTween.Hash ("x", columIndex + xOffset, "y", rowIndex + yOffset, "time", 0.5f));
}
public void RandomCreateGemstoneBg(){//随机的宝石类型
if (gemstoneBg != null)
return;
gemstoneType = Random.Range (, gemstoneBgs.Length);
gemstoneBg = Instantiate (gemstoneBgs [gemstoneType])as GameObject;
gemstoneBg.transform.parent = this.transform;
}
public void OnMouseDown(){
gameController.Select (this);
}
public void Dispose(){
Destroy (this.gameObject);
Destroy (gemstoneBg.gameObject);
gameController = null;
}

为Gemstone预设体添加消除素材图片

最后在MainCamera添加AudioSource组件来播放背景音乐

运行效果

Unity3d开发“类三消”游戏的更多相关文章

  1. 消消乐、candy crush类三消游戏程序逻辑分析

    最近在开发一款类似消消乐的三消游戏,在碰到实现斜方向下落的时候卡住了很长时间.好几天没有思路,原本的思路是一次性预判多个宝石的一连串运动路径,运用缓动运动队列来实现宝石运动路径,例如 下落->滑 ...

  2. Unity3D开发类似保龄球游戏

    先学习一些基本的脚本实现: 1.动态创建物体.默认位置是(0,0)位置 GameObject goNew = GameObject.CreatePrimitive(PrimitiveType.Cube ...

  3. Unity3d开发IOS游戏 基础

    Unity3d开发IOS游戏 基础 @阿龙 -  649998群 1.先说明两个问题,我在WIN7下面的U3D里面,用了雅黑字体,但是导出为ios后,字体就看不见了,这是为什么呢?这是需要在MAC下找 ...

  4. Unity 4.2.0 官方最新破解版(Unity3D 最新破解版,3D游戏开发工具和游戏引擎套件)

    Unity是一款跨平台的游戏开发工具,从一开始就被设计成易于使用的产品.作为一个完全集成的专业级应用,Unity还包含了价值数百万美元的功能强大的游戏引擎.Unity作为一个游戏开发工具,它的设计主旨 ...

  5. 添加启动游戏过渡场景Default Splash Scene(Unity3D开发之十三)

    添加启动游戏过渡场景Default Splash Scene(Unity3D开发之十三) 猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blo ...

  6. Unity3D游戏开发——编程实现游戏管理器

    本篇简介 本篇介绍了如何将上一篇的设计模式思想运用到实际的开发过程中. 脚本文件 (1)IGameManager:这个接口存在声明了一个属性(一个拥有getter函数的变量,属性的类型是Manager ...

  7. Unity2016 Unity3D开发VR游戏的经验

    http://z.youxiputao.com/articles/8313 在4月12日的Unite 2016大会上,暴风魔镜高级产品经理吴涛分享他用Unity3D开发VR游戏的经验,以下为分享实录: ...

  8. 【Android开发VR实战】三.开发一个寻宝类VR游戏TreasureHunt

    转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53939303 本文出自[DylanAndroid的博客] [Android开发 ...

  9. Unity3D核心类介绍

    脚本介绍与Unity核心类介绍 -------------------------------------------------------------------------------- 脚本介 ...

随机推荐

  1. 为什么页面设计宽度要控制在960px

    其实这里涉及到了一个网页栅格系统的问题,而且这个观念是从苹果的设计师那里来的 网站 首页页面宽度 px Yahoo! 950 淘宝 950 MySpace 960 新浪 950 网易 960 Live ...

  2. python 全局变量的使用

    我尝试使用 类似 C 语言的方式去调用 python 的全局变量,发现不行,后经过 尝试,要使用 global 进行调用 test_num = 0; # 首先声明一个全局变量 def test_fun ...

  3. STM32F10x_ADC三通道DMA连续转换(3通道、软件单次触发)

    Ⅰ.概述 上一篇文章讲述的内容是:三通道逐次转换(单次.单通道软件触发),也就是说3条通道要三次软件触发才能完成转换,而且是通过软件读取转换数值. 本文讲述三通道DMA连续转换(3通道.软件单次触发) ...

  4. SpringMVC REST 风格静态资源访问配置

    1 在web.xml中使用默认servlet处理静态资源,缺点是如果静态资源过多,则配置量会比较大,一旦有遗漏,则会造成资源无法正常显示或404错误. <!-- 静态资源访问控制 --> ...

  5. CPP_template

    泛型编程是独立于任何特定类型的方式编写代码.模板是泛型编程的基础,模板使程序员能够快速建立具有类型安全的类库集合和函数集合,它的实现,方便了大规模的软件开发. 模板提供通用类型和通用函数,定义中包含t ...

  6. Sahi (2) —— https/SSL配置(102 Tutorial)

    Sahi (2) -- https/SSL配置(102 Tutorial) jvm版本: 1.8.0_65 sahi版本: Sahi Pro 6.1.0 参考来源: Sahi官网 Sahi Quick ...

  7. window.print()局部打印三种方式

    首先准备要打印的内容,也可以打印时再填充,html中定义如下: <!--startprint--> <div id="printcontent" style=&q ...

  8. 百度地图Api进阶教程-点聚合9.html

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. C#内置泛型委托:Func委托

    1.什么是Func委托 Func委托代表有返回类型的委托 2.Func委托定义 查看Func的定义: using System.Runtime.CompilerServices; namespace ...

  10. jquery 回车事件实现代码

    // 键盘事件 1.keydown()  keydown事件会在键盘按下时触发. 2.keyup()  keyup事件会在按键释放时触发,也就是你按下键盘起来后的事件 3.keypress()  ke ...