下载地址工程
先来看一下运行之后的效果图
素材包大家可以下载工程,工程里面都有!会需要导入一个iTween的插件
场景的搭建我就不做过多的介绍了,每个人有自己喜欢的场景!
看一下!CameController.cs完成整个游戏的控制
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CameController : MonoBehaviour {
public GemStone gemstone;
public int rowNum = 10;//列
public int coluNum = 7;//行
public ArrayList gamestonrlist;//存储游戏物体的列表
private GemStone currentgemstone;//保存当前点击
private ArrayList mathesgemstone;//相同宝石的数组
public ArrayList MidGemstonelist;//取得中间位置的行和列的宝石
/// <summary>
/// 三种声音
/// </summary>
public AudioClip mathesClip;//消除之后的声音
public AudioClip swapclip;//物体交换的声音
public AudioClip errorclip;//不能交换的声音
static float EnergButton;//表示当技能栏充满的时候
// Use this for initialization
void Start () {
gamestonrlist = new ArrayList ();
mathesgemstone = new ArrayList ();
MidGemstonelist = new ArrayList ();
for (int rowindex=0; rowindex<rowNum; rowindex++) {
ArrayList temp = new ArrayList();
for(int coluindex=0;coluindex<coluNum;coluindex++)
{
GemStone c = AddGemstone(rowindex,coluindex);//生成一行宝石,将行添加到temp里面!
temp.Add(c);
}
gamestonrlist.Add(temp);//将所有的行添加到gamestonelist里面。
}
if (CheckHorizontalMatches () || CheckVerticalMathces ()) {
RemoveMathes();
}
}
// Update is called once per frame
void Update () {
}
public GemStone AddGemstone(int rowindex,int coluindex)//生成宝石
{
GemStone c = Instantiate (gemstone) as GemStone;//强制装换
c.transform.parent = this.transform;
c.GetComponent<GemStone>().RandomCreateGemstone();
c.GetComponent<GemStone>().UpdatePosition(rowindex,coluindex);
return c;
}
public void Select(GemStone c)
{
if (currentgemstone == null) {
currentgemstone = c;
currentgemstone.isSelectd = true;
return;
} else {
if((Mathf.Abs(currentgemstone.rowIndex-c.rowIndex)+Mathf.Abs(currentgemstone.clouIndex-c.clouIndex))==1)
{
//ExangeAndMatches(currentgemstone,c);
StartCoroutine(ExangeAndMatches(currentgemstone,c));//协程的调用
}else
{
audio.PlayOneShot(errorclip);
}
currentgemstone.isSelectd = false;
currentgemstone=null;//不能多次交换
}
}
//协程
IEnumerator ExangeAndMatches(GemStone c1,GemStone c2)//实现交换监测行和列是否有相同的宝石
{
Change (c1, c2);
yield return new WaitForSeconds (0.5f);
if (CheckHorizontalMatches () || CheckVerticalMathces ()) {
RemoveMathes ();
} else {
Debug.Log("没有检测到相同的宝石,交换回来");
Change (c1, c2);
}
}
void AddMathes(GemStone c)
{
if (mathesgemstone == null) {
mathesgemstone = new ArrayList();
}
int index = mathesgemstone.IndexOf (c);//检测该宝石是否已经存在数组中
if (index == -1) {
mathesgemstone.Add(c);
}
}
void AddMidMathes(GemStone c)//找到中间一行的宝石,和中间一列的宝石
{
if (MidGemstonelist == null) {
MidGemstonelist = new ArrayList();
}
int index = MidGemstonelist.IndexOf (c);//检测是否在列表中
if (index == -1) {
MidGemstonelist.Add(c);
}
}
void RemoveMathes()//消除相同的宝石
{
for (int i=0; i<mathesgemstone.Count; i++) {
GemStone c = mathesgemstone[i] as GemStone;
RemoveGemstone(c);
}
GameObject.FindGameObjectWithTag ("score").GetComponent<ScoreScript> ().Score+=1;//实现消失了一组之后再增加分数
EnergButton=GameObject.FindGameObjectWithTag ("energline").GetComponent<Scrollbar> ().value+=0.1f;
if (GameObject.FindGameObjectWithTag ("score").GetComponent<ScoreScript> ().Score%50==0) {
GameObject.FindGameObjectWithTag ("ExScore").GetComponent<ScoreScript> ().Score+=1;
Application.LoadLevel ("GameStart");
}
Debug.Log(EnergButton);
Energ ();
mathesgemstone = new ArrayList ();
StartCoroutine (WaitForCheckMathesAgain ());
}
/// <summary>
/// 消除中间的宝石
/// </summary>
void RemoveMid()
{
for (int i=0; i<MidGemstonelist.Count; i++) {
GemStone c = MidGemstonelist[i] as GemStone;
RemoveGemstone(c);
}
GameObject.FindGameObjectWithTag ("score").GetComponent<ScoreScript> ().Score+=20;//实现消失了一组之后再增加分数
MidGemstonelist = new ArrayList ();
StartCoroutine (WaitForCheckMathesAgain ());//重新检测行和列有没有相同的
}
public void Energ()//当技能栏释放完之后进行的动作,改变技能提示按钮的颜色
{
if (EnergButton >= 1.0f) {
GameObject.FindGameObjectWithTag("EnergButton").GetComponent<Image>().color = Color.green;
GameObject.FindGameObjectWithTag("Skill").GetComponent<MeshRenderer>().enabled = true;
GameObject.FindGameObjectWithTag("Skill").GetComponent<SkillAnimation>().enabled = true;
//EnergRelease();
}
}
public void EnergRelease()
{
if (EnergButton >= 1.0f) {
GameObject.FindGameObjectWithTag ("score").GetComponent<ScoreScript> ().Score += 20;//实现消失了一组之后再增加分数
EnergButton = GameObject.FindGameObjectWithTag ("energline").GetComponent<Scrollbar> ().value = 0.0f;
GameObject.FindGameObjectWithTag("EnergButton").GetComponent<Image>().color = Color.white;
GameObject.FindGameObjectWithTag("Skill").GetComponent<MeshRenderer>().enabled = false ;
GameObject.FindGameObjectWithTag("Skill").GetComponent<SkillAnimation>().enabled = false ;
} else {
GameObject.FindGameObjectWithTag ("score").GetComponent<ScoreScript> ().Score += 0;//实现消失了一组之后再增加分数
}
}
public void SecendStart()
{
Application.LoadLevel ("GameStart");
}
IEnumerator WaitForCheckMathesAgain()
{
yield return new WaitForSeconds (0.5f);
if (CheckHorizontalMatches () || CheckVerticalMathces ()) {
RemoveMathes();
}
}
void RemoveGemstone(GemStone c)//实现删除宝石
{
//Debug.Log("删除");
c.DisPose ();
audio.PlayOneShot (mathesClip);
for(int i=c.rowIndex+1;i<rowNum;i++)
{
GemStone tempGemstone = Getgemstone(i,c.clouIndex);
tempGemstone.rowIndex--;
Setgemstone(tempGemstone.rowIndex,tempGemstone.clouIndex,tempGemstone);
//tempGemstone.UpdatePosition(tempGemstone.rowIndex,tempGemstone.clouIndex);
tempGemstone.TweenPOsition(tempGemstone.rowIndex,tempGemstone.clouIndex);
}
GemStone newGemstone = AddGemstone (rowNum,c.clouIndex);
newGemstone.rowIndex--;
Setgemstone (newGemstone.rowIndex,newGemstone.clouIndex,newGemstone);
//newGemstone.UpdatePosition (newGemstone.rowIndex,newGemstone.clouIndex);
newGemstone.TweenPOsition(newGemstone.rowIndex,newGemstone.clouIndex);
}
bool CheckHorizontalMatches()
{
bool isMathces = false;
for (int rowIndex=0; rowIndex<rowNum; rowIndex++) {
for(int coluIndex=0;coluIndex<coluNum-2;coluIndex++)
{
if((Getgemstone(rowIndex,coluIndex).gemstontype==Getgemstone(rowIndex,coluIndex+1).gemstontype)&&(Getgemstone(rowIndex,coluIndex).gemstontype==Getgemstone(rowIndex,coluIndex+2).gemstontype))
{
Debug.Log("发现行相同的宝石");
AddMathes(Getgemstone(rowIndex,coluIndex));
AddMathes(Getgemstone(rowIndex,coluIndex+1));
AddMathes(Getgemstone(rowIndex,coluIndex+2));
isMathces = true;
}
}
}
return isMathces;
}
bool MidRowMathes()//中间位置行的宝石
{
bool isMidMathes = false;
AddMidMathes(Getgemstone(0,0));
AddMidMathes(Getgemstone(0,1));
AddMidMathes(Getgemstone(0,2));
isMidMathes = true;
return isMidMathes;
}
bool MidColuMathes()//中间位置列的宝石
{
bool isMidMathes = false;
AddMidMathes(Getgemstone(2,5));
AddMidMathes(Getgemstone(1,0));
AddMidMathes(Getgemstone(2,0));
isMidMathes =true;
return isMidMathes;
}
bool CheckVerticalMathces()
{
bool isMathces = false;
for(int coluIndex=0; coluIndex<coluNum; coluIndex++) {
for (int rowIndex=0; rowIndex<rowNum-2; rowIndex++)
{
if((Getgemstone(rowIndex,coluIndex).gemstontype==Getgemstone(rowIndex+1,coluIndex).gemstontype)&& (Getgemstone(rowIndex,coluIndex).gemstontype==Getgemstone(rowIndex+2,coluIndex).gemstontype))
{
Debug.Log("发现列相同的宝石");
AddMathes(Getgemstone(rowIndex,coluIndex));
AddMathes(Getgemstone(rowIndex+1,coluIndex));
AddMathes(Getgemstone(rowIndex+2,coluIndex));
isMathces = true;
}
}
}
return isMathces;
}
public GemStone Getgemstone(int rowindex,int coluindex)//通过行号列号取得对应位置的宝石
{
ArrayList temp = gamestonrlist [rowindex] as ArrayList;
GemStone c = temp [coluindex] as GemStone;
return c;
}
public void Setgemstone(int rowindex,int coluindex,GemStone c)//设置所对应行号列号对应的宝石
{
ArrayList temp = gamestonrlist [rowindex] as ArrayList;
temp [coluindex] = c;
}
public void Change(GemStone c1,GemStone c2)//实现交换
{
audio.PlayOneShot (swapclip);
Setgemstone (c1.rowIndex,c1.clouIndex,c2);
Setgemstone (c2.rowIndex,c2.clouIndex,c1);
//交换c1 c2行号
int tempRowIndex;
tempRowIndex = c1.rowIndex;
c1.rowIndex = c2.rowIndex;
c2.rowIndex = tempRowIndex;
int tempcoluINdex;
tempcoluINdex = c1.clouIndex;
c1.clouIndex = c2.clouIndex;
c2.clouIndex = tempcoluINdex;
//c1.UpdatePosition (c1.rowIndex, c1.clouIndex);
c1.TweenPOsition (c1.rowIndex, c1.clouIndex);
//c2.UpdatePosition (c2.rowIndex, c2.clouIndex);
c2.TweenPOsition (c2.rowIndex, c2.clouIndex);
}
}
GameStone.cs的代码 游戏物体的代码
using UnityEngine;
using System.Collections;
public class GemStone : MonoBehaviour {
public float xoffset = -4.8f;
public float yoffset = -2f;
public int rowIndex=0;
public int clouIndex=0;
public GameObject[] gemstonebgs;//宝石数组
public int gemstontype;//宝石类型
private GameObject gemastoneBg;
public SpriteRenderer spriteRenderer;
public bool isSelectd{
set{
if(value){
spriteRenderer.color = Color.red;
}else
{
spriteRenderer.color = Color.white;
}
}
}
public CameController cameController;
// Use this for initialization
void Awake()
{
}
void Start () {
cameController = GameObject.Find ("GameCortoller").GetComponent<CameController> ();
spriteRenderer = gemastoneBg.GetComponent<SpriteRenderer> ();
}
// Update is called once per frame
void Update () {
}
public void UpdatePosition(int _rowIndex,int _coluIndex)//没有特效的下落特效
{
rowIndex = _rowIndex;
clouIndex = _coluIndex;
this.transform.position = new Vector3 (clouIndex+xoffset, rowIndex+yoffset, 0);
}
public void TweenPOsition(int _rowIndex,int _coluIndex)//当消除之后的插件特效
{
rowIndex = _rowIndex;
clouIndex = _coluIndex;
iTween.MoveTo (this.gameObject,iTween.Hash("x",clouIndex+xoffset,"y",rowIndex+yoffset,"time",0.4f));
}
public void RandomCreateGemstone()//生成随机类型的宝石
{
if (gemastoneBg != null) {
return;
}
gemstontype = Random.Range(0, gemstonebgs.Length);
gemastoneBg = Instantiate(gemstonebgs[gemstontype]) as GameObject;
gemastoneBg.transform.parent = this.transform;
}
public void OnMouseDown()//鼠标点击事件
{
cameController.Select (this);
}
public void DisPose()//释放宝石!
{
Destroy (this.gameObject);
Destroy (gemastoneBg.gameObject);
cameController = null;
}
}
Backone.cs的代码 用来支持andorid设备虚拟返回键
using UnityEngine;
using System.Collections;
public class BackOne : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {//android 键盘的虚拟返回键
if(Input.GetKey(KeyCode.Escape)){
Application.LoadLevel("Load");
}
}
}
页面跳转之间的代码EnergButtoncripts.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EnergButtonScript : MonoBehaviour {
//游戏的键值类
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void EnergClick()
{
CameController cameColtroller = new CameController ();
cameColtroller.EnergRelease ();
}
public void Exit()
{
Application.LoadLevel ("Load");
}
public void ButtomRemove()
{
GameObject.FindGameObjectWithTag ("Gameover").GetComponent<RomoveBottom> ().isRomove = true;
}
/// <summary>
///
///
///
/// </summary>
public void SeStart()//重新开始
{
CameController c = new CameController ();
c.SecendStart ();
}
public void Load()
{
Application.LoadLevel ("GameStart");
}
public void Explain()
{
Application.LoadLevel ("My");
}
public void Open()
{
Application.Quit ();
}
public void Back()
{
Application.LoadLevel ("Load");
}
public void Audio()
{
if (GameObject.FindGameObjectWithTag ("AudioText").GetComponent<Text> ().text == "Open") {
PlayerPrefs.SetInt("Index",1);
GameObject.FindGameObjectWithTag ("Audio").GetComponentInChildren<Text> ().text = "Close";
print("1");
}
if (GameObject.FindGameObjectWithTag ("AudioText").GetComponent<Text> ().text == "Close") {
PlayerPrefs.SetInt("Index",0);
GameObject.FindGameObjectWithTag ("AudioText").GetComponent<Text> ().text = "Open";
}
}
}
主界面菜单按钮下落 RomoveButton.cs
using UnityEngine;
using System.Collections;
public class RomoveBottom : MonoBehaviour {
public float RomoveSpeed = 1.5f;
public bool isRomove = false;
//在游戏开始界面进行返回的按钮!
// Use this for initialization
void Start () {
}
//-313
// Update is called once per frame
void Update () {
if (isRomove) {
transform.Translate (-Vector3.up * RomoveSpeed);
if (transform.localPosition.y <= -185) {
RomoveSpeed = 0.0f;
}
}
}
}
Score.cs 分数的脚本
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreScript : MonoBehaviour {
//分数的类
public int Score=0;
public int Experience=0;//关卡
Text s;
Text e;
// Use this for initialization
void Start () {
s=transform.GetComponent<Text> ();
e=transform.GetComponent<Text> ();
}
// Update is called once per frame
void Update () {
s.text = Experience.ToString ();
e.text = Score.ToString ();
}
}
退出游戏的脚本 Screen.cs
using UnityEngine;
using System.Collections;
public class Screen : MonoBehaviour {
// Use this for initialization
void Start () {
}
//退出整个游戏场景
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.Escape)){
Application.Quit();
}
}
}
动画脚本 SkillAnimation.cs
using UnityEngine;
using System.Collections;
public class SkillAnimation : MonoBehaviour {
//当经验条满格的时候,进行技能输出!
int current;
int count;
float timer;
public int fps=10;
Object [] ani;
public string path;
// Use this for initialization
void Start () {
current = 0;
ani = Resources.LoadAll("Image/"+path+"/img");
count = ani.Length;
}
// Update is called once per frame
void Update () {
if (ani != null) {
timer+=Time.deltaTime;
if(timer>=1.0f/fps)
{
timer=0;
current++;
if(current>count-1)
{
current=0;
}
this.renderer.material.mainTexture = (Texture2D)ani[current];
}
}
}
}