Unity拼图小游戏
Unity拼图小游戏的实现
目前做了一款拼图小游戏,不规则的图形,*拖拽,网上其他拼图大部分都是基于九宫格的类似华容道拼图模式,我做的时候没有找到我这种类型的相关的项目代码(有一付费下载,然后发现是用3d实现碰撞,然后正交相机来模拟2d),所以现在来记录一下自己的完成思路。
下附一张效果截图。
一、主要难点
1.不规则图形的显示和碰撞
2.拼图自动生成
3.拼图拼合移动
二、难点思路
1.显示和碰撞
我之前是一直卡在不规则图形的显示和碰撞上,找了很多资料,基本要切成上图的样子只有用算法。(这里是一个基于算法实现凹凸拼图切割的博客) 很强大,很正统,正确思路应该就是这么做,但是我不会,所以没有用,以后再啃吧。我的实现思路就很简单,用unity自带的mask遮罩,附一张我的思路图。这里我把完整拼图切成216(18×12)小块,然后分成,3×4,4×3,3×5,5×3的不等小块,再用四种mask,旋转再各自匹配以后,基本就得到了这类拼图游戏的所有拼图模板,如下。
2019.6.20更新
这里我把完整拼图切成216(18×12)小块,然后分成,3×4,4×3,3×5,5×3的4种小块,然后配合14种mask,来得到我这个模式下所有的14种拼图基类,所有mask样式如下。之所以要有这么多mask,是为了后面射线检测的的时候能用统一的代码来控制(旋转后个体的坐标轴会变,目前我无法解决,故用这个笨方法)。
然后为了更加规格化,我加入了插入点和重心点的概念,加入以后分解图如下
白色点是每块拼图的插入点,因为每块拼图是由一个图片组构成的,每次生成需要加载一组sprite依次放入模板的image里。比如左上角第一块3×4,插入点为1的拼图,他要加载的sprite组是:~
1,2,3,
19,20,21,
37,38,39,
55,56,57
这一组sprite,当把加载方法封装好以后,调用只需要传入插入点位置的sprite编号,就可以创建一块完成拼图出来了
2019.6.20更新
第二个版本我换了切图的方法,切完以后子图片的顺序变成了,从下往上,从左往右,
3 6 9
2 5 8
1 4 7
就是上面的结构,于是对插入点做了调整,简单来说就是从左上调整到了左下,比如左下角第一块4×3,插入点为1的拼图,他要加载的sprite组就变成了:
3 15 27 39
2 14 26 38
1 13 25 37
黑色点是每块拼图的重心点,用来挂上碰撞检测的脚本,这样每块拼图之间的距离检测就一样了,也是为了更加的。。简单?随他去吧,我是懒得一个个设置碰撞距离,想到了这个偷懒的方法 = =。然后我设定,只有鼠标选择的拼图块才**碰撞检测脚本,避免bug,碰撞以后把碰撞的两个拼图放到一个父级下面实现拼好一起移动的功能。
最后加入插入点以后,拼图模板有六个,分别是,(拼图大小,插入点)(3×4,1),(3×4,2),(4×3,4),(4×3,1),(5×3,4),(3×5,2)。
2019.6.20更新
重心点概念还是这样,只是调整了代码结构以后,射线检测脚本也挂到了mask物体上,不再额外挂到子图片上。
2.拼图的生成
显示和碰撞搞定了,剩下就是如何自动生成每块拼图了,首先,准备一张图,然后切成216块(18×12),这里用unity自带的处理方法就好,sprite mode 设置成multiple以后打开sprite Editor,设置如下
单个拼图的加载基本就是这样,调用图片模板,加载并根据插入点传入sprite组,给重心点的图片加载碰撞检测脚本,然后加载mask,完成
2019.6.20更新
因为被要求代码动态切图,所以弃用sprite Editor切图,改用SetPixel方法来切图,这个方法网上有大量的实例,这里不再说明,只是说一下这个方法得到的子图片是从左到右,从下往上排列的,和像素点的排列顺序一样
3.拼图拼合移动
2019.6.20更新
这点在之前是没有的,我只是把拼合好的拼图放到一个父级下面实现共同移动,后面遇到了很多bug,于是想了一个思路。
拼图的拼合,现在是每块拼图会记录它上下左右其他拼图和它自身的一个偏移量,如果检测以后是正确的碰撞,就把该拼图的位置设置为当前拼图的位置+这个偏移量。、
现在的拼图从开始到结束全在一个父级下面,每个拼图会记录一个list和一个transform,list里面是所有跟它拼合的拼图,transform是记录的当前鼠标进入的拼图,当拖动的时候,list里所有拼图和transform计算一个偏移量,然后所有拼图的位置就等于鼠标移动的实时位置+这个偏移量,这就实现了一个“虚拟父级”的概念,完成了拼合好的拼图一起移动的功能。
对于拼图旋转,设定一个整型数值用来记录旋转次数,有这么几种0,1,2,3,超过4次就重新从0开始,然后射线检测的时候加个检测双方这个数值是否相等的语句,即可分离不同旋转次数的拼图的碰撞检测。
三、Show me the code
2019.6.20更新
几乎更新了所有代码,现在还是把主要的代码放出来,工程在下面。
1.生成拼图的GameStart 类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameStart : MonoBehaviour
{
//切完的小图
private List<Texture2D> textureList = new List<Texture2D>();
//要切的大图
private Texture2D mCurTexture;
//14种mask
private List<GameObject> maskList = new List<GameObject>();
//拼图生成在jps下,存在jigsawPuzzleList里
private Transform jps;
private List<GameObject> jigsawPuzzleList = new List<GameObject>();
void Start()
{
InIt();
}
//初始化
private void InIt()
{
jps = GameObject.Find("JPs").transform;
//切图
mCurTexture = Resources.Load<Texture2D>("Textures/ow");
textureList = DePackTexture(GameManager.Instance.lieNum * 3, GameManager.Instance.hangNum * 3, GameManager.Instance.SideLength, mCurTexture);
//读取14种mask预制体
for (int i = 0; i < 14; i++)
{
maskList.Add(Resources.Load<GameObject>("Prefabs/Mask/mask" + (i + 1)));
}
CreateJigsawPuzzle(GameManager.Instance.hangNum, GameManager.Instance.lieNum);
//随机位置
for (int i = 0; i < jigsawPuzzleList.Count; i++)
{
int posX = Random.Range(-300, 350);
int posY = Random.Range(600, -200);
jigsawPuzzleList[i].transform.localPosition = new Vector3(posX, posY, 0f);
jigsawPuzzleList[i].AddComponent<SingleJigsawPuzzle>();
}
Debug.Log("拼图生成完毕");
}
//列数,行数,小图的长宽
private List<Texture2D> DePackTexture(int PicColumnCount, int PicRowCount, int SideLength, Texture2D CurTexture)
{
List<Texture2D> newTList = new List<Texture2D>();
for (int i = 0; i < PicColumnCount; ++i)
{
for (int j = 0; j < PicRowCount; ++j)
{
int curX = i * SideLength;
int curY = j * SideLength;
Texture2D newTexture = new Texture2D(SideLength, SideLength);
for (int m = curY; m < curY + SideLength; ++m)
{
for (int n = curX; n < curX + SideLength; ++n)
{
newTexture.SetPixel(n - curX, m - curY, CurTexture.GetPixel(n, m));
}
}
newTexture.Apply();
newTList.Add(newTexture);
}
}
return newTList;
}
//5*3,插入点为4
private void FiveThreeIndex4(int Index, Transform Parent)
{
List<Texture2D> ttList = new List<Texture2D>();
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
ttList.Add(textureList[Index - GameManager.Instance.hangNum * 3 + j + i * GameManager.Instance.hangNum*3]);
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
GameObject go = new GameObject();
go.AddComponent<Image>();
go.transform.SetParent(Parent);
go.transform.localPosition = new Vector3(60f + i * 30f, 30f + j * 30f, 0f);
Image ie = go.GetComponent<Image>();
ie.rectTransform.sizeDelta = new Vector2(30f, 30f);
ie.sprite = Sprite.Create(ttList[i * 3 + j], new Rect(0, 0, ttList[i * 3 + j].width, ttList[i * 3 + j].height), new Vector2(0.5f, 0.5f));
}
}
}
//3*5,插入点为2
private void ThreeFiveIndex2(int Index, Transform Parent)
{
List<Texture2D> ttList = new List<Texture2D>();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
ttList.Add(textureList[Index - 1 + j + i * GameManager.Instance.hangNum*3]);
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
GameObject go = new GameObject();
go.AddComponent<Image>();
go.transform.SetParent(Parent);
go.transform.localPosition = new Vector3(-30f + i * 30f, -60f + j * 30f, 0f);
Image ie = go.GetComponent<Image>();
ie.rectTransform.sizeDelta = new Vector2(30f, 30f);
ie.sprite = Sprite.Create(ttList[i * 5 + j], new Rect(0, 0, ttList[i * 5 + j].width, ttList[i * 5 + j].height), new Vector2(0.5f, 0.5f));
}
}
}
//3*4,插入点为1
private void ThreeFourIndex1(int Index, Transform Parent)
{
List<Texture2D> ttList = new List<Texture2D>();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
ttList.Add(textureList[Index + j + i * GameManager.Instance.hangNum*3]);
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
GameObject go = new GameObject();
go.AddComponent<Image>();
go.transform.SetParent(Parent);
go.transform.localPosition = new Vector3(-60f + i * 30f, -60f + j * 30f, 0f);
Image ie = go.GetComponent<Image>();
ie.rectTransform.sizeDelta = new Vector2(30f, 30f);
ie.sprite = Sprite.Create(ttList[i * 4 + j], new Rect(0, 0, ttList[i * 4 + j].width, ttList[i * 4 + j].height), new Vector2(0.5f, 0.5f));
}
}
}
//3*4,插入点为2
private void ThreeFourIndex2(int Index, Transform Parent)
{
List<Texture2D> ttList = new List<Texture2D>();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
ttList.Add(textureList[Index - 1 + j + i * GameManager.Instance.hangNum*3]);
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
GameObject go = new GameObject();
go.AddComponent<Image>();
go.transform.SetParent(Parent);
go.transform.localPosition = new Vector3(-60f + i * 30f, -60f + j * 30f, 0f);
Image ie = go.GetComponent<Image>();
ie.rectTransform.sizeDelta = new Vector2(30f, 30f);
ie.sprite = Sprite.Create(ttList[i * 4 + j], new Rect(0, 0, ttList[i * 4 + j].width, ttList[i * 4 + j].height), new Vector2(0.5f, 0.5f));
}
}
}
//4*3,插入点为1
private void FourThreeIndex1(int Index, Transform Parent)
{
List<Texture2D> ttList = new List<Texture2D>();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
ttList.Add(textureList[Index + j + i * GameManager.Instance.hangNum*3]);
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
GameObject go = new GameObject();
go.AddComponent<Image>();
go.transform.SetParent(Parent);
go.transform.localPosition = new Vector3(-60f + i * 30f, -30f + j * 30f, 0f);
Image ie = go.GetComponent<Image>();
ie.rectTransform.sizeDelta = new Vector2(30f, 30f);
ie.sprite = Sprite.Create(ttList[i * 3 + j], new Rect(0, 0, ttList[i * 3 + j].width, ttList[i * 3 + j].height), new Vector2(0.5f, 0.5f));
}
}
}
//4*3,插入点为4
private void FourThreeIndex4(int Index, Transform Parent)
{
List<Texture2D> ttList = new List<Texture2D>();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
ttList.Add(textureList[Index - GameManager.Instance.hangNum * 3 + j + i * GameManager.Instance.hangNum*3]);
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
GameObject go = new GameObject();
go.AddComponent<Image>();
go.transform.SetParent(Parent);
go.transform.localPosition = new Vector3(-60f + i * 30f, -30f + j * 30f, 0f);
Image ie = go.GetComponent<Image>();
ie.rectTransform.sizeDelta = new Vector2(30f, 30f);
ie.sprite = Sprite.Create(ttList[i * 3 + j], new Rect(0, 0, ttList[i * 3 + j].width, ttList[i * 3 + j].height), new Vector2(0.5f, 0.5f));
}
}
}
//生成拼图hangNum*lieNum
private void CreateJigsawPuzzle(int hangNum, int lieNum)
{
int index = 0;
int nameNum = 0;
for (int i = 0; i < lieNum; i++)
{
for (int j = 0; j < hangNum; j++)
{
index = j * 3 + i * 9 * hangNum;
nameNum = j + i * hangNum;
//边框
//左下角
if (i == 0 && j == 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[2]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
FourThreeIndex1(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(15f, 0f, 0f);
jigsawPuzzleList.Add(goMask);
}
//左上角
else if (i == 0 && j == hangNum - 1)
{
GameObject goMask = Instantiate<GameObject>(maskList[3]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
ThreeFourIndex2(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(30f, 15f, 0f);
jigsawPuzzleList.Add(goMask);
}
//右下角
else if (i == lieNum - 1 && j == 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[1]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
ThreeFourIndex1(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(30f, 15f, 0f);
jigsawPuzzleList.Add(goMask);
}
//右上角
else if (i == lieNum - 1 && j == hangNum - 1)
{
GameObject goMask = Instantiate<GameObject>(maskList[0]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
FourThreeIndex4(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(15f, 0f, 0f);
jigsawPuzzleList.Add(goMask);
}
//第一列
else if (i == 0)
{
//偶数行
if (j % 2 == 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[9]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
FourThreeIndex1(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(15f, 0f, 0f);
jigsawPuzzleList.Add(goMask);
}
//奇数行
else if (j % 2 != 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[7]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
ThreeFiveIndex2(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(0f, 0f, 0f);
jigsawPuzzleList.Add(goMask);
}
}
//最后一列
else if (i == lieNum - 1)
{
//偶数行
if (j % 2 == 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[6]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
ThreeFiveIndex2(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(0f, 0f, 0f);
jigsawPuzzleList.Add(goMask);
}
//奇数行
else if (j % 2 != 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[8]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
FourThreeIndex4(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(15f, 0f, 0f);
jigsawPuzzleList.Add(goMask);
}
}
//第一行
else if (j == 0)
{
//奇数列
if (i % 2 != 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[10]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
ThreeFourIndex1(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(30f, 15f, 0f);
jigsawPuzzleList.Add(goMask);
}
//偶数列
else if (i % 2 == 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[4]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
FiveThreeIndex4(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(-120f, -60f, 0f);
jigsawPuzzleList.Add(goMask);
}
}
//最后一行
else if (j == hangNum - 1)
{
//奇数列
if (i % 2 != 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[5]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
FiveThreeIndex4(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(-120f, -60f, 0f);
jigsawPuzzleList.Add(goMask);
}
//偶数列
else if (i % 2 == 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[11]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
ThreeFourIndex2(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(30f, 15f, 0f);
jigsawPuzzleList.Add(goMask);
}
}
//内部
//奇数列
else if (i % 2 != 0)
{
//偶数行
if (j % 2 == 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[13]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
ThreeFiveIndex2(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(0f, 0f, 0f);
jigsawPuzzleList.Add(goMask);
}
//奇数行
else if (j % 2 != 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[12]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
FiveThreeIndex4(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(-120f, -60f, 0f);
jigsawPuzzleList.Add(goMask);
}
}
//偶数列
else if (i % 2 == 0)
{
//偶数行
if (j % 2 == 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[12]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
FiveThreeIndex4(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(-120f, -60f, 0f);
jigsawPuzzleList.Add(goMask);
}
//奇数行
else if (j % 2 != 0)
{
GameObject goMask = Instantiate<GameObject>(maskList[13]);
goMask.transform.SetParent(jps);
goMask.name = "JP-" + nameNum;
GameObject go1 = new GameObject();
ThreeFiveIndex2(index, go1.transform);
go1.transform.SetParent(goMask.transform);
go1.transform.localPosition = new Vector3(0f, 0f, 0f);
jigsawPuzzleList.Add(goMask);
}
}
}
}
}
}
2.单个拼图功能SingleJigsawPuzzle类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class SingleJigsawPuzzle : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
{
private bool mouseEnter = false;
private RectTransform curRtTrans;
//拖拽开始时,图片-鼠标的位置差
public Vector3 offsetPos = new Vector3(0, 0, 0);
public int rotateNum=0;
public int maskNum=-1;
private static float distance = 95f;
private int mIndex;
private int hangNum;
private int lieNum;
private int curHang;
private int curLie;
//逻辑父级
public int parentNum;
public List<Transform> jpList = new List<Transform>();
private RectTransform followParent;
//旋转需要
private RectTransform mRotate;
//碰撞开关
public bool isCollision = false;
// Use this for initialization
void Start()
{
InIt();
}
// Update is called once per frame
void Update()
{
RotateAndMove();
JudgeWin();
}
private void LateUpdate()
{
if (isCollision)
{
JudgeCollision();
}
}
private void InIt()
{
hangNum = GameManager.Instance.hangNum;
lieNum = GameManager.Instance.lieNum;
//获得当前拼图的正确序位
mIndex = int.Parse(transform.name.Split('-')[1]);
//得到当前拼图所在行列数
curLie = mIndex / hangNum;
curHang = mIndex % hangNum;
//Debug.Log("" + mIndex + " lie " + curLie + "hang " + curHang);
mRotate = GameObject.Find("Rotate").GetComponent<RectTransform>();
maskNum = int.Parse(gameObject.GetComponent<Image>().sprite.name.Split('k')[1]);
jpList.Add(transform);
parentNum = mIndex;
GameManager.Instance.jpParentDic.Add(transform.name, gameObject);
followParent = gameObject.GetComponent<RectTransform>();
}
//鼠标进入
public void OnPointerEnter(PointerEventData eventData)
{
mouseEnter = true;
foreach (Transform tf in jpList)
{
tf.GetComponent<SingleJigsawPuzzle>().isCollision = true;
}
}
public void OnPointerExit(PointerEventData eventData)
{
mouseEnter = false;
foreach (Transform tf in jpList)
{
tf.GetComponent<SingleJigsawPuzzle>().isCollision = false;
}
}
//拖拽
public void OnBeginDrag(PointerEventData eventData)
{
curRtTrans = GetComponent<RectTransform>();
foreach (Transform tf in jpList)
{
tf.SetAsLastSibling();
tf.GetComponent<SingleJigsawPuzzle>().followParent = transform.GetComponent<RectTransform>();
tf.GetComponent<SingleJigsawPuzzle>().offsetPos = tf.GetComponent<RectTransform>().position - tf.GetComponent<SingleJigsawPuzzle>().followParent.position;
}
offsetPos = curRtTrans.position - Input.mousePosition;
}
public void OnDrag(PointerEventData eventData)
{
if (eventData != null)
{
curRtTrans.position = new Vector3(eventData.position.x, eventData.position.y, 0) + offsetPos;
}
}
public void OnEndDrag(PointerEventData eventData)
{
//Debug.Log("OnEndDrag");
}
//旋转和移动跟随
private void RotateAndMove()
{
if (followParent.name != transform.name)
{
transform.GetComponent<RectTransform>().position = new Vector3(followParent.position.x, followParent.position.y, 0) + offsetPos;
}
if (rotateNum == 4)
{
rotateNum = 0;
}
if (mouseEnter == true)
{
if (Input.GetKeyDown(KeyCode.Space))
{
foreach (Transform tf in jpList)
{
tf.GetComponent<RectTransform>().SetParent(mRotate);
tf.GetComponent<SingleJigsawPuzzle>().followParent = tf.GetComponent<RectTransform>();
}
mRotate.RotateAround(Input.mousePosition, new Vector3(0, 0, 1), 90f);
foreach (Transform tf in jpList)
{
tf.GetComponent<SingleJigsawPuzzle>().rotateNum++;
tf.GetComponent<RectTransform>().SetParent(mRotate.parent);
}
}
}
}
//结束判断
private void JudgeWin()
{
if (jpList.Count == GameManager.Instance.lieNum * GameManager.Instance.hangNum)
{
GameManager.Instance.isWin = true;
}
}
//拼合检测
private void CollisionFromRight(Vector3 Offset,int Dvalue)
{
RaycastHit2D infoRight = Physics2D.Raycast(transform.position, transform.right, distance);
if (infoRight.collider != null && infoRight.transform.name == "JP-" + Dvalue && rotateNum ==infoRight.transform.GetComponent<SingleJigsawPuzzle>().rotateNum)
{
SingleJigsawPuzzle infoSJP = infoRight.transform.GetComponent<SingleJigsawPuzzle>();
if (parentNum != infoSJP.parentNum)
{
if (Input.GetMouseButtonUp(0) && Vector3.Distance(transform.GetComponent<RectTransform>().position, infoRight.transform.GetComponent<RectTransform>().position) < 105f)
{
foreach (Transform tf in jpList)
{
tf.GetComponent<SingleJigsawPuzzle>().followParent = transform.GetComponent<RectTransform>();
tf.GetComponent<SingleJigsawPuzzle>().offsetPos = tf.GetComponent<RectTransform>().position - tf.GetComponent<SingleJigsawPuzzle>().followParent.position;
}
transform.GetComponent<RectTransform>().position = infoRight.transform.GetComponent<RectTransform>().position + Offset;
foreach (Transform tf in jpList)
{
tf.GetComponent<SingleJigsawPuzzle>().parentNum = infoSJP.parentNum;
tf.GetComponent<SingleJigsawPuzzle>().jpList = infoSJP.jpList;
infoSJP.jpList.Add(tf);
}
}
}
else
{
return;
}
}
}
private void CollisionFromLeft(Vector3 Offset,int Dvalue)
{
RaycastHit2D infoLeft = Physics2D.Raycast(transform.position, -transform.right, distance);
if (infoLeft.collider != null && infoLeft.transform.name == "JP-" + Dvalue && rotateNum == infoLeft.transform.GetComponent<SingleJigsawPuzzle>().rotateNum)
{
SingleJigsawPuzzle infoSJP = infoLeft.transform.GetComponent<SingleJigsawPuzzle>();
if (parentNum != infoSJP.parentNum)
{
if (Input.GetMouseButtonUp(0) && Vector3.Distance(transform.GetComponent<RectTransform>().position, infoLeft.transform.GetComponent<RectTransform>().position) < 105f)
{
foreach (Transform tf in jpList)
{
tf.GetComponent<SingleJigsawPuzzle>().followParent = transform.GetComponent<RectTransform>();
tf.GetComponent<SingleJigsawPuzzle>().offsetPos = tf.GetComponent<RectTransform>().position - tf.GetComponent<SingleJigsawPuzzle>().followParent.position;
}
transform.GetComponent<RectTransform>().position = infoLeft.transform.GetComponent<RectTransform>().position + Offset;
foreach (Transform tf in jpList)
{
tf.GetComponent<SingleJigsawPuzzle>().parentNum = infoSJP.parentNum;
tf.GetComponent<SingleJigsawPuzzle>().jpList = infoSJP.jpList;
infoSJP.jpList.Add(tf);
}
}
}
else
{
return;
}
}
}
private void CollisionFromUp(Vector3 Offset,int Dvalue)
{
RaycastHit2D infoUp = Physics2D.Raycast(transform.position, transform.up, distance);
if (infoUp.collider != null && infoUp.transform.name == "JP-" + Dvalue && rotateNum == infoUp.transform.GetComponent<SingleJigsawPuzzle>().rotateNum)
{
SingleJigsawPuzzle infoSJP = infoUp.transform.GetComponent<SingleJigsawPuzzle>();
if (parentNum != infoSJP.parentNum)
{
if (Input.GetMouseButtonUp(0) && Vector3.Distance(transform.GetComponent<RectTransform>().position, infoUp.transform.GetComponent<RectTransform>().position) < 105f)
{
foreach (Transform tf in jpList)
{
tf.GetComponent<SingleJigsawPuzzle>().followParent = transform.GetComponent<RectTransform>();
tf.GetComponent<SingleJigsawPuzzle>().offsetPos = tf.GetComponent<RectTransform>().position - tf.GetComponent<SingleJigsawPuzzle>().followParent.position;
}
transform.GetComponent<RectTransform>().position = infoUp.transform.GetComponent<RectTransform>().position + Offset;
foreach (Transform tf in jpList)
{
tf.GetComponent<SingleJigsawPuzzle>().parentNum = infoSJP.parentNum;
tf.GetComponent<SingleJigsawPuzzle>().jpList = infoSJP.jpList;
infoSJP.jpList.Add(tf);
}
}
}
else
{
return;
}
}
}
private void CollisionFromDown(Vector3 Offset, int Dvalue)
{
RaycastHit2D infoDown = Physics2D.Raycast(transform.position, -transform.up, distance);
if (infoDown.collider != null && infoDown.transform.name == "JP-" + Dvalue && rotateNum == infoDown.transform.GetComponent<SingleJigsawPuzzle>().rotateNum)
{
SingleJigsawPuzzle infoSJP = infoDown.transform.GetComponent<SingleJigsawPuzzle>();
if (parentNum != infoSJP.parentNum)
{
if (Input.GetMouseButtonUp(0) && Vector3.Distance(transform.GetComponent<RectTransform>().position, infoDown.transform.GetComponent<RectTransform>().position) < 105f)
{
foreach (Transform tf in jpList)
{
tf.GetComponent<SingleJigsawPuzzle>().followParent = transform.GetComponent<RectTransform>();
tf.GetComponent<SingleJigsawPuzzle>().offsetPos = tf.GetComponent<RectTransform>().position - tf.GetComponent<SingleJigsawPuzzle>().followParent.position;
}
transform.GetComponent<RectTransform>().position = infoDown.transform.GetComponent<RectTransform>().position + Offset;
foreach (Transform tf in jpList)
{
tf.GetComponent<SingleJigsawPuzzle>().parentNum = infoSJP.parentNum;
tf.GetComponent<SingleJigsawPuzzle>().jpList = infoSJP.jpList;
infoSJP.jpList.Add(tf);
}
}
}
else
{
return;
}
}
}
private void JudgeCollision()
{
switch (maskNum)
{
case 0:
if (rotateNum == 0)
{
CollisionFromLeft(new Vector3(75f,15f,0f),mIndex-hangNum);
CollisionFromDown(new Vector3(-15f,90f,0f),mIndex-1);
}
else if (rotateNum == 1)
{
CollisionFromLeft(new Vector3(-15f,75f,0f), mIndex - hangNum);
CollisionFromDown(new Vector3(-90f,-15f,0f), mIndex - 1);
}
else if (rotateNum == 2)
{
CollisionFromLeft(new Vector3(-75f, -15f, 0f), mIndex - hangNum);
CollisionFromDown(new Vector3(15f, -90f, 0f), mIndex - 1);
}
else if (rotateNum == 3)
{
CollisionFromLeft(new Vector3(15f, -75f, 0f), mIndex - hangNum);
CollisionFromDown(new Vector3(90f, 15f, 0f), mIndex - 1);
}
break;
case 1:
if (rotateNum == 0)
{
CollisionFromUp(new Vector3(15f,-75f,0f),mIndex+1);
CollisionFromLeft(new Vector3(90f,15f,0f),mIndex-hangNum);
}
else if (rotateNum == 1)
{
CollisionFromUp(new Vector3(75f,15f,0f),mIndex+1);
CollisionFromLeft(new Vector3(-15f,90f,0f),mIndex-hangNum);
}
else if (rotateNum == 2)
{
CollisionFromUp(new Vector3(-15f, 75f, 0f), mIndex + 1);
CollisionFromLeft(new Vector3(-90f, -15f, 0f), mIndex - hangNum);
}
else if (rotateNum == 3)
{
CollisionFromUp(new Vector3(-75f, -15f, 0f), mIndex + 1);
CollisionFromLeft(new Vector3(15f, -90f, 0f), mIndex - hangNum);
}
break;
case 2:
if (rotateNum == 0)
{
CollisionFromUp(new Vector3(15f, -90f, 0f), mIndex + 1);
CollisionFromRight(new Vector3(-75f, -15f, 0f), mIndex + hangNum);
}
else if (rotateNum == 1)
{
CollisionFromUp(new Vector3(90f, 15f, 0f), mIndex + 1);
CollisionFromRight(new Vector3(15f, -75f, 0f), mIndex + hangNum);
}
else if (rotateNum == 2)
{
CollisionFromUp(new Vector3(-15f, 90f, 0f), mIndex + 1);
CollisionFromRight(new Vector3(75f, 15f, 0f), mIndex + hangNum);
}
else if (rotateNum == 3)
{
CollisionFromUp(new Vector3(-90f, -15f, 0f), mIndex + 1);
CollisionFromRight(new Vector3(-15f, 75f, 0f), mIndex + hangNum);
}
break;
case 3:
if (rotateNum == 0)
{
CollisionFromRight(new Vector3(-90f, -15f, 0f), mIndex + hangNum);
CollisionFromDown(new Vector3(-15f, 75f, 0f), mIndex - 1);
}
else if (rotateNum == 1)
{
CollisionFromRight(new Vector3(15f, -90f, 0f), mIndex + hangNum);
CollisionFromDown(new Vector3(-75f, -15f, 0f), mIndex - 1);
}
else if (rotateNum == 2)
{
CollisionFromRight(new Vector3(90f, 15f, 0f), mIndex + hangNum);
CollisionFromDown(new Vector3(15f, -75f, 0f), mIndex - 1);
}
else if (rotateNum == 3)
{
CollisionFromRight(new Vector3(-15f, 90f, 0f), mIndex + hangNum);
CollisionFromDown(new Vector3(75f, 15f, 0f), mIndex - 1);
}
break;
case 4:
if (rotateNum == 0)
{
CollisionFromRight(new Vector3(-90f, 0f, 0f), mIndex + hangNum);
CollisionFromUp(new Vector3(-15f, -90f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(-15f, 90f, 0f), mIndex - 1);
}
else if (rotateNum == 1)
{
CollisionFromRight(new Vector3(0f,-90f,0f),mIndex+hangNum);
CollisionFromUp(new Vector3(90f,-15f,0f),mIndex+1);
CollisionFromDown(new Vector3(-90f,-15f,0f),mIndex-1);
}
else if (rotateNum == 2)
{
CollisionFromRight(new Vector3(90f, 0f, 0f), mIndex + hangNum);
CollisionFromUp(new Vector3(15f, 90f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(15f, -90f, 0f), mIndex - 1);
}
else if (rotateNum == 3)
{
CollisionFromRight(new Vector3(0f, 90f, 0f), mIndex + hangNum);
CollisionFromUp(new Vector3(-90f, 15f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(90f, 15f, 0f), mIndex - 1);
}
break;
case 5:
if (rotateNum == 0)
{
if (curLie == 1)
{
CollisionFromLeft(new Vector3(75f,15f,0f),mIndex-hangNum);
}
else
{
CollisionFromLeft(new Vector3(90f, 15f, 0f), mIndex - hangNum);
}
CollisionFromRight(new Vector3(-90f, 15f, 0f), mIndex + hangNum);
CollisionFromUp(new Vector3(0f, -75f, 0f), mIndex + 1);
}
else if (rotateNum == 1)
{
if (curLie == 1)
{
CollisionFromLeft(new Vector3(-15f, 75f, 0f), mIndex - hangNum);
}
else
{
CollisionFromLeft(new Vector3(-15f, 90f, 0f), mIndex - hangNum);
}
CollisionFromRight(new Vector3(-15f,-90f,0f),mIndex+hangNum);
CollisionFromUp(new Vector3(75f,0f,0f),mIndex+1);
}
else if (rotateNum == 2)
{
if (curLie == 1)
{
CollisionFromLeft(new Vector3(-75f, -15f, 0f), mIndex - hangNum);
}
else
{
CollisionFromLeft(new Vector3(-90f, -15f, 0f), mIndex - hangNum);
}
CollisionFromRight(new Vector3(90f, -15f, 0f), mIndex + hangNum);
CollisionFromUp(new Vector3(0f, 75f, 0f), mIndex + 1);
}
else if (rotateNum == 3)
{
if (curLie == 1)
{
CollisionFromLeft(new Vector3(15f, -75f, 0f), mIndex - hangNum);
}
else
{
CollisionFromLeft(new Vector3(15f, -90f, 0f), mIndex - hangNum);
}
CollisionFromRight(new Vector3(15f, 90f, 0f), mIndex + hangNum);
CollisionFromUp(new Vector3(-75f, 0f, 0f), mIndex + 1);
}
break;
case 6:
if (rotateNum == 0)
{
if (curHang == hangNum - 2)
{
CollisionFromUp(new Vector3(15f,-75f,0f),mIndex+1);
}
else
{
CollisionFromUp(new Vector3(15f, -90f, 0f), mIndex + 1);
}
CollisionFromDown(new Vector3(15f, 90f, 0f), mIndex - 1);
CollisionFromRight(new Vector3(-75f, 0f, 0f), mIndex + hangNum);
}
else if (rotateNum == 1)
{
if (curHang == hangNum - 2)
{
CollisionFromUp(new Vector3(75f, 15f, 0f), mIndex + 1);
}
else
{
CollisionFromUp(new Vector3(90f, 15f, 0f), mIndex + 1);
}
CollisionFromDown(new Vector3(-90f,15f,0f),mIndex-1);
CollisionFromRight(new Vector3(0f,-75f,0f),mIndex+hangNum);
}
else if (rotateNum == 2)
{
if (curHang == hangNum - 2)
{
CollisionFromUp(new Vector3(-15f, 75f, 0f), mIndex + 1);
}
else
{
CollisionFromUp(new Vector3(-15f, 90f, 0f), mIndex + 1);
}
CollisionFromDown(new Vector3(-15f, -90f, 0f), mIndex - 1);
CollisionFromRight(new Vector3(75f, 0f, 0f), mIndex + hangNum);
}
else if (rotateNum == 3)
{
if (curHang == hangNum - 2)
{
CollisionFromUp(new Vector3(-75f, -15f, 0f), mIndex + 1);
}
else
{
CollisionFromUp(new Vector3(-90f, -15f, 0f), mIndex + 1);
}
CollisionFromDown(new Vector3(90f, -15f, 0f), mIndex - 1);
CollisionFromRight(new Vector3(0f, 75f, 0f), mIndex + hangNum);
}
break;
case 7:
if (rotateNum == 0)
{
if (curLie == lieNum - 2)
{
CollisionFromRight(new Vector3(-75f,-15f,0f),mIndex+hangNum);
}
else
{
CollisionFromRight(new Vector3(-90f, -15f, 0f), mIndex + hangNum);
}
CollisionFromDown(new Vector3(0f, 75f, 0f), mIndex - 1);
CollisionFromLeft(new Vector3(90f, -15f, 0f), mIndex - hangNum);
}
else if (rotateNum == 1)
{
if (curLie == lieNum - 2)
{
CollisionFromRight(new Vector3(15f, -75f, 0f), mIndex + hangNum);
}
else
{
CollisionFromRight(new Vector3(15f, -90f, 0f), mIndex + hangNum);
}
CollisionFromDown(new Vector3(-75f,0f,0f),mIndex-1);
CollisionFromLeft(new Vector3(15f,90f,0f),mIndex-hangNum);
}
else if (rotateNum == 2)
{
if (curLie == lieNum - 2)
{
CollisionFromRight(new Vector3(75f, 15f, 0f), mIndex + hangNum);
}
else
{
CollisionFromRight(new Vector3(90f, 15f, 0f), mIndex + hangNum);
}
CollisionFromDown(new Vector3(0f, -75f, 0f), mIndex - 1);
CollisionFromLeft(new Vector3(-90f, 15f, 0f), mIndex - hangNum);
}
else if (rotateNum == 3)
{
if (curLie == lieNum - 2)
{
CollisionFromRight(new Vector3(-15f, 75f, 0f), mIndex + hangNum);
}
else
{
CollisionFromRight(new Vector3(-15f, 90f, 0f), mIndex + hangNum);
}
CollisionFromDown(new Vector3(75f, 0f, 0f), mIndex - 1);
CollisionFromLeft(new Vector3(-15f, -90f, 0f), mIndex - hangNum);
}
break;
case 8:
if (rotateNum == 0)
{
CollisionFromLeft(new Vector3(90f,0f,0f),mIndex-hangNum);
CollisionFromUp(new Vector3(15f,-90f,0f),mIndex+1);
CollisionFromDown(new Vector3(15f,90f,0f),mIndex-1);
}
else if (rotateNum == 1)
{
CollisionFromDown(new Vector3(-90f,15f,0f),mIndex-1);
CollisionFromUp(new Vector3(90f,15f,0f),mIndex+1);
CollisionFromLeft(new Vector3(0f,90f,0f),mIndex-hangNum);
}
else if (rotateNum == 2)
{
CollisionFromDown(new Vector3(-15f, -90f, 0f), mIndex - 1);
CollisionFromUp(new Vector3(-15f, 90f, 0f), mIndex + 1);
CollisionFromLeft(new Vector3(-90f, 0f, 0f), mIndex - hangNum);
}
else if (rotateNum == 3)
{
CollisionFromDown(new Vector3(90f, -15f, 0f), mIndex - 1);
CollisionFromUp(new Vector3(-90f, -15f, 0f), mIndex + 1);
CollisionFromLeft(new Vector3(0f, -90f, 0f), mIndex - hangNum);
}
break;
case 9:
if (rotateNum == 0)
{
CollisionFromLeft(new Vector3(90f,-15f,0f),mIndex-hangNum);
CollisionFromRight(new Vector3(-90f,-15f,0f),mIndex+hangNum);
CollisionFromUp(new Vector3(0f,-90f,0f),mIndex+1);
}
else if (rotateNum == 1)
{
CollisionFromRight(new Vector3(15f,-90f,0f),mIndex+hangNum);
CollisionFromLeft(new Vector3(15f,90f,0f),mIndex-hangNum);
CollisionFromUp(new Vector3(90f,0f,0f),mIndex+1);
}
else if (rotateNum == 2)
{
CollisionFromRight(new Vector3(90f, 15f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(-90f, 15f, 0f), mIndex - hangNum);
CollisionFromUp(new Vector3(0f, 90f, 0f), mIndex + 1);
}
else if (rotateNum == 3)
{
CollisionFromRight(new Vector3(-15f, 90f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(-15f, -90f, 0f), mIndex - hangNum);
CollisionFromUp(new Vector3(-90f, 0f, 0f), mIndex + 1);
}
break;
case 10:
if (rotateNum == 0)
{
CollisionFromLeft(new Vector3(90f,15f,0f),mIndex-hangNum);
CollisionFromRight(new Vector3(-90f,15f,0f),mIndex+hangNum);
CollisionFromDown(new Vector3(0f,90f,0f),mIndex-1);
}
else if (rotateNum == 1)
{
CollisionFromRight(new Vector3(-15f,-90f,0f),mIndex+hangNum);
CollisionFromLeft(new Vector3(-15f,90f,0f),mIndex-hangNum);
CollisionFromDown(new Vector3(-90f,0f,0f),mIndex-1);
}
else if (rotateNum == 2)
{
CollisionFromLeft(new Vector3(-90f, -15f, 0f), mIndex - hangNum);
CollisionFromRight(new Vector3(90f, -15f, 0f), mIndex + hangNum);
CollisionFromDown(new Vector3(0f, -90f, 0f), mIndex - 1);
}
else if (rotateNum == 3)
{
CollisionFromLeft(new Vector3(15f, -90f, 0f), mIndex - hangNum);
CollisionFromRight(new Vector3(15f, 90f, 0f), mIndex + hangNum);
CollisionFromDown(new Vector3(90f, 0f, 0f), mIndex - 1);
}
break;
case 11:
if (rotateNum == 0)
{
if (curLie == 1)
{
CollisionFromLeft(new Vector3(75f, 0f, 0f), mIndex - hangNum);
CollisionFromRight(new Vector3(-90f, 0f, 0f), mIndex + hangNum);
}
else if (curLie == lieNum - 2)
{
CollisionFromRight(new Vector3(-75f, 0f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(90f, 0f, 0f), mIndex - hangNum);
}
else
{
CollisionFromRight(new Vector3(-90f, 0f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(90f, 0f, 0f), mIndex - hangNum);
}
CollisionFromUp(new Vector3(0f,-90f,0f),mIndex+1);
CollisionFromDown(new Vector3(0f,90f,0f),mIndex-1);
}
else if (rotateNum == 1)
{
if (curLie == 1)
{
CollisionFromRight(new Vector3(0f, -90f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(0f, 75f, 0f), mIndex - hangNum);
}
else if (curLie == lieNum - 2)
{
CollisionFromRight(new Vector3(0f, -75f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(0f, 90f, 0f), mIndex - hangNum);
}
else
{
CollisionFromRight(new Vector3(0f, -90f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(0f,90f, 0f), mIndex - hangNum);
}
CollisionFromUp(new Vector3(90f,0f,0f),mIndex+1);
CollisionFromDown(new Vector3(-90f,0f,0f),mIndex-1);
}
else if (rotateNum == 2)
{
if (curLie == 1)
{
CollisionFromRight(new Vector3(90f, 0f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(-90f, 0f, 0f), mIndex - hangNum);
}
else if (curLie == lieNum - 2)
{
CollisionFromRight(new Vector3(75f, 0f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(-90f, 0f, 0f), mIndex - hangNum);
}
else
{
CollisionFromRight(new Vector3(90f, 0f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(-90f, 0f, 0f), mIndex - hangNum);
}
CollisionFromUp(new Vector3(0f, 90f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(0f, -90f, 0f), mIndex - 1);
}
else if (rotateNum == 3)
{
if (curLie == 1)
{
CollisionFromRight(new Vector3(0f, 90f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(0f, -75f, 0f), mIndex - hangNum);
}
else if (curLie == lieNum - 2)
{
CollisionFromRight(new Vector3(0f, 75f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(0f, -90f, 0f), mIndex - hangNum);
}
else
{
CollisionFromRight(new Vector3(0f, 90f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(0f, -90f, 0f), mIndex - hangNum);
}
CollisionFromUp(new Vector3(-90f, 0f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(90f, 0f, 0f), mIndex - 1);
}
break;
case 12:
if (rotateNum == 0)
{
if (curHang == 1)
{
CollisionFromUp(new Vector3(0f,-90f,0f),mIndex+1);
CollisionFromDown(new Vector3(0f,75f,0f),mIndex-1);
}
else if (curHang == hangNum - 2)
{
CollisionFromUp(new Vector3(0f,-75f,0f),mIndex+1);
CollisionFromDown(new Vector3(0f,90f,0f),mIndex-1);
}
else
{
CollisionFromUp(new Vector3(0f, -90f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(0f, 90f, 0f), mIndex - 1);
}
CollisionFromLeft(new Vector3(90f,0f,0f),mIndex-hangNum);
CollisionFromRight(new Vector3(-90f,0f,0f),mIndex+hangNum);
}
else if (rotateNum == 1)
{
if (curHang == 1)
{
CollisionFromUp(new Vector3(90f, 0f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(-75f, 0f, 0f), mIndex - 1);
}
else if (curHang == hangNum - 2)
{
CollisionFromUp(new Vector3(75f, 0f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(-90f, 0f, 0f), mIndex - 1);
}
else
{
CollisionFromUp(new Vector3(90f, 0f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(-90f, 0f, 0f), mIndex - 1);
}
CollisionFromRight(new Vector3(0f,-90f,0f),mIndex+hangNum);
CollisionFromLeft(new Vector3(0f,90f,0f),mIndex-hangNum);
}
else if (rotateNum == 2)
{
if (curHang == 1)
{
CollisionFromUp(new Vector3(0f, 90f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(0f, -75f, 0f), mIndex - 1);
}
else if (curHang == hangNum - 2)
{
CollisionFromUp(new Vector3(0f, 75f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(0f, -90f, 0f), mIndex - 1);
}
else
{
CollisionFromUp(new Vector3(0f, 90f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(0f, -90f, 0f), mIndex - 1);
}
CollisionFromRight(new Vector3(90f, 0f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(-90f,0f, 0f), mIndex - hangNum);
}
else if (rotateNum == 3)
{
if (curHang == 1)
{
CollisionFromUp(new Vector3(-90f, 0f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(75f, 0f, 0f), mIndex - 1);
}
else if (curHang == hangNum - 2)
{
CollisionFromUp(new Vector3(-75f, 0f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(90f, 0f, 0f), mIndex - 1);
}
else
{
CollisionFromUp(new Vector3(-90f, 0f, 0f), mIndex + 1);
CollisionFromDown(new Vector3(90f, 0f, 0f), mIndex - 1);
}
CollisionFromRight(new Vector3(0f, 90f, 0f), mIndex + hangNum);
CollisionFromLeft(new Vector3(0f, -90f, 0f), mIndex - hangNum);
}
break;
case 13:
if (rotateNum == 0)
{
if (curHang == 1)
{
CollisionFromDown(new Vector3(-15f,75f,0f),mIndex-1);
}
else
{
CollisionFromDown(new Vector3(-15f, 90f, 0f), mIndex - 1);
}
CollisionFromUp(new Vector3(-15f, -90f, 0f), mIndex + 1);
CollisionFromLeft(new Vector3(75f,0f,0f),mIndex-hangNum);
}
else if (rotateNum == 1)
{
if (curHang == 1)
{
CollisionFromDown(new Vector3(-75f, -15f, 0f), mIndex - 1);
}
else
{
CollisionFromDown(new Vector3(-90f, -15f, 0f), mIndex - 1);
}
CollisionFromUp(new Vector3(90f,-15f,0f),mIndex+1);
CollisionFromLeft(new Vector3(0f,75f,0f),mIndex-hangNum);
}
else if (rotateNum == 2)
{
if (curHang == 1)
{
CollisionFromDown(new Vector3(15f, -75f, 0f), mIndex - 1);
}
else
{
CollisionFromDown(new Vector3(15f, -90f, 0f), mIndex - 1);
}
CollisionFromUp(new Vector3(15f, 90f, 0f), mIndex + 1);
CollisionFromLeft(new Vector3(-75f, 0f, 0f), mIndex - hangNum);
}
else if (rotateNum == 3)
{
if (curHang == 1)
{
CollisionFromDown(new Vector3(75f, 15f, 0f), mIndex - 1);
}
else
{
CollisionFromDown(new Vector3(90f, 15f, 0f), mIndex - 1);
}
CollisionFromUp(new Vector3(-90f, 15f, 0f), mIndex + 1);
CollisionFromLeft(new Vector3(0f, -75f, 0f), mIndex - hangNum);
}
break;
default:
Debug.Log("maskNum is err");
break;
}
}
}
4.游戏管理GameManager类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : Singleton<GameManager>
{
//lieNum=m,hangNum=n
public int hangNum = 4;
public int lieNum = 6;
public bool isWin = false;
public Dictionary<string, GameObject> jpParentDic;
public int SideLength;
public void InIt()
{
jpParentDic = new Dictionary<string, GameObject>();
}
}
5.扩展功能GamingUI类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GamingUI : UIBase
{
private Button help;
private Button shadow;
private Button showFrame;
private Button showAll;
private Button pingpu;
private Text showTime;
private GameObject shadowPic;
private List<GameObject> jpList = new List<GameObject>();
//计时器部分
private int second = 0;
private int minute = 0;
private int hour = 0;
private string s;
private string m;
private string h;
private float time;
private GameObject uibg;
// Use this for initialization
void Start()
{
InIt();
}
// Update is called once per frame
void Update()
{
Timer();
IsWin();
}
private void InIt()
{
showTime = GameObject.Find("time").GetComponent<Text>();
shadowPic = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Textures/shadowImage"));
shadowPic.transform.SetParent(GameObject.Find("UIRoot").transform);
shadowPic.transform.localPosition = new Vector3(40, 0, 0);
shadowPic.transform.SetAsFirstSibling();
uibg = GameObject.Find("UIbg");
uibg.transform.SetAsFirstSibling();
shadowPic.GetComponent<RectTransform>().sizeDelta = new Vector2(30 * GameManager.Instance.lieNum * 3,
30 * GameManager.Instance.hangNum * 3);
shadowPic.SetActive(false);
help = GameObject.Find("help").GetComponent<Button>();
shadow = GameObject.Find("shadow").GetComponent<Button>();
showFrame = GameObject.Find("showFrame").GetComponent<Button>();
showAll = GameObject.Find("showAll").GetComponent<Button>();
showAll.gameObject.SetActive(false);
pingpu = GameObject.Find("pingpu").GetComponent<Button>();
help.onClick.AddListener(OnHelpClicked);
shadow.onClick.AddListener(OnShadowClicked);
showFrame.onClick.AddListener(OnShowFrameClicked);
showAll.onClick.AddListener(OnShowAllClicked);
pingpu.onClick.AddListener(OnPingpuClicked);
for (int i = 0; i < GameManager.Instance.hangNum * GameManager.Instance.lieNum; i++)
{
jpList.Add(GameObject.Find("JP-" + i));
}
}
private void OnHelpClicked()
{
Debug.Log("没得帮助,这就一凑数的");
}
//显示影子图
private void OnShadowClicked()
{
if (shadowPic.activeSelf == false)
{
shadowPic.SetActive(true);
}
else
{
shadowPic.SetActive(false);
}
}
//只显示边框的拼图,隐藏内部
private void OnShowFrameClicked()
{
showAll.gameObject.SetActive(true);
showFrame.gameObject.SetActive(false);
for (int i = 0; i < jpList.Count; i++)
{
string[] names = jpList[i].transform.name.Split('-');
int jpIndex = int.Parse(names[1]);
if (jpIndex % GameManager.Instance.hangNum != 0 && jpIndex % GameManager.Instance.hangNum != GameManager.Instance.hangNum - 1
&& jpIndex > GameManager.Instance.hangNum && jpIndex < GameManager.Instance.hangNum * (GameManager.Instance.lieNum - 1))
{
jpList[jpIndex].SetActive(false);
}
}
}
//全部显示
private void OnShowAllClicked()
{
showAll.gameObject.SetActive(false);
showFrame.gameObject.SetActive(true);
foreach (GameObject go in jpList)
{
if (go.activeSelf == false)
{
go.SetActive(true);
}
}
}
//平铺拼图,每个的位置随机
private void OnPingpuClicked()
{
int posX = -200;
int posY = 500;
List<int> indexList = new List<int>();
for (int i = 0; i < jpList.Count; i++)
{
indexList.Add(i);
}
for (int i = 0; i < jpList.Count; i++)
{
int index = Random.Range(0, indexList.Count);
jpList[indexList[index]].transform.localPosition = new Vector3(posX, posY, 0);
indexList.RemoveAt(index);
posX += 110;
if ((i + 1) % GameManager.Instance.lieNum == 0 && i != 0)
{
posY -= 130;
posX = -210;
}
}
}
//计时器
private void Timer()
{
time += Time.deltaTime;
if (time >= 1)
{
second++;
time -= 1;
}
if (second == 60)
{
minute++;
second = 0;
}
if (minute == 60)
{
hour++;
minute = 0;
}
if (second < 10)
{
s = "0" + second;
}
else
{
s = "" + second;
}
if (minute < 10)
{
m = "0" + minute;
}
else
{
m = "" + minute;
}
if (hour < 10)
{
h = "0" + hour;
}
else
{
h = "" + hour;
}
showTime.text = h + ":" + m + ":" + s;
}
//结束判定
private void IsWin()
{
if (GameManager.Instance.isWin == true)
{
UIManager.Instance.Close("GamingUI");
shadowPic.SetActive(true);
shadowPic.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
for (int i = 0; i < jpList.Count; i++)
{
Destroy(jpList[i]);
}
Debug.Log("拼图完成");
}
}
}
四、工程包
1.0 https://download.csdn.net/download/weixin_44927626/11223891
2019.6.20更新
2.0 https://download.csdn.net/download/weixin_44927626/11250296
第一版的工程包已经改为私密,现在放出第二版的工程压缩包下载,屏幕设为1920×1080即可,基本应该是最后一次更新了,因为,我考核过关了~哈哈哈哈
捧场的可以投币下一下,主体代码基本都在上面了,不下问题也不大。
上一篇: Qt实现3D饼图
下一篇: Vue实现拼图小游戏