Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Drag and drop to grid

Discussion in 'Scripting' started by Ivan_Kondratev, Jan 20, 2021.

  1. Ivan_Kondratev

    Ivan_Kondratev

    Joined:
    Apr 15, 2019
    Posts:
    5
    Hello everybody!
    Can't implement drag and drop to grid. I know how to do drag and drop without grid, I also know how to drag 1 cell item. Dragging objects on a grid, the size of which, for example, 2 cells, leads me into confusion. I don't understand how to make a drop for several cells at once. I would be grateful for your help! An example of what I want to achieve in a GIF.
    BlueStacks_Recording_2021.01.20_16.35.59.45.gif
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,766
    Definitely start with some drag and drop inventory tutorial videos on Youtube for this. There are plenty of them with just 1x1 simple squares, but there are also more complex ones that have multi-cell objects.
     
    Ivan_Kondratev likes this.
  3. Ivan_Kondratev

    Ivan_Kondratev

    Joined:
    Apr 15, 2019
    Posts:
    5
    Honestly, I could not find a single video that tells how to drag and drop a multicellular object. I can implement drag and drop for a single-celled object. Can I form the request on youtube incorrectly?
    :confused:
    Or maybe you've seen similar topics on the forum that might help me?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,766
    I know I've seen at least one but I have no idea where or when.

    The idea is simple enough to implement:

    With 1x1 cells, at some point during dragging you make a decision: "Can I drag this here?"

    Extract all the information going into that question into an API that says "Given a 1x1 shape here, can I fit?"

    Once you have done that, then add an extra argument saying the dimensions. "Given a 1x3 shape here, can I fit?"

    Iterate.
     
    Ivan_Kondratev likes this.
  5. Ivan_Kondratev

    Ivan_Kondratev

    Joined:
    Apr 15, 2019
    Posts:
    5
    Well, I'll try to build on what you wrote. Thanks for the tip, I didn't think I would get feedback so quickly. In Unity community we trust)
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,766
    You're welcome! Just work methodically and break it apart, so that you really are saying "Hey can I put this 1x1 here?" with a bunch of arguments. When you code like that, it can be easier to extend functionality to say "Hey can you fit this SHAPE here?"
     
    Ivan_Kondratev likes this.