示例#1
0
    public static void SwapSpriteFromFile(MonoBehaviour target, string filename)
    {
        try
        {
            Sprite newSprite = SpriteRegistry.Get(filename);
            if (newSprite == null)
            {
                newSprite = fromFile(FileLoader.pathToModFile("Sprites/" + filename + ".png"));
                SpriteRegistry.Set(filename, newSprite);
            }

            Image img = target.GetComponent <Image>();
            img.sprite = newSprite;
            //enemyImg.SetNativeSize();
            img.rectTransform.sizeDelta = new Vector2(newSprite.texture.width, newSprite.texture.height);
        }
        catch (Exception)
        {
            // TODO do something I guess
        }
    }
示例#2
0
    public static void SwapSpriteFromFile(Component target, string filename, int bubbleID = -1)
    {
        try {
            if (bubbleID != -1)
            {
                FileInfo fi = new FileInfo(Path.ChangeExtension(FileLoader.pathToModFile("Sprites/" + filename + ".png"), "xml"));
                if (!fi.Exists)
                {
                    fi = new FileInfo(Path.ChangeExtension(FileLoader.pathToDefaultFile("Sprites/" + filename + ".png"), "xml"));
                }
                if (fi.Exists)
                {
                    XmlDocument xmld = new XmlDocument();
                    xmld.Load(fi.FullName);
                    if (xmld["spritesheet"] != null && "single".Equals(xmld["spritesheet"].GetAttribute("type")))
                    {
                        if (!UnitaleUtil.IsOverworld)
                        {
                            UIController.instance.encounter.EnabledEnemies[bubbleID].bubbleWidth = ParseUtil.GetFloat(xmld["spritesheet"].GetElementsByTagName("width").Count > 0
                                ? xmld["spritesheet"].GetElementsByTagName("width")[0].InnerText
                                : xmld["spritesheet"].GetElementsByTagName("wideness")[0].InnerText);
                        }
                    }
                }
                else
                {
                    UIController.instance.encounter.EnabledEnemies[bubbleID].bubbleWidth = 0;
                }
            }
        } catch (Exception) {
            UIController.instance.encounter.EnabledEnemies[bubbleID].bubbleWidth = 0;
        }
        Sprite newSprite = SpriteRegistry.Get(filename);

        if (newSprite == null)
        {
            if (filename.Length == 0)
            {
                Debug.LogError("SwapSprite: Filename is empty!");
                return;
            }
            newSprite = FromFile(FileLoader.pathToModFile("Sprites/" + filename + ".png"));
            if (newSprite == null)
            {
                throw new CYFException("The sprite Sprites/" + filename + ".png doesn't exist.");
            }
            SpriteRegistry.Set(filename, newSprite);
        }

        Image img = target.GetComponent <Image>();

        if (!img)
        {
            SpriteRenderer img2  = target.GetComponent <SpriteRenderer>();
            Vector2        pivot = img2.GetComponent <RectTransform>().pivot;
            img2.sprite = newSprite;
            img2.GetComponent <RectTransform>().sizeDelta = new Vector2(newSprite.texture.width, newSprite.texture.height);
            img2.GetComponent <RectTransform>().pivot     = pivot;
        }
        else
        {
            Vector2 pivot = img.rectTransform.pivot;
            img.sprite = newSprite;
            //enemyImg.SetNativeSize();
            img.rectTransform.sizeDelta = new Vector2(newSprite.texture.width, newSprite.texture.height);
            img.rectTransform.pivot     = pivot;
        }
    }