Beispiel #1
0
    private void LoadAtlas(string resPath, SpriteAlignment alignment = SpriteAlignment.Center)
    {
        if (!_atlases.ContainsKey(resPath))
        {
            TextAsset   xmlAsset = Resources.Load <TextAsset>(resPath);
            AtlasData   atlas    = new AtlasData();
            XmlDocument document = new XmlDocument();
            document.LoadXml(xmlAsset.text);
            XmlElement root = document.DocumentElement;
            if (root.Name == "TextureAtlas")
            {
                bool failed = false;
                atlas.texture = Resources.Load <Texture2D>(resPath);
                int textureHeight = atlas.texture.height;
                foreach (XmlNode childNode in root.ChildNodes)
                {
                    if (childNode.Name == "sprite")
                    {
                        try
                        {
                            int width  = Convert.ToInt32(childNode.Attributes["w"].Value);
                            int height = Convert.ToInt32(childNode.Attributes["h"].Value);
                            int x      = Convert.ToInt32(childNode.Attributes["x"].Value);
                            int y      = textureHeight - (height + Convert.ToInt32(childNode.Attributes["y"].Value));

                            SpriteFrameData spriteMetaData = new SpriteFrameData
                            {
                                border = new Vector4(),
                                name   = childNode.Attributes["n"].Value,
                                pivot  = GetPivotValue(alignment),
                                rect   = new Rect(x, y, width, height)
                            };

                            atlas.frames.Add(spriteMetaData.name, spriteMetaData);
                        }
                        catch (Exception exception)
                        {
                            failed = true;
                            Debug.LogException(exception);
                        }
                    }
                    else
                    {
                        Debug.Log("Child nodes should be named 'sprite' !");
                    }
                }

                if (!failed)
                {
                    _atlases.Add(resPath, atlas);
                }
            }
            else
            {
                Debug.Log("XML needs to have a 'TextureAtlas' root node!");
            }
        }
    }
Beispiel #2
0
        internal Sprite(SpriteResource resource)
        {
            Performance.Push("Sprite ctor");
            fixedSpriteFrame = 0;
            allAnimations    = new List <SpriteAnimation>();
            CurrentAnimation = null;

            frameData = SpriteFrameData.LoadSpriteFrameData(resource);

            Performance.Pop();
        }
Beispiel #3
0
    public Sprite CreateSpriteFromAtlas(string resPath, string spriteName)
    {
        AtlasData atlas = GetAtlas(resPath);

        if (atlas != null)
        {
            if (atlas.frames.ContainsKey(spriteName))
            {
                SpriteFrameData frame = atlas.frames[spriteName];
                return(Sprite.Create(atlas.texture, frame.rect, frame.pivot));
            }
            else
            {
                Debug.Log("AtlasFramesCache: atlas <" + resPath + "> doesn't contains <" + spriteName + "> sprite!");
            }
        }
        // error loading atlas
        return(null);
    }
Beispiel #4
0
 public SpriteFrame(SpriteFrameData data)
 {
     Data    = data;
     lbSpeed = new Label(GameContent.font, $"[{FrameSpeed:N2}]");
     SetFrameSpeed(Data.FrameSpeed);
 }
Beispiel #5
0
 public SpriteFrame(int x, int y, int w, int h)
 {
     Data    = new SpriteFrameData(x, y, w, h, 0.25f);
     lbSpeed = new Label(GameContent.font, $"[{FrameSpeed:N2}]");
     SetFrameSpeed(Data.FrameSpeed);
 }