新建一个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. java中的元数据

    java中的Annotation和C#中的Attribute相似. 写法上差别较大 @Target(ElementType.METHOD) @Retention(RetentionPolicy.CLA ...

  2. Mybatis 返回插入的主键

    业务中,会遇到这样的问题,就是感觉返回的主键,插入作为其他表的外键. 那么问题来了,如何去实现,其实方法比较简单,这里就是重点记录下,会出现的误区. 用自动生成sql工具的话,加上下面这句话 < ...

  3. STM32cube库配置双ADC的同步规则采样

    http://www.stmcu.org/module/forum/forum.php?mod=viewthread&tid=605203&extra=page%3D&page ...

  4. Linxu Yum方式安装Mysql

    1.下载yum源 进入http://dev.mysql.com/downloads/repo/,下载RedHat Enterprise Linux 6 / Oracle Linux 6版.文件名称:m ...

  5. Sahi (3) —— 压力测试Load Test以CAS SSO登陆场景为例(103 Tutorial)

    Sahi (3) -- 压力测试Load Test以CAS SSO登陆场景为例(103 Tutorial) jvm版本: 1.8.0_65 sahi版本: Sahi Pro 6.1.0 参考来源: S ...

  6. 如何修改被hosts.deny禁止访问的IP

    自己的密码忘记了,重试了很多次被加入了黑名单. 如果仅仅清空 /etc/hosts.deny 文件内容,你会发现过一会自己的IP又进入里面了. 其实系统后台会定期自动扫描一些文件,然后将这些异常的IP ...

  7. JProfiler 9版本注册码(亲测可用!!!)

    JProfiler 9版本注册码(亲测可用!!!) 按默认选择“Single or evaluation license” ,Name 和 Company 随意填!!! JProfiler 9.2  ...

  8. 多选下拉框带搜索(aps.net)

    自己写了一个带搜索功能的多选下拉框,为了要获取值,就没有封装插件,实现思路 1.一个文本框 做搜索 2.一个文本框显示选中文本,一个隐藏控件存值 3.一个div里面绑定CheckBoxList控件(这 ...

  9. vim for python

    set encoding=utf8 set paste  编辑模式下的 复制粘贴 set expandtab  貌似是一个 tab 替换成4个空格, 而且当你使用 backspace(删除键), 会自 ...

  10. Java设计模式(12)迭代模式(Iterator模式)

    上了这么多年学,我发现一个问题,好象老师都很喜欢点名,甚至点名都成了某些老师的嗜好,一日不点名,就饭吃不香,觉睡不好似的,我就觉得很奇怪,你的课要是讲的好,同学又怎么会不来听课呢,殊不知:“误人子弟, ...