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

后端 未结 3 1449
感情败类
感情败类 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();
     Sprite[] sprites = Resources.LoadAll("Textures");
     string resName = "icon_favorite_2";
    
     foreach (UI2DSprite uiSprite in uiSprites)
     {
     if(uiSprite.name = "icon")
      SetSprite(uiSprite, sprites , resName);
    
     }
    

提交回复
热议问题