Unity 4.3 - 2D, how to assign programmatically sprites to an object

后端 未结 3 1447
感情败类
感情败类 2021-02-13 18:37

I\'m trying to create an object that will be responsible of creating and showing different sprites, so I would like to access directly the assets/sprites programmatically instea

相关标签:
3条回答
  • 2021-02-13 19:16

    Here is real code.. will help you

    void SetSprite(UI2DSprite uiSprite, Sprite[] sprites, string strKey)
     {
         foreach (Sprite stexture in sprites)
         {
             if (stexture.name == strKey)
             {
                 uiSprite.sprite2D = stexture;
                 break;
             }
         }
     }
    

    use it following the way..

    UI2DSprite[] uiSprites = tmpObject.GetComponentsInChildren<UI2DSprite>();
     Sprite[] sprites = Resources.LoadAll<Sprite>("Textures");
     string resName = "icon_favorite_2";
    
     foreach (UI2DSprite uiSprite in uiSprites)
     {
     if(uiSprite.name = "icon")
      SetSprite(uiSprite, sprites , resName);
    
     }
    
    0 讨论(0)
  • 2021-02-13 19:17

    Just to throw my tuppence worth in. I had this problem when I changed a SerializedField from Texture2D to Sprite in the code. The texture was already a sprite in the editor but it needed to be Sprite in the code. The field in the editor initially said type mismatch and then displayed the sprite texture again. To cure it I set the field in the editor to none and the reset it to the required sprite.

    0 讨论(0)
  • 2021-02-13 19:31

    To create a sprite programmatically might be a little too difficult to do. You'd probably need to create a Texture in memory, fill in the data, and then write that texture on the disk. After that, you should be able to read that using C#'s File, or Unity's WWW.LoadFromCacheOrDownload.

    Here is how you would create a texture dynamically: http://answers.unity3d.com/questions/9919/how-do-i-create-a-texture-dynamically-in-unity.html


    To load your sprites programmatically (not the ones created dynamically though), they need to be under a folder with the name Resources (which is a special folder).

    I.e. suppose you have a sprite named MySprite.png under a folder named Sprites which is under a folder named Resources. You would then load it like this:

    renderer.sprite = Resources.Load<Sprite>("Sprites/MySprite");
    

    (notice how you do not include the Resources folder and the extension of the sprite)

    You can find more details in the Loading Resources at Runtime documentation.


    PPtr cast failed when dereferencing!

    I did some searching and found out from this forum post that usually that happens when you had a list of one type and then changed it into another type. I.e. if mySprites was at some point GameObject[] and you linked a bunch of Sprites, and then changed it to Sprite[]. And since it specifically says Texture2D, I'm assuming that the import settings of your Sprites (or some of them) are set to Texture instead of Sprite:

    Sprite import settings


    Also, all MonoBehaviours have already a field named renderer, which is of type Renderer. I would suggest you rename that field to avoid confusion, unless you only used that as an example.

    0 讨论(0)
提交回复
热议问题