using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; public class GameControl : MonoBehaviour { //资源加载路径
public string spritePath = "Sprites/Teriri"; //拼图碎片
public Sprite[] sprites = null; //拼图控制(以按钮的形式)
public Button[] buttons = null; //携带网格方块自动布局组件的背景
public Transform bgTransform = null; //碎片预制体
public GameObject puzzlePrefab = null; //用于记录空图片的位置
public RectTransform nullImage = null; //用于获取布局方块的大小,便于调节碎片
private GridLayoutGroup bgLayout = null; //用于记录正确的图片名字相加顺序
private string correctSpritesNameOrder = null; //用于确认按钮化的图片碎片是否正确
private string spritesNameOrder = null; private Sprite remainSprite = null;//用于保存被置空的图片,最后游戏结束时现实出来 //此函数用于初始化
private void Start() { string theLastSpriteName = null; //资源加载
this.sprites = Resources.LoadAll<Sprite>(this.spritePath);
this.bgLayout = bgTransform.GetComponent<GridLayoutGroup>(); List<Sprite> tempSprites = new List<Sprite>(); //记录正确的精灵名字顺序
for (int i = ; i < this.sprites.Length; i++) {
tempSprites.Add(this.sprites[i]);
this.correctSpritesNameOrder += this.sprites[i].name;
//记录下最后一张碎片的名字
if (i == this.sprites.Length - ) {
theLastSpriteName = this.sprites[i].name;
}
} print("正确的图片名字构成顺序:" + this.correctSpritesNameOrder.ToString()); int spritesLength = this.sprites.Length;
//Debug.Log(spritesLength);
for (int i = spritesLength - ; i >= ; i--) {
//实例化预制体
GameObject btnInstance = Instantiate(this.puzzlePrefab) as GameObject;
//获取按钮组件
Button btn = btnInstance.GetComponent<Button>(); GameControl self = this;//要保存一下当前的this对象,以免出现this指向错误
btn.onClick.AddListener(delegate () {
self.BtnOnclick(btnInstance.GetComponent<RectTransform>());
}); //随机取图片出来放置,达到打乱的效果
int randomNumber = Random.Range(, tempSprites.Count);
btnInstance.name = tempSprites[randomNumber].name; Image btnInstanceImage = btnInstance.GetComponent<Image>();
if (btnInstance.name == theLastSpriteName) {
//置空,否则打乱
this.remainSprite = tempSprites[randomNumber];
btnInstanceImage.sprite = null;
this.nullImage = btnInstance.GetComponent<RectTransform>();
} else {
//btnInstance.GetComponent<Image>().sprite = tempSprites[randomNumber];
btnInstanceImage.sprite = tempSprites[randomNumber];
} //Debug.Log(btnInstanceImage); tempSprites.Remove(tempSprites[randomNumber]);
btnInstance.transform.SetParent(bgTransform);
btnInstance.GetComponent<RectTransform>().localScale = Vector3.one;
//Debug.Log(randomNumber + ":" + btnInstance.name);
}
this.buttons = this.transform.Find("BackGround").GetComponentsInChildren<Button>(); ////随机放置一个空图片位置
//int tempNumber = Random.Range(0, this.buttons.Length);
//this.remainSprite = buttons[tempNumber].GetComponent<Image>().sprite;//保存被置空的图片
//this.buttons[tempNumber].GetComponent<Image>().sprite = null;
//this.nullImage = this.buttons[tempNumber].GetComponent<RectTransform>(); //设置最右下角的图片为消失的图片
} //拼图按钮点击事件
private void BtnOnclick(RectTransform btnRect) {
if (Vector2.Distance(btnRect.anchoredPosition, this.nullImage.anchoredPosition)
<= (this.bgLayout.cellSize.x + this.bgLayout.spacing.x)) {
//print("产生替换");
//Sprite btnRectSprite = btnRect.GetComponent<Image>().sprite;//获取需要替换的引用
////这个空位应该是啥也没有
//Sprite nullImageSprite = this.nullImage.GetComponent<Image>().sprite;
Sprite cacheSprite = btnRect.GetComponent<Image>().sprite;//交换前缓存一下图片
string cacheString = btnRect.gameObject.name; btnRect.gameObject.name = this.nullImage.gameObject.name;
this.nullImage.gameObject.name = cacheString; btnRect.GetComponent<Image>().sprite = this.nullImage.GetComponent<Image>().sprite;
this.nullImage.GetComponent<Image>().sprite = cacheSprite;
//this.nullImage.GetComponent<Image>().sprite = cacheSprite;
this.nullImage = btnRect; CheckAll();
}
} //遍历所有图片名字顺序,判断是否完成拼图,结束游戏
private void CheckAll() {
this.spritesNameOrder = "";
this.buttons = this.bgTransform.GetComponentsInChildren<Button>();
for (int i = ; i < this.buttons.Length; i++) {
this.spritesNameOrder += this.buttons[i].gameObject.name;
} if (this.spritesNameOrder == this.correctSpritesNameOrder) {
this.nullImage.GetComponent<Image>().sprite = this.remainSprite;//现实被置空的图片
Debug.Log("德丽莎天下第一可爱!");
this.enabled = false;//停止游戏刷新
}
}
}

结束:

Unity初步 基本拼图实现的更多相关文章

  1. ARToolKit for Unity环境搭建(初步搭建成功)

    最近一段时间才开始学习使用Unity3d,AR的学习使用中,先后使用了metaio SDK.vuforia SDK,但由于这两个都属于收费的,今天开始尝试使用ARToolKit.先将ARToolKit ...

  2. Unity 芯片拼图算法

    很多游戏的养成系统中会有利用芯片或者碎片来合成特定道具的功能,或者来给玩家以额外的属性提升等,先截个图以便更好说明: 如上图,我们有各种各样形状迥异的碎片,上面只不过列举了其中一部分,现在,我们需要利 ...

  3. Unity3D学习笔记3——Unity Shader的初步使用

    目录 1. 概述 2. 详论 2.1. 创建材质 2.2. 着色器 2.2.1. 名称 2.2.2. 属性 2.2.3. SubShader 2.2.3.1. 标签(Tags) 2.2.3.2. 渲染 ...

  4. Unity V3 初步使用 —— 为我的.NET项目从简单三层架构转到IOC做准备

    [声明]由于本人表达能力有限,为避免不必要的误人子弟,本文将不会涉及IOC与DI,仅仅描述新版本Unity 3的使用(非Unity 3D,如果您想看的是Unity 3D请立即离开,否则莫怪此处“谋财害 ...

  5. Egret3D初步笔记二 (Unity导出场景使用)

    一 Scene 根据上一节的继续.在导入unity4.7.1_Egret3D_Dll.unitypackage后. 在Unity中双击打开Assets/Egret3D/Example下的Example ...

  6. Unity中数据的存储与交互的初步分析(PlayerPrefs,Dictionary,JsonUnility)

    1.PlayerPrefs   PlayerPrefs.SetString(key,Value);  PlayerPrefs.GetString(key,Value);字符串类型 PlayerPref ...

  7. Unity3D游戏开发初探—2.初步了解3D模型基础

    一.什么是3D模型? 1.1 3D模型概述 简而言之,3D模型就是三维的.立体的模型,D是英文Dimensions的缩写. 3D模型也可以说是用3Ds MAX建造的立体模型,包括各种建筑.人物.植被. ...

  8. [Unity3D]Unity+Android交互教程——让手机"动"起来

    想要用Unity实现一个二维码扫描的功能,然后网上找插件,找到一个貌似叫EasyCodeScanner,但下载下来用用,真不好使,一导入运行就报错,调好错了再运行发现点按钮没反应,反复试了几遍发现还是 ...

  9. Unity学习疑问记录之触屏

    当将Unity游戏运行到ios或android设备上时,桌面系统中的鼠标左键操作可以自动变为手机屏幕上的触屏操作,但鼠标操作无法实现一些特有的触屏操作,比如多点触屏. 触控对于Android移动设备来 ...

随机推荐

  1. 【经典数据结构】B树与B+树的解释

    本文转载自:http://www.cnblogs.com/yangecnu/p/Introduce-B-Tree-and-B-Plus-Tree.html 前面讲解了平衡查找树中的2-3树以及其实现红 ...

  2. Ubuntu安装MySQL密码初始化问题

    在Ubuntu上使用sudo apt-get install mysql-server mysql-common 命令安装MySQL以后,安装过程中没有提示输入密码.然后使用mysql -u root ...

  3. Windows 下 Hbuilder 真机调试(Android,iphone)

    概述:主要讲讲自己在使用 HBuilder 真机调试功能时遇到的问题,以及如何解决.Android 相对没有遇到什么大问题,在电脑安装如360手机助手就可以正常使用了,主要问题是在 iphone 上( ...

  4. AppBoxFuture: Sql存储的ORM查询示例

      上篇介绍集成第三方Sql数据库时未实现如导航属性.子查询等功能,经过大半个月的努力作者初步实现了这些功能,基本上能满足80%-90%查询需求,特别复杂的查询可以用原生sql来处理,下面分别示例介绍 ...

  5. vue实现数据遍历、多个倒计时列表

    移动端效果图: 1.HTML显示代码: <template> <div class="activeList"> <div class="li ...

  6. 【struts 报错】 No action config found for the specified url

    1 type Exception report message org.apache.struts.chain.commands.InvalidPathException: No action con ...

  7. GCD: 求两数最大公因数算法【欧几里得法】原理的个人理解 (80%图片讲解!)

    那么,求 a,b 的最大公因数就是求最大的,能均分a,b的块!  

  8. ProtoBuf试用与JSON的比较

    介绍 ProtoBuf 是google团队开发的用于高效存储和读取结构化数据的工具.什么是结构化数据呢,正如字面上表达的,就是带有一定结构的数据.比如电话簿上有很多记录数据,每条记录包含姓名.ID.邮 ...

  9. Bugku - CTF加密篇之聪明的小羊(一只小羊翻过了2个栅栏)

    聪明的小羊 一只小羊翻过了2个栅栏 KYsd3js2E{a2jda}  

  10. ETCD监控

    Watch key changes Applications can watch on a key or a range of keys to monitor for any updates. Her ...