Christmas tree jigsaw puzzle game made in unity

Christmas in 2022 is coming, I am very happy that we can spend it together again this time~

I. Introduction

Tip: Use unity to make a jigsaw puzzle, and the picture is the Christmas tree.

2. Creative name

Christmas Tree Jigsaw Puzzle

3. Effect display

The final effect of the Christmas tree jigsaw puzzle.

The effect in the game is as follows:

After the game puzzle is completed, the effect is as follows:

 

 

4. Implementation steps

The first step is to create a new scene. The content of the scene is as follows:

The background image Background is the final target image of the puzzle. Used as a reference for the puzzle.

A script main is bound to the Body. The script is used to generate picture puzzle pieces, and use this as the parent object to generate puzzle piece sub-objects through code.

The picture in the corresponding scene after running is:

 

 Description of the code content of the second step: Bind a script main to the body object, and there is an array of tex all in the code, which is used to place different pictures. That is to say, the game is more than one puzzle, it can be multiple levels, and each level is a picture. The picture can be modified by assigning values ​​to the tex all array.

The following variable _RandomPos is used to set the position of the generated picture puzzle pieces. A large image can randomly generate small images of 6 regions.

The generated style is as follows:

 

The generated child object has been defined in the code, and its name is: tempObj.name = "Item" + k;. But dragging the generated sub-object with the mouse still requires a code.

The child object is the object Plane in the scene. The script added to the object is shown in the figure:

 

 

5. Coding implementation

The details of the main script are as follows:

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;
	}
	
}

The details of the script plane are as follows:

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;
	}
}

In the script main, two buttons are written in GUI, one is the play button to start playing the game, and the other is the button to switch to the next picture. As shown in the picture:

 

Guess you like

Origin blog.csdn.net/alayeshi/article/details/128437638