1. 程式人生 > >Unity3d開發“類三消”遊戲

Unity3d開發“類三消”遊戲

  新建一個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指令碼

<span style="font-size:14px;">using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {
	public Gemstone gemstone;
	public int rowNum=7;//寶石列數
	public int columNum=10;//寶石行數
	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=0; rowIndex<rowNum; rowIndex++) {
			ArrayList temp=new ArrayList();
			for(int columIndex=0;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)==1){
				//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=0; rowIndex<rowNum; rowIndex++) {
			for (int columIndex=0; columIndex<columNum-2; columIndex++) {
				if ((GetGemstone (rowIndex, columIndex).gemstoneType == GetGemstone (rowIndex, columIndex + 1).gemstoneType) && (GetGemstone (rowIndex, columIndex).gemstoneType == GetGemstone (rowIndex, columIndex + 2).gemstoneType)) {
					//Debug.Log ("發現行相同的寶石");
					AddMatches(GetGemstone(rowIndex,columIndex));
					AddMatches(GetGemstone(rowIndex,columIndex+1));
					AddMatches(GetGemstone(rowIndex,columIndex+2));
					isMatches = true;
				}
			}
		}
		return isMatches;
	}
	bool CheckVerticalMatches(){//實現檢測垂直方向的匹配
		bool isMatches = false;
		for (int columIndex=0; columIndex<columNum; columIndex++) {
			for (int rowIndex=0; rowIndex<rowNum-2; rowIndex++) {
				if ((GetGemstone (rowIndex, columIndex).gemstoneType == GetGemstone (rowIndex + 1, columIndex).gemstoneType) && (GetGemstone (rowIndex, columIndex).gemstoneType == GetGemstone (rowIndex + 2, columIndex).gemstoneType)) {
					//Debug.Log("發現列相同的寶石");
					AddMatches(GetGemstone(rowIndex,columIndex));
					AddMatches(GetGemstone(rowIndex+1,columIndex));
					AddMatches(GetGemstone(rowIndex+2,columIndex));
					isMatches=true;
				}
			}
		}
		return isMatches;
	}
	void AddMatches(Gemstone c){
		if (matchesGemstone == null)
			matchesGemstone = new ArrayList ();
		int Index = matchesGemstone.IndexOf (c);//檢測寶石是否已在陣列當中
		if (Index == -1) {
			matchesGemstone.Add(c);
		}
	}
	void RemoveMatches(){//刪除匹配的寶石
		for (int i=0; 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+1; 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);
	}
}</span>

為GameController新增聲音原始檔和Gemstone指令碼


Gemstone.cs指令碼

<span style="font-size:14px;">using UnityEngine;
using System.Collections;

public class Gemstone : MonoBehaviour {

	public float xOffset = -4.5f;//x方向的偏移
	public float yOffset = -2.0f;//y方向的偏移
	public int rowIndex = 0;
	public int columIndex = 0;
	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, 0);
	}
	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 (0, 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;
	}
}</span>

為Gemstone預設體新增消除素材圖片


最後在MainCamera新增AudioSource元件來播放背景音樂


執行效果