Search Unity

Get child sprites in a "Multiple" Sprite through drag/drop in inspector

Discussion in 'Scripting' started by mrCharli3, Feb 16, 2018.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    All answers I can find to this questions involve using Resources.LoadAll. Is there no way I can just drag+drop the "parent-sprite" onto my Script and then find it's "child-sprites" through the C# script?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    What you might want to do is look into Unity's new spriteAtlas https://docs.unity3d.com/ScriptReference/U2D.SpriteAtlas.html which allows you to create a spriteatlas and reference that, and pull sprites from it by name.

    I think with a spriteSheet (where you have an image and define multiple "cutouts" within it) requires you to access the texture of the sprite, but I don't know if you can get the individual pieces from that or if you have to define the rect yourself to pull a sprite out.
     
  3. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Thanks but doing that would mean I lose all control of how they are ordered, and since I use them for bitmasking that is not possible (just tried and sprite 32_1 got placed at index 0 in the atlas).

    If this is my only option I guess I'm stuck dragging+dropping all children to the inspector.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Yeah, if that is the case, just use a collection and you should be able to select all sprites and drag and drop them into the collection. It should keep them in the same order (usually does, but sometimes) so you can maintain your index if you need it a certain way.