Search Unity

May i get all sprites in a Multiple sprite mode texture?

Discussion in '2D' started by ddtxwd, Jul 21, 2015.

  1. ddtxwd

    ddtxwd

    Joined:
    Oct 29, 2012
    Posts:
    3
    The texture is multiple sprite mode.
    And i can see all the sprites in the project view.
    But i cant find any API to get all the sprites by code!
    Can somebody help me!!!
    Thanks!!
     
  2. Leo-Yaik

    Leo-Yaik

    Unity Technologies

    Joined:
    Aug 13, 2014
    Posts:
    436
    Given the path to the texture, you will need to call

    AssetDatabase.LoadAllAssetsAtPath (http://docs.unity3d.com/ScriptReference/AssetDatabase.LoadAllAssetsAtPath.html)
    and filter the Object[] array by Sprite class.

    Code (CSharp):
    1. Object[] objs = AssetDatabase.LoadAllAssetsAtPath("Assets/myTexture");
    2.  
    3. foreach(Object o in objs)
    4. {
    5.     if(o as Sprite != null)
    6.     { // this is a sprite object
    7.     }
    8. }

    Note that this only works in Editor.
     
    Last edited: Jul 21, 2015
    Dextozz and ddtxwd like this.
  3. ddtxwd

    ddtxwd

    Joined:
    Oct 29, 2012
    Posts:
    3
    You sovle my problem,Thanks!