从FGUI中取一张图片并返回一个Sprite

从Fgui中的图集中取一个图素,把图素用到场景等非UI的地方。

此操作会动态创建一个Sprite对象,效率不好,不适合大量使用。

 private static Dictionary<string, Sprite> cacheSpriteByFGUIDic = new Dictionary<string, Sprite>();
/// <summary>
    /// 从FGUI中取一张图片并返回一个Sprite(不建议频繁使用)
    /// </summary>
    /// <param name="packageName"></param>
    /// <param name="texName"></param>
    public static Sprite GetSpriteByFGUI(string packageName, string texName)
    {
        if (cacheSpriteByFGUIDic.ContainsKey(texName))
            return cacheSpriteByFGUIDic[texName];
        else
        {
            FairyGUI.GImage gImage = FairyGUI.UIPackage.CreateObject(packageName, texName).asImage;
            Rect rect = new Rect(gImage.texture.uvRect.x * gImage.texture.nativeTexture.width, gImage.texture.uvRect.y * gImage.texture.nativeTexture.height,
               gImage.texture.uvRect.width * gImage.texture.nativeTexture.width, gImage.texture.uvRect.height * gImage.texture.nativeTexture.height);
            Sprite sprite = Sprite.Create((Texture2D)gImage.texture.nativeTexture, rect, new Vector2(0.5f, 0.5f), 100);
            gImage.Dispose();
            cacheSpriteByFGUIDic.Add(texName, sprite);
            return sprite;
        }
    }

 

上一篇:python-Pygame-使精灵沿其朝向移动


下一篇:pygame.sprite--精灵