Eonirr

Untitled

Apr 11th, 2025 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. public struct SlotRect
  2. {
  3. public Vector2 bottomLeft;
  4. public Vector2 topRight;
  5.  
  6. public SlotRect(Vector2 bl, Vector2 tr)
  7. {
  8. bottomLeft = bl;
  9. topRight = tr;
  10. }
  11.  
  12. public bool Contains(Vector2 point)
  13. {
  14. return point.x >= bottomLeft.x && point.x <= topRight.x &&
  15. point.y >= bottomLeft.y && point.y <= topRight.y;
  16. }
  17. }
  18.  
  19. public int nbSlotsX;
  20. public int nbSlotsY;
  21. public float totalSizeX;
  22. public float totalSizeY;
  23.  
  24. public float slotSizeX;
  25. public float slotSizeY;
  26. private SlotRect[,] slotGrid;
  27.  
  28. private void NoMoellonageGrid()
  29. {
  30. slotGrid = new SlotRect[nbSlotsX, nbSlotsY];
  31. float centerX = totalSizeX / 2f;
  32. float centerY = totalSizeY / 2f;
  33.  
  34. for (int x = 0; x < nbSlotsX; x++)
  35. {
  36. for (int y = 0; y < nbSlotsY; y++)
  37. {
  38. float startX = -centerX + x * slotSizeX;
  39. float endX = startX + slotSizeX;
  40.  
  41. float startY = -centerY + y * slotSizeY;
  42. float endY = startY + slotSizeY;
  43.  
  44. slotGrid[x, y] = new SlotRect(new Vector2(startX, startY), new Vector2(endX, endY));
  45. }
  46. }
  47. }
  48.  
  49. public bool TryGetSlotIndex(Vector2 localPoint, out Vector2Int slotIndex)
  50. {
  51. for (int x = 0; x < nbSlotsX; x++)
  52. {
  53. for (int y = 0; y < nbSlotsY; y++)
  54. {
  55. if (slotGrid[x, y].Contains(localPoint))
  56. {
  57. slotIndex = new Vector2Int(x, y);
  58. return true;
  59. }
  60. }
  61. }
  62.  
  63. slotIndex = new Vector2Int(-1, -1);
  64. return false;
  65. }
  66.  
  67. // et t'use genre Grid.TryGetSlotIndex(); avec RectTransformUtility.ScreenPointToLocalPointInRectangle (comme sur la Doc oui oui Kappa ) pour récup' la pos' du cursor, puis out le slot
Advertisement
Add Comment
Please, Sign In to add comment