Unity3d开发“类三消”游戏

时间:2023-03-09 08:50:08
Unity3d开发“类三消”游戏

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

Unity3d开发“类三消”游戏    【1】                                    Unity3d开发“类三消”游戏         【3】

Unity3d开发“类三消”游戏【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脚本

Unity3d开发“类三消”游戏

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预设体添加消除素材图片

Unity3d开发“类三消”游戏

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

Unity3d开发“类三消”游戏

运行效果

Unity3d开发“类三消”游戏