新建一个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. [转]关于oracle with as用法

    原文地址:https://www.cnblogs.com/linjiqin/archive/2013/06/24/3152667.html with as语法–针对一个别名with tmp as (s ...

  2. TP v5中Url Compat模式

    compatible 对于配置pathinfo的支持,在Nginx作服务器.无数种系统要同时运行的环境,实在是一项很累赘的事情,而又不想很low的多个参数(像m.c.a)构造路由参数,我需要那种不必强 ...

  3. gitlab 地址https://www.gitlab.com.cn/installation/#centos-7

    https://www.gitlab.com.cn/installation/#centos-7 1.安装并配置必要的依赖关系 在CentOS 7(和RedHat / Oracle / Scienti ...

  4. gitlab安装与配置(Centos6.8)

    0.Centos7请参照官方文档 https://about.gitlab.com/installation/#centos-7 1. Install and configure the necess ...

  5. 基于Zookeeper的分步式队列系统集成案例

    基于Zookeeper的分步式队列系统集成案例 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, ...

  6. 线段树 + 区间更新 + 模板 ---- poj 3468

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59798   ...

  7. 解决java.lang.IllegalStateException: The application’s PagerAdapter changed the adapter’s content

    A界面中有viewpager的动态加载,从界面A跳到界面B,再finish掉B返回A时报出此异常. java.lang.IllegalStateException: The application's ...

  8. 【转】【Mac】invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library

    异常原因 我在昨天升级了 macOX Sierra,悲剧的是,今天我发现git命令无法执行,homebrew也无法使用,这种情景我在升级OS X El Capitan也遇到过一次,完整异常提示如下: ...

  9. [Python学习]Iterator 和 Generator的学习心得

    [Python学习]Iterator 和 Generator的学习心得 Iterator是迭代器的意思,它的作用是一次产生一个数据项,直到没有为止.这样在 for 循环中就可以对它进行循环处理了.那么 ...

  10. idea破解,idea激活,使用破解补丁无需注册码

    dea激活,JetBrain旗下软件激活 前言 idea激活有多种方式,网上较多的是使用注册码或者填License server网址,目前(2017年8月19日)使用注册码的方式,亲测可用的只有lan ...