Unity初步 基本拼图实现
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 6 public class GameControl : MonoBehaviour { 7 8 //资源加载路径 9 public string spritePath = "Sprites/Teriri"; 10 11 //拼图碎片 12 public Sprite[] sprites = null; 13 14 //拼图控制(以按钮的形式) 15 public Button[] buttons = null; 16 17 //携带网格方块自动布局组件的背景 18 public Transform bgTransform = null; 19 20 //碎片预制体 21 public GameObject puzzlePrefab = null; 22 23 //用于记录空图片的位置 24 public RectTransform nullImage = null; 25 26 //用于获取布局方块的大小,便于调节碎片 27 private GridLayoutGroup bgLayout = null; 28 29 //用于记录正确的图片名字相加顺序 30 private string correctSpritesNameOrder = null; 31 32 //用于确认按钮化的图片碎片是否正确 33 private string spritesNameOrder = null; 34 35 private Sprite remainSprite = null;//用于保存被置空的图片,最后游戏结束时现实出来 36 37 38 39 //此函数用于初始化 40 private void Start() { 41 42 string theLastSpriteName = null; 43 44 //资源加载 45 this.sprites = Resources.LoadAll<Sprite>(this.spritePath); 46 this.bgLayout = bgTransform.GetComponent<GridLayoutGroup>(); 47 48 List<Sprite> tempSprites = new List<Sprite>(); 49 50 //记录正确的精灵名字顺序 51 for (int i = 0; i < this.sprites.Length; i++) { 52 tempSprites.Add(this.sprites[i]); 53 this.correctSpritesNameOrder += this.sprites[i].name; 54 //记录下最后一张碎片的名字 55 if (i == this.sprites.Length - 1) { 56 theLastSpriteName = this.sprites[i].name; 57 } 58 } 59 60 print("正确的图片名字构成顺序:" + this.correctSpritesNameOrder.ToString()); 61 62 int spritesLength = this.sprites.Length; 63 //Debug.Log(spritesLength); 64 for (int i = spritesLength - 1; i >= 0; i--) { 65 //实例化预制体 66 GameObject btnInstance = Instantiate(this.puzzlePrefab) as GameObject; 67 //获取按钮组件 68 Button btn = btnInstance.GetComponent<Button>(); 69 70 GameControl self = this;//要保存一下当前的this对象,以免出现this指向错误 71 btn.onClick.AddListener(delegate () { 72 self.BtnOnclick(btnInstance.GetComponent<RectTransform>()); 73 }); 74 75 //随机取图片出来放置,达到打乱的效果 76 int randomNumber = Random.Range(0, tempSprites.Count); 77 btnInstance.name = tempSprites[randomNumber].name; 78 79 Image btnInstanceImage = btnInstance.GetComponent<Image>(); 80 if (btnInstance.name == theLastSpriteName) { 81 //置空,否则打乱 82 this.remainSprite = tempSprites[randomNumber]; 83 btnInstanceImage.sprite = null; 84 this.nullImage = btnInstance.GetComponent<RectTransform>(); 85 } else { 86 //btnInstance.GetComponent<Image>().sprite = tempSprites[randomNumber]; 87 btnInstanceImage.sprite = tempSprites[randomNumber]; 88 } 89 90 //Debug.Log(btnInstanceImage); 91 92 tempSprites.Remove(tempSprites[randomNumber]); 93 btnInstance.transform.SetParent(bgTransform); 94 btnInstance.GetComponent<RectTransform>().localScale = Vector3.one; 95 //Debug.Log(randomNumber + ":" + btnInstance.name); 96 } 97 this.buttons = this.transform.Find("BackGround").GetComponentsInChildren<Button>(); 98 99 ////随机放置一个空图片位置 100 //int tempNumber = Random.Range(0, this.buttons.Length); 101 //this.remainSprite = buttons[tempNumber].GetComponent<Image>().sprite;//保存被置空的图片 102 //this.buttons[tempNumber].GetComponent<Image>().sprite = null; 103 //this.nullImage = this.buttons[tempNumber].GetComponent<RectTransform>(); 104 105 //设置最右下角的图片为消失的图片 106 } 107 108 //拼图按钮点击事件 109 private void BtnOnclick(RectTransform btnRect) { 110 if (Vector2.Distance(btnRect.anchoredPosition, this.nullImage.anchoredPosition) 111 <= (this.bgLayout.cellSize.x + this.bgLayout.spacing.x)) { 112 //print("产生替换"); 113 //Sprite btnRectSprite = btnRect.GetComponent<Image>().sprite;//获取需要替换的引用 114 ////这个空位应该是啥也没有 115 //Sprite nullImageSprite = this.nullImage.GetComponent<Image>().sprite; 116 Sprite cacheSprite = btnRect.GetComponent<Image>().sprite;//交换前缓存一下图片 117 string cacheString = btnRect.gameObject.name; 118 119 btnRect.gameObject.name = this.nullImage.gameObject.name; 120 this.nullImage.gameObject.name = cacheString; 121 122 btnRect.GetComponent<Image>().sprite = this.nullImage.GetComponent<Image>().sprite; 123 this.nullImage.GetComponent<Image>().sprite = cacheSprite; 124 //this.nullImage.GetComponent<Image>().sprite = cacheSprite; 125 this.nullImage = btnRect; 126 127 CheckAll(); 128 } 129 } 130 131 //遍历所有图片名字顺序,判断是否完成拼图,结束游戏 132 private void CheckAll() { 133 this.spritesNameOrder = ""; 134 this.buttons = this.bgTransform.GetComponentsInChildren<Button>(); 135 for (int i = 0; i < this.buttons.Length; i++) { 136 this.spritesNameOrder += this.buttons[i].gameObject.name; 137 } 138 139 if (this.spritesNameOrder == this.correctSpritesNameOrder) { 140 this.nullImage.GetComponent<Image>().sprite = this.remainSprite;//现实被置空的图片 141 Debug.Log("德丽莎天下第一可爱!"); 142 this.enabled = false;//停止游戏刷新 143 } 144 } 145 }
结束:
转载于:https://www.cnblogs.com/steamedbun/p/10833102.html
Unity初步 基本拼图实现相关推荐
- unity期末作业-拼图游戏
unity期末作业-拼图游戏 unity期末作业-拼图游戏,有22,33,44,55四种模式供选择,有开始按钮,结束提示等等内容,如下图所示: 资源下载链接:https://download.csdn ...
- Unity小游戏(一)——Unity JigsawPuzzle(拼图游戏)
1.前言: 简单的Unity小游戏,切割图片,生成随机区块,拖拽交换位置. 此游戏代码只贴了一小部分,具体可见GitHub:源码 效果: 2.素材准备 简单做了下,所以没有用太多素材,只准备了两种字体 ...
- unity实现动态拼图的思路总结
主要总结动态拼图的其中2个关键点 1.如何实现一个边缘不是锯齿的遮罩 2.如何实现对动态内容的遮罩 针对第一点,unity原生提供的sprite mask是用模板缓冲测试来实现的遮罩,当遮罩图某个像素 ...
- Unity 自学成长路线总结
因为自己学了一段时间的Unity,因为对游戏一点兴趣也没有,可以算是被迫学习了,做了几个小游戏熟悉了一下,正式喜欢的是它在汽车hmi所做的宣传,因为我本身从事了设计工作,,之前对于汽车HMI中控设计出 ...
- jdk 11 模块系统_JDK 9:模块系统状态的重点
jdk 11 模块系统 马克·雷因霍尔德 ( Mark Reinhold )的"模块系统状态 (SOMS)"已于本月初发布,它提供了信息丰富的可读性"对Jigsaw项目中 ...
- JDK 9:模块系统状态的重点
马克·雷因霍尔德 ( Mark Reinhold )的"模块系统状态 (SOMS)"已于本月初发布,它提供了信息丰富的可读性"对项目Jigsaw中原型的Java SE平台 ...
- win7高级启动选项:_使您的手机游戏成功。 第二部分:高级选项
win7高级启动选项: In the latest in our series of blog posts bringing you insights from Unity users that ha ...
- unity 制作拼图游戏
Unity中material.mainTextureOffset和material.mainTextureScale的作用和用法 mainTextureOffset和mainTextureScale是 ...
- 圣诞树拼图游戏unity制作
2022年圣诞节到来啦,很高兴这次我们又能一起度过~ 一.前言 提示:使用unity来制作一个拼图游戏,图片便是圣诞树. 二.创意名 圣诞树拼图游戏 三.效果展示 圣诞树拼图游戏最终效果. 游戏中效果 ...
最新文章
- 继承和多态 2.0 -- 继承的六个默认成员函数
- eclipse failed to create the java virtual machine 问题图文解析
- 第16届东北赛区线上比赛斯赛点时间安排+直播链接
- Training—Capturing Photos
- LINK:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- 【下班后学Android】Android开发环境搭建
- STM32 网络通信Web Server中 SSI与CGI的应用解析
- 服务器漏洞文件被删除漏洞,【华中科技大学 - 漏洞预警】Wordpress = 4.9.6 任意文件删除漏洞...
- HDOJ-1019 Least Common Multiple
- 相机标准之onvif---开放型网络视频接口论坛onvif 简介
- 【错误分析】NX error status: 32
- [Web Chart系列之四] 图形布局-Layout 之js设计实现
- PHP 面向对象的简单介绍
- Missing session attribute 'user' of type List 解决办法
- 独立站电商广告和营销洞察
- 记录一次VM14虚拟机出问题 Oh no,something has gone wrong!
- 锐浪报表 Grid++Report 图像打印
- oc 之中的 汉字字符串转化成为拼音 汉字字符串的排序
- 推荐9款便签云生成工具
- c语言 大小写 islower,C 库函数 islower() 使用方法及示例