Search Unity

Assign "Mulitple Sprite Mode" texture to a script for assignment in editor.

Discussion in 'Scripting' started by JimTheSquid, May 22, 2015.

  1. JimTheSquid

    JimTheSquid

    Joined:
    Sep 7, 2014
    Posts:
    2
    I have textures that are set to "Sprite (2D and UI)", with the sprite mode set to "Multiple". I am currently reading them using Resources.LoadAll, and indexing the sprites as required... works fine.

    I have a new directive to remove all resource folders from the project. So now I want a script that manages these sprites. (Note in the future, this will all be changed to getting resources from a server, but I need a stop-gap solution for now.)

    My simplest plan is to add in a value that can be set inside the editor, and get the individual sprites with MySpriteArray[index]. Here's what I've tried so far without success:

    public Sprite[] MySpriteArray;
    Problem: The editor provides an array interface, where I would have to manually drag each and every sub-sprite (which there are many) by hand... not what I want to do.

    public Sprite MySpriteArray;
    Problem: This does allow me to drag my multiple sprite to the script, but I have not been able to successfully recast the value to Sprite[]. This one appears the most hopeful, but I can't figure out how to bind up the fact there is Multi-Sprite information associated with the sprite.

    public Texture2D MySpriteArray;
    public Texture2D[] MySpriteArray;
    Problem: Exact same problems as starting with Sprite or Sprite[].

    So, the question is: How do I assign a "Multi-sprite" to a script via the editor, where I can index into it at run time without having to hand-assign the individual sub-sprites?
     
  2. pixpusher2

    pixpusher2

    Joined:
    Oct 30, 2013
    Posts:
    121
    How bout selecting multiple sub-sprites and dragging them into the Sprite[] array as shown in one of the GIFs? At least it'll save you time compared to dragging them one by one.
    http://imgur.com/a/2w7zd
     
  3. JimTheSquid

    JimTheSquid

    Joined:
    Sep 7, 2014
    Posts:
    2
    Might be a decent stop-gap pixpusher2, thanks.

    I still would be interested in how to assign the "master sprite" to a component and still get to the sub-sprites in code.
     
    antony_morar likes this.