圣诞树拼图游戏unity制作
2022年圣诞节到来啦,很高兴这次我们又能一起度过~
一、前言
提示:使用unity来制作一个拼图游戏,图片便是圣诞树。
二、创意名
圣诞树拼图游戏
三、效果展示
圣诞树拼图游戏最终效果。
游戏中效果如图:
游戏拼图完成后效果如图:
四、实现步骤
第一步,先新建场景,场景中包含内容如下图:
背景图Background是拼图的最终目标图片。用来为拼图作为参照的。
Body上绑定一个脚本main,脚本是用来生成图片拼图碎片的,并且以此为父物体,通过代码生成拼图碎片子物体。
运行后对应场景中的图片是:
第二步代码内容说明:在body这个物体上绑定一个脚本main,在代码中有tex all这个数组,该数组用来放置不同的图片。也就是说游戏不止一个拼图,可以是多个关卡,每个关卡是一张图片。图片可以通过给tex all这个数组赋值来修改。
下面的_RandomPos这个变量是用来设定,生成图片拼图碎片的位置。一张大图片可以随机生成6个区域的小图片。
生成的样式如下图:
生成的子物体在代码中已经定义好了,名字为:tempObj.name = "Item" + k;。但生成的子物体通过鼠标拖动的还需要一个代码。
子物体就是场景中的物体Plane。该物体添加的脚本如图:
五、编码实现
main脚本详情如下:
using UnityEngine; using System.Collections;public class main : MonoBehaviour {public GameObject _plane; public GameObject _planeParent; public GameObject _background; public Texture2D[] _texAll; public Vector3[] _RandomPos; public int row = 3; public int column = 3; public float factor = 0.25f; GameObject[] _tempPlaneAll; float sideLength = 0; int finishCount = 0; int _index = 0;Vector2 originPoint; Vector2 space; void Start(){sideLength = _background.transform.localScale.x;space.x = sideLength / column;space.y = sideLength / row;originPoint.x = -((column - 1) * space.x) / 2;originPoint.y = ((row - 1) * space.y) / 2;Vector2 range;range.x = space.x * factor * 10f;range.y = space.y * factor * 10f;_tempPlaneAll = new GameObject[row * column];int k = 0;for(int i = 0 ; i != row ; ++i){for(int j = 0 ; j != column ; ++j){GameObject tempObj = (GameObject)Instantiate(_plane);tempObj.name = "Item" + k;tempObj.transform.parent = _planeParent.transform;tempObj.transform.localPosition = new Vector3((originPoint.x + space.x * j) * 10f, (originPoint.y - space.y * i) * 10f, 0);tempObj.transform.localScale = new Vector3(space.x, 1f, space.y);Vector2 tempPos = new Vector2(originPoint.x + space.x * j, originPoint.y - space.y * i);float offset_x = (tempPos.x <= 0 + Mathf.Epsilon) ? (0.5f - Mathf.Abs((tempPos.x - space.x / 2) / sideLength)) : (0.5f + (tempPos.x - space.x / 2) / sideLength);float offset_y = (tempPos.y <= 0 + Mathf.Epsilon) ? (0.5f - Mathf.Abs((tempPos.y - space.y / 2) / sideLength)) : (0.5f + (tempPos.y - space.y / 2) / sideLength);float scale_x = Mathf.Abs(space.x / sideLength);float scale_y = Mathf.Abs(space.y / sideLength);tempObj.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(offset_x, offset_y);tempObj.GetComponent<Renderer>().material.mainTextureScale = new Vector2(scale_x, scale_y);tempObj.SendMessage("SetRange", range);_tempPlaneAll[k] = tempObj;++k;}}}void OnGUI(){if(GUI.Button(new Rect(10, 10, 100, 30), "Play"))StartGame();if(GUI.Button(new Rect(10, 80, 100, 30), "Next Textrue"))ChangeTex();}void StartGame(){for(int i = 0 ; i != _tempPlaneAll.Length ; ++i){int tempRank = Random.Range(0, _RandomPos.Length);_tempPlaneAll[i].transform.localPosition = new Vector3(_RandomPos[tempRank].x, _RandomPos[tempRank].y, 0f);}gameObject.BroadcastMessage("Play");}void SetIsMoveFale(){gameObject.BroadcastMessage("IsMoveFalse");}void IsFinish(){++finishCount;if(finishCount == row * column)Debug.Log("Finish!");}void ChangeTex(){_background.GetComponent<Renderer>().material.mainTexture = _texAll[_index];gameObject.BroadcastMessage("SetTexture", _texAll[_index++]);if(_index > _texAll.Length - 1)_index = 0;}}
脚本plane详细如下:
using UnityEngine; using System.Collections;public class plane : MonoBehaviour {Transform mTransform;Vector3 offsetPos; Vector3 finishPos = Vector3.zero; Vector2 range; float z = 0;bool isPlay = false; bool isMove = false; void Start(){mTransform = transform;finishPos = mTransform.localPosition;}void Update(){if(!isPlay)return ;Vector3 tempMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);if(Input.GetMouseButtonDown(0) && tempMousePos.x > GetComponent<Collider>().bounds.min.x && tempMousePos.x < GetComponent<Collider>().bounds.max.x && tempMousePos.y > GetComponent<Collider>().bounds.min.y && tempMousePos.y < GetComponent<Collider>().bounds.max.y){mTransform.parent.SendMessage("SetIsMoveFale");offsetPos = mTransform.position - tempMousePos;z = mTransform.position.z;isMove = true;}if(isMove && Input.GetMouseButton(0)){tempMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);mTransform.position = new Vector3(tempMousePos.x + offsetPos.x, tempMousePos.y + offsetPos.y, z - 0.1f);}if(Input.GetMouseButtonUp(0)){mTransform.position = new Vector3(mTransform.position.x, mTransform.position.y, z);isMove = false;}IsFinish();}void IsFinish(){if(mTransform.localPosition.x > finishPos.x - range.x && mTransform.localPosition.x < finishPos.x + range.x&& mTransform.localPosition.y > finishPos.y - range.y && mTransform.localPosition.y < finishPos.y + range.y){isPlay = false;mTransform.localPosition = finishPos;mTransform.parent.SendMessage("IsFinish");}}void Play(){isPlay = true;}void IsMoveFalse(){isMove = false;}void SetRange(Vector2 _range){range = _range;}void SetTexture(Texture2D _tex){mTransform.GetComponent<Renderer>().material.mainTexture = _tex;} }
在脚本main中用GUI写了两个按钮,一个是开始玩游戏的play按钮,另一个是切换到下一张图片的按钮。如图:
圣诞树拼图游戏unity制作相关推荐
- linux编程拼图游戏,cocos2d-x制作拼图游戏
由于本人最近在学习cocos2d-x,一时手痒,写一个游戏练练手,也是对cocos2d-x进一步的巩固,于是敲了两天,就写了这个拼图游戏,还挺有成就感的,:-),先把我的成果展示如下: 由于cocos ...
- cs6制作拼图游戏 dreamweaver_Dreamweaver制作拼图步骤
1 .处理图片 ①打开 Photoshop 软件 ②打开图片 kdc.jpg ,选择切片工具. ③对图片进行"划分切片" ,水平划分为 2 ,垂直划分为 2 . ④保存为" ...
- 用flash制作简单拼图游戏
简介: 可能有很多玩Flash的朋友都曾和我一样想自己动手制作一个拼图游戏,但是苦于不知道实现的方法或不了解ActionScript(以下简称AS)而心存遗憾.别急,今天盗匪就告诉你如何利用Flash ...
- unity期末作业-拼图游戏
unity期末作业-拼图游戏 unity期末作业-拼图游戏,有22,33,44,55四种模式供选择,有开始按钮,结束提示等等内容,如下图所示: 资源下载链接:https://download.csdn ...
- unity 制作拼图游戏
Unity中material.mainTextureOffset和material.mainTextureScale的作用和用法 mainTextureOffset和mainTextureScale是 ...
- Unity3d制作简单拼图游戏
本文为原创,如需转载请注明原址:http://blog.csdn.net/cube454517408/article/details/7907247 最近一直在使用Unity,对它有了一些小的使用心得 ...
- unity课设小游戏_Unity制作20个迷你小游戏实例训练视频教程
本教程是关于Unity制作20个迷你小游戏实例训练视频教程,时长:20小时,大小:3.8 GB,MP4高清视频格式,教程使用软件:Unity,附源文件,作者:Raja Biswas,共97个章节,语言 ...
- Unity制作2D动作平台游戏视频教程
Metroidvania工具包:打造统一的2D行动平台 流派:电子学习| MP4 |视频:h264,1280×720 |音频:AAC,48.0 KHz 语言:英语+中英文字幕(根据原英文字幕机译更准确 ...
- Unity制作游戏中的场景
Unity制作游戏中的场景 1.2.3 场景 在Unity中,场景(Scene)就是游戏开发者制作游戏时,所使用的游戏场景.它是一个三维空间,对应的三维坐标轴分别是X轴.Y轴和Z轴本文选自Unity ...
最新文章
- WHAT THE DATA SAYS ABOUT KUBERNETES DEPLOYMENT PATTERNS
- html超链接打开共享文件夹,教你如何访问共享文件夹
- git merge 回退_Git 基础学习总结2(学不会你锤我)
- 面向对象思想设计原则
- django外键和多数据库应用
- js+运行+php+文件,php中运行JS
- PSX 610G 使用说明书
- ActiveMQ笔记(一)
- 77个数据科学家常见面试题
- 使用Mixamo_Converter重新定义根骨骼导入UE4
- css实现图片毛玻璃效果
- 【17.12.22.B】
- 今天nba预测分析_NBA情报预测分析_NBA足球俱乐部 - 全球体育网
- python无法打开文件filenotfounderror_解决Python在导入文件时的FileNotFoundError问题
- 如何把几张照片拼在一起?
- 关于HTTP请求出现405状态码 Method not allowed的解决办法
- 给陌生的你听-G.G张思源
- 什么是网关?使用网关有什么好处
- 聚合物钽电容和普通钽电容的区别
- Python14-15