Search Unity

How to divide the sprite into 2 parts from code

Discussion in '2D' started by MrAlter, Nov 27, 2013.

  1. MrAlter

    MrAlter

    Joined:
    May 15, 2013
    Posts:
    9
    Hello,

    I have some sprite and I want to divide it into two parts by randomly line (or polyline) like at picture.

    But without MeshFilter I have no idea how to do it.

    Help me please.

    Sorry for my English and Thanks!

    $divsp.png
     
  2. imurashka

    imurashka

    Joined:
    Aug 5, 2012
    Posts:
    20
    Do you need this in edit mode or runtime?
     
  3. MrAlter

    MrAlter

    Joined:
    May 15, 2013
    Posts:
    9
    Runtime.
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Thats a little bit difficult. You can do square chunks of code at runtime fairly easily, but as for dynamic angled shapes like that, its going to take a home-rolled solution to fit your exact needs for sure. You'd need something that iterates through the whole image, checking a couple points (randomly selected? depending on "sword" slices?), doing some math to get a line between them, and to determine which pixels are on which side of said line, then copying pixels from side A and side B into their own new sprites... so yeah not gonna be easy, lol be sure to share it with us when you wrap that up :p

    I can imagine something like that will turn up on the asset store soon though, with the new 2D tools kinda being so popular.
     
  5. MrAlter

    MrAlter

    Joined:
    May 15, 2013
    Posts:
    9
    I was thinking about copying pixels via GetPixel/SetPixel from Texture2D. But I think it is very difficult for productivity. Or is there a quick way to copy the pixels?


    In the first step, I want to learn how to cut a straight line.
     
  6. popc

    popc

    Joined:
    Nov 13, 2013
    Posts:
    12
    You can use Texture2D.ReadPixels and PolygonCollider2D.SetPath(if you use physics) for that.
     
  7. MrAlter

    MrAlter

    Joined:
    May 15, 2013
    Posts:
    9
    hmm This function gets a portion the screen?
    How can I ReadPixels from some Sprite?
     
  8. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    with get pixels 32 and set pixels 32, that's the fastest way to grab those pixels to work with. add them to a Color array, and iterate through working some magic.