Unity Resources.Load vs as Sprite

前端 未结 2 1610
清酒与你
清酒与你 2021-01-18 17:25

I\'ve tried to change the image of my object with this code (used as Sprite cast):

GetComponent().sprite = Resources.L         


        
2条回答
  •  半阙折子戏
    2021-01-18 17:50

    FunctionR's answer is probably the more common answer, and I may just be wrong here, but I believe the difference between Load() and Load() is that Load() checks for meta-data. "Hole" is not a Sprite, it's an image file. Load() finds that image file and loads it as it's default type for the file type, in this case Texture2D.

    In other words, you can't use as Sprite because you cannot cast a Texture2D to a Sprite. You CAN, however, use

        Texture2D = Resources.Load("GameObjects/Tiles/Hole");
        Rect rect = {whatever};
        Vector2 pivot = {whatever};
        Sprite.Create(texture, rect, pivot);
    

    but this requires you to know the size of the Sprite you were trying to load.

    In summary, Load() treats it based solely on the file type you're loading, Load() includes metadata.

提交回复
热议问题