Before I start I just want to say I’m sorry if this is the wrong place to post this.
I’m busy with a jigsaw puzzle game on unity and I’m a bit stuck. I have a puzzle piece prefab and then a socket prefab. The socket prefab is placed in a grid layout group. I have resized the puzzle pieces so that they automatically fit the screen size available. What I’m struggling to do is actually getting the cell size of the layout group to be the same size as the puzzle piece. If you look at line 68, thats my latest attempt.
Could someone maybe point me in the right direction? Thanks in advance
Here’s my code (its still a work in progress so excuse the mess):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Puzzle : MonoBehaviour
{
[SerializeField] private GameObject piecePrefab; // the default prefab of the piece (used to place in socket)
[SerializeField] private GameObject socketPrefab; // where the puzzle piece must be placed
[SerializeField] private GameObject puzzleTable; // wooden piece where puzzle pieces will be placed
[SerializeField] private GameObject puzzleSocketTable; // location where the puzzles will be assembled
[SerializeField] private Texture2D completePuzzle; //picture of the finished puzzle
[SerializeField] private int puzzleSize; //number of puzzle pieces
[SerializeField] private int ratioY; //puzzle x to y ratio, y portion
[SerializeField] private int ratioX; //puzzle x to y ratio, x portion
Sprite sprite;
int cellCountX; // number of pieces in the x Direction
int cellCountY; // number of pieces in the x Direction
float cellLengthX; // x size of the of the single puzzle piece
float cellLengthY; // y size of the of the single puzzle piece
void Awake()
{
int row = 0; // variable used to count the columns of the sprite (starts bottom left)
int column = 0; // variable used to count the rows of the sprite (starts bottom left)
Rect location; //rect variable that represents what portion of sprite will be shown in puzzle piece
float puzzleRatio = (float)completePuzzle.height / (float)completePuzzle.width; //used to make sure aspect ratio of original picture is maintained
if (puzzleSize % ratioX == 0 && puzzleSize % ratioY == 0) //making sure that a puzzle can be made with the selected sizes
{
cellCountX = puzzleSize / ratioX; //number of pieces in x direction
cellCountY = puzzleSize / ratioY; //number of pieces in the y direction
/*
I know the x and y doesn't make sense but it works so leave it
*/
cellLengthX = completePuzzle.height / cellCountX; //length of puzzle piece in x direction
cellLengthY = completePuzzle.width / cellCountY; //length of puzzle piece in y direction
}else
{
Debug.Log("Invalid option");
}
for (int i = 0; i < puzzleSize; i++)//iterating through puzzle pieces
{
location = new Rect( //adjusting rect for new puzzle piece (remember this is for selecting which part of picture piece is)
cellLengthY * column, cellLengthX * row, //Rect(y_origin, x_origin, y_length, x_length)
cellLengthY, cellLengthX);
sprite = Sprite.Create(completePuzzle, location, new Vector2(0.5f, 0.5f)); //creating sprite for puzzle piece being instantiated in this iteration
GameObject piece = Instantiate(piecePrefab,transform.parent); //instantiating puzzle piece
piece.GetComponent<RectTransform>().anchorMax = new Vector2(1.0f / ratioX, 1.0f / ratioX * puzzleRatio); //resizing piece and keeping aspect ratio
piece.GetComponent<RectTransform>().anchorMin = new Vector2(0f, 0f);
piece.GetComponent<RectTransform>().sizeDelta = new Vector2(0,0); //setting anchors to the edge of the sprite
piece.name = "C" + column + "r" + row; //renaming sprite
piece.GetComponent<Image>().sprite = sprite; //setting image for sprite from the image cropped
GameObject socket = Instantiate(socketPrefab, puzzleSocketTable.transform); //instantiating socket for puzzle piece
puzzleSocketTable.GetComponent<GridLayoutGroup>().cellSize = new Vector2(piece.GetComponent<RectTransform>().rect.width, piece.GetComponent<RectTransform>().rect.height);
// socket.GetComponent<RectTransform>().sizeDelta =
// new Vector2(
// puzzleSocketTable.GetComponent<RectTransform>().rect.width,
// puzzleSocketTable.GetComponent<RectTransform>().rect.height);
socket.name = piece.name; //renaming socket to same as piece to make finding match easier
//keeping track of column and row count
if (column >= cellCountY - 1)
{
row++;
column = 0;
} else
{
column++;
}
}
}
}