Advertisement
DugganSC

PuzzleScript.cs

Jul 12th, 2024
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEditor.Rendering;
  5. using UnityEngine;
  6. using UnityEngine.Assertions;
  7. using UnityEngine.UI;
  8.  
  9. [RequireComponent(typeof(RectTransform))]
  10. public class PuzzleScript : MonoBehaviour
  11. {
  12.     [SerializeField] GameObject _puzzleSlot;
  13.     [SerializeField] Sprite _puzzleSprite;
  14.  
  15.     RectTransform _puzzleRectTransform;
  16.  
  17.     private void Start()
  18.     {
  19.         _puzzleRectTransform = GetComponent<RectTransform>();
  20.         SegmentAndPlace(_puzzleSprite, 2, 2);
  21.     }
  22.  
  23.     public void SegmentAndPlace(Sprite puzzleImage, int x, int y)
  24.     {
  25.         Transform parent = transform;
  26.         Texture2D texture = puzzleImage.texture;
  27.  
  28.         Vector3 position = _puzzleRectTransform.localPosition;
  29.         Vector3 dimensions = _puzzleRectTransform.sizeDelta;
  30.  
  31.         float sliceX = texture.width / x;
  32.         float sliceY = texture.height / y;
  33.  
  34.         for (int h = 0; h < x; h++)
  35.         {
  36.             for (int v = 0; v < y; v++)
  37.             {
  38.                 GameObject newGameObject = new GameObject();
  39.                 newGameObject.name = $"Slice {h} {v}";
  40.                 newGameObject.transform.parent = parent;
  41.                 Image image = newGameObject.AddComponent<Image>();
  42.                 DraggablePuzzlePiece piece = newGameObject.AddComponent<DraggablePuzzlePiece>();
  43.                 piece.SetHV(h, v);
  44.  
  45.                 Rect rect = new Rect(h * sliceX, v * sliceY, sliceX, sliceY);
  46.  
  47.                 Sprite newSprite = Sprite.Create(texture, rect, Vector2.one / 2f);
  48.                 newSprite.name = $"Sprite {h} {v}";
  49.                 image.sprite = newSprite;
  50.                
  51.                 RectTransform rectTransform = newGameObject.GetComponent<RectTransform>();
  52.                 rectTransform.localPosition = position + new Vector3(Random.Range(0, dimensions.x), Random.Range(0, dimensions.y), 0);
  53.                 rectTransform.sizeDelta = new Vector3(dimensions.x / x, dimensions.y / y, 1);
  54.  
  55.                 GameObject newPuzzleSlot = Instantiate(_puzzleSlot);
  56.                 newPuzzleSlot.name = $"Slot {h} {v}";
  57.                 newPuzzleSlot.transform.parent = parent;
  58.                
  59.                 PuzzleSlot slot = newPuzzleSlot.AddComponent<PuzzleSlot>();
  60.                 slot.SetHV(h, v);
  61.  
  62.                 rectTransform = newPuzzleSlot.GetComponent<RectTransform>();
  63.                 rectTransform.localPosition = position + new Vector3((h-0.5f) * dimensions.x / x, (v - 0.5f) * dimensions.y / y, 0);
  64.                 rectTransform.sizeDelta = new Vector3(dimensions.x / x, dimensions.y / y, 1);
  65.             }
  66.         }
  67.     }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement