コード例 #1
0
    public override void AddDynamicSpriteData(string spriteName, Texture2D textureReference, DynamicSpriteData.eSpriteResTypes type, bool is_runtime = true)
    {
        DynamicSpriteData data = GetFreeSpriteData();

        if (data == null)
        {
            data = new DynamicSpriteData();
            m_DynamicSpriteDatas.Add(data);
        }
        else
        {
            SquareAllocator.Node node = data.UserData as SquareAllocator.Node;
            if (node != null)
            {
                m_Allocator.Free(node);
            }
        }

        if (textureReference != data.TextureReference)
        {
            data.UnloadTexture();
        }

        data.name = spriteName;
        data.SpriteResourceType = type;
        data.UsageReference     = is_runtime ? 1 : 0;
        data.RuntimeFlag        = is_runtime;
        data.TextureReference   = textureReference;

        CalculateSpriteData(data);
    }
コード例 #2
0
    public override bool SynchronizeTexture(Texture2D texture)
    {
        if (texture != null)
        {
            DynamicSpriteData sprite_data = GetSpriteData(texture.name);
            if (sprite_data != null)
            {
                //sprite_data.UnloadTexture();
                //sprite_data.TextureReference = texture;
                //sprite_data.SpriteResourceType = DynamicSpriteData.eSpriteResTypes.Asset_Bundle;

                //CalculateSpriteData(sprite_data);

                if (sprite_data.TextureReference != texture)
                {
                    sprite_data.UnloadTexture();
                    sprite_data.TextureReference   = texture;
                    sprite_data.SpriteResourceType = DynamicSpriteData.eSpriteResTypes.Asset_Bundle;

                    CalculateSpriteData(sprite_data);
                }

                return(true);
            }
        }

        return(false);
    }
コード例 #3
0
    void OnTextureAsyncFailed()
    {
        if (isLoadHeroHead)
        {
            return;
        }

        if (spriteName.Equals(DynamicAtlas.CurrentAsyncedTexture))
        {
            if (mSpinningRender != null)
            {
                mSpinningRender.gameObject.SetActive(false);
            }
        }

        DynamicSpriteData sprite_data = DynamicAtlasManager.GetInstance().GetAtlasSprite(spriteName, mDynamicAtlasType) as DynamicSpriteData;

        if (sprite_data == null || sprite_data.TextureReference == null)
        {
            if (mDefaultRender != null)
            {
                mDefaultRender.SetActive(true);
            }
        }
    }
コード例 #4
0
    private DynamicSpriteData FindFreeSpriteData(int width, int height)
    {
        int size                 = m_DynamicSpriteDatas.size;
        int deltaSqure           = int.MaxValue;
        DynamicSpriteData result = null;

        for (int i = 0; i < size; i++)
        {
            var sprite = m_DynamicSpriteDatas[i];
            if (sprite != null && sprite.UsageReference <= 0 && sprite.RuntimeFlag && sprite.width >= width && sprite.height >= height)
            {
                int w  = sprite.width - width;
                int h  = sprite.height - height;
                int ds = w * w + h * h;
                if (ds < deltaSqure)
                {
                    deltaSqure = ds;
                    result     = sprite;

                    if (deltaSqure == 0)
                    {
                        break;
                    }
                }
            }
        }
        return(result);
    }
コード例 #5
0
    void LoadTextureAsyncCallback(string texName, Texture2D texture, bool success)
    {
        if (m_SpriteManager == null)
        {
            EB.Debug.LogError("Dynamic Sprite Manager is null for DynamicAtlas: {0}", gameObject.name);
            return;
        }
        if (success && texture != null)
        {
            DynamicSpriteData data = m_SpriteManager.GetSpriteData(texName);
            if (data != null)
            {
                data.AddReference();
            }
            else
            {
                m_SpriteManager.AddDynamicSpriteData(texName, texture, DynamicSpriteData.eSpriteResTypes.Asset_Bundle);
                m_UVUpdateFlag = true;

                CurrentAsyncedTexture = texture.name;
                EventDelegate.Execute(onTextureAsyncSucceeded);
            }
        }
        else
        {
            //EB.Debug.LogWarning(string.Format("Load texture [{0}] from assetbundle failed.", texName));
            CurrentAsyncedTexture = texName;
            EventDelegate.Execute(onTextureAsyncFailed);
        }
    }
コード例 #6
0
    static int CompareDynamicSprite(DynamicSpriteData a, DynamicSpriteData b)
    {
        // A is null b is not b is greater so put it at the front of the list
        if (a == null && b != null)
        {
            return(1);
        }

        // A is not null b is null a is greater so put it at the front of the list
        if (a != null && b == null)
        {
            return(-1);
        }


        // Get the total pixels used for each sprite
        int aPixels = a.width * a.height;
        int bPixels = b.width * b.height;

        if (aPixels > bPixels)
        {
            return(-1);
        }
        else if (aPixels < bPixels)
        {
            return(1);
        }
        return(0);
    }
コード例 #7
0
 public void AddSprite(DynamicSpriteData spriteData)
 {
     if (spriteData == null || spriteDictionary.ContainsKey(spriteData.Name))
     {
         return;
     }
     spriteDictionary.Add(spriteData.Name, spriteData);
     SpriteList.Add(spriteData);
 }
コード例 #8
0
    public void RemoveSpriteReference(string spriteName)
    {
        DynamicSpriteData sprite_data = GetSpriteData(spriteName);

        if (sprite_data != null)
        {
            sprite_data.RemoveReference();
            sprite_data.RuntimeFlag = true;
        }
    }
コード例 #9
0
    public DynamicSpriteData TryAddSpriteReference(string spriteName)
    {
        DynamicSpriteData sprite_data = GetSpriteData(spriteName);

        if (sprite_data != null)
        {
            sprite_data.AddReference();
            sprite_data.RuntimeFlag = true;
            return(sprite_data);
        }
        return(null);
    }
コード例 #10
0
    public DynamicSpriteManagerFixedSize(int mat_width, int mat_height, int sprite_width, int sprite_height)
    {
        int column      = mat_width / sprite_width;
        int row         = mat_height / sprite_height;
        int total_count = column * row;

        for (int i = 0; i < total_count; i++)
        {
            DynamicSpriteData sprite_data = new DynamicSpriteData();
            sprite_data.width  = sprite_width;
            sprite_data.height = sprite_height;
            sprite_data.x      = (i % column) * sprite_width;
            sprite_data.y      = (i / column) * sprite_height;
            m_DynamicSpriteDatas.Add(sprite_data);
        }
    }
コード例 #11
0
    public void LoadDynamicSprite(string spriteName, bool is_runtime = true)
    {
        if (m_SpriteManager == null)
        {
            EB.Debug.LogError("DynamicAtlas[LoadDynamicSprite]: Sprite Manager does not exist!");
            return;
        }

        DynamicSpriteData sprite_data = null;

        if (is_runtime)
        {
            sprite_data = m_SpriteManager.TryAddSpriteReference(spriteName);
        }
        else
        {
            sprite_data = m_SpriteManager.GetSpriteData(spriteName);
        }

        if (sprite_data == null)
        {
            //bool is_hd = Misc.HDAtlas();
            //Texture2D texture = Resources.Load(string.Format("{0}/{1}", is_hd ? m_LocalHDTexturePath : m_LocalSDTexturePath, spriteName)) as Texture2D;

            //m_SpriteManager.AddDynamicSpriteData(spriteName, texture, DynamicSpriteData.eSpriteResTypes.Local_Resource, is_runtime);

            //check if there is new icon in AssetBundle
            LoadTextureAsync(spriteName);
        }
        else
        {
            if (sprite_data.UsageReference == 1)
            {
                // no need to update uv
                //m_UVUpdateFlag = true;
            }

            CurrentAsyncedTexture = spriteName;
            EventDelegate.Execute(onTextureAsyncSucceeded);
        }
    }
コード例 #12
0
    public override void AddDynamicSpriteData(string spriteName, Texture2D textureReference, DynamicSpriteData.eSpriteResTypes type, bool is_runtime = true)
    {
        DynamicSpriteData sprite_data = GetFreeSpriteData();

        if (sprite_data != null)
        {
            if (sprite_data.TextureReference != textureReference)
            {
                sprite_data.UnloadTexture();
            }

            sprite_data.name               = spriteName;
            sprite_data.TextureReference   = textureReference;
            sprite_data.UsageReference     = is_runtime ? 1 : 0;
            sprite_data.RuntimeFlag        = is_runtime;
            sprite_data.SpriteResourceType = type;
        }
        else
        {
            EB.Debug.LogError("Try to load two many fixed-size textures in dynamic atlas");
        }
    }
コード例 #13
0
    /// <summary>
    /// A helper method that sets the UVs in the UV array based on a sprite data.
    /// </summary>
    /// <param name="index">Index of sprite data in atlas sprite list.</param>
    /// <param name="uvs">Array of uvs that will be filled.</param>
    private void SetUvs(int index, Vector2[] uvs)
    {
        DynamicSpriteData sprite = Atlas.SpriteList[index] as DynamicSpriteData;

        if (sprite == null)
        {
            Debug.LogError("Sprite data in atlas is null or of wrong type.");
            return;
        }

        // Get the x, y, width and height in UV space
        float x = (sprite.X - Padding) / TextureSize.x;
        float y = (sprite.Y - Padding) / TextureSize.y;
        float w = (sprite.Width + 2f * Padding) / TextureSize.x;
        float h = (sprite.Height + 2f * Padding) / TextureSize.y;

        // Apply
        uvs[index * 4]     = new Vector2(x, 1 - y);
        uvs[index * 4 + 1] = new Vector2(x + w, 1 - y);
        uvs[index * 4 + 2] = new Vector2(x, 1 - (y + h));
        uvs[index * 4 + 3] = new Vector2(x + w, 1 - (y + h));
    }
コード例 #14
0
    // Use this for initialization
    void Start()
    {
        int mat_width  = mDynamicMaterial.mainTexture.width;
        int mat_height = mDynamicMaterial.mainTexture.height;

        int sprite_width  = mDefaultSpriteMaterial.mainTexture.width;
        int sprite_height = mDefaultSpriteMaterial.mainTexture.height;
        int column        = mat_width / sprite_width;
        int row           = mat_height / sprite_height;

        float camera_size  = mRenderCamera.orthographicSize;
        float plane_width  = 2.0f * camera_size / (float)column;
        float plane_height = 2.0f * camera_size / (float)row;

        int     total_count = column * row;
        Vector3 local_pos   = Vector3.zero;
        Vector3 local_scale = new Vector3(plane_width, 0.0f, plane_height);

        for (int i = 0; i < total_count; i++)
        {
            MeshRenderer renderer = InstantiateMeshRenderer();
            local_pos.x = -camera_size + plane_width / 2.0f + i % column * plane_width;
            local_pos.y = camera_size - plane_height / 2.0f - i / column * plane_height;
            renderer.transform.localPosition = local_pos;
            renderer.transform.localScale    = local_scale;

            mMeshRenderers.Add(renderer);

            DynamicSpriteData sprite_data = new DynamicSpriteData();
            sprite_data.width  = sprite_width;
            sprite_data.height = sprite_height;
            sprite_data.x      = (i % column) * sprite_width;
            sprite_data.y      = (i / column) * sprite_height;
            mDynamicSpriteDatas.Add(sprite_data);
        }

        mIsInitialized = true;
    }
コード例 #15
0
 void CalculateSpriteData(DynamicSpriteData data)
 {
     if (data != null)
     {
         Texture2D texture = data.TextureReference;
         if (texture != null)
         {
             SquareAllocator.Node node = m_Allocator.Allocate(texture.width, texture.height);
             if (node != null)
             {
                 data.x        = node.X;
                 data.y        = node.Y;
                 data.width    = texture.width;
                 data.height   = texture.height;
                 data.UserData = node;
             }
             else
             {
                 EB.Debug.LogError("Failed to allocate squre![" + texture.name + "]");
             }
         }
     }
 }
コード例 #16
0
    /// <summary>
    /// Draws all new textures on atlas texture and creates/updates list of sprite data.
    /// Requires:
    /// textures is not null or empty; texturePositions contain position for every texture starting from indexOfFirstTexturePosition.
    /// Render target is set to desired texture.
    /// </summary>
    /// <param name="textures">Textures to be added to atlas texture.</param>
    /// <param name="texturePositionsFunc">This func should return positions of new textures in AtlasTexture and it will be called with index of element and atlas texture. Positions of new textures to be added should start from index specified by third argument.</param>
    /// <param name="indexOfFirstTexturePosition">Index of position of first texture in texturePositions list.</param>
    /// <param name="fillUnusedPlaces">Whether unused sprites should be swapped with new textures.</param>
    protected void DrawNewTextures(List <Texture> textures, Func <int, Texture, Rect> texturePositionsFunc, int indexOfFirstTexturePosition,
                                   bool fillUnusedPlaces)
    {
        // Mesh we will write to.
        Mesh mesh = new Mesh();

        mesh.MarkDynamic();

        // Initialize the data we need to fill
        Vector3[] positions = new Vector3[4];
        Vector2[] uvs       = new Vector2[4];
        int[]     triangles = new int[6];

        int j = 0;

        for (int i = 0; i < textures.Count; i++)
        {
            DynamicSpriteData spriteData = null;

            if (fillUnusedPlaces)
            {
                // first fill empty places, if there is no more empty places fill unused places
                Rect spritePosition = texturePositionsFunc(i + indexOfFirstTexturePosition, AtlasTexture);
                if (spritePosition.xMax > AtlasTexture.width || spritePosition.yMax > AtlasTexture.height)
                {
                    spriteData = FindFirstUnusedSprite(ref j);
                }
            }

            if (spriteData != null)
            {
                // unused sprite found
                numberOfUnused--;
                Atlas.RenameSprite(spriteData.Name, textures[i].name);
                SetPosition(texturePositionsFunc(j - 1, AtlasTexture), TextureSize, spriteData, positions, 0);
                indexOfFirstTexturePosition--;
            }
            else
            {
                spriteData = new DynamicSpriteData(textures[i].name);
                Atlas.AddSprite(spriteData);

                // Set the positions, and sprite data.
                SetPosition(texturePositionsFunc(i + indexOfFirstTexturePosition, AtlasTexture), TextureSize, spriteData, positions, 0);
            }

            spriteData.Reset();
            spriteData.Acquire();

            // Set the UVs
            SetupUvs(uvs, textures[i]);

            // Set the triangles
            SetTriangles(0, triangles);

            mesh.Clear();
            // Add the data to the mesh
            mesh.vertices  = positions;
            mesh.uv        = uvs;
            mesh.triangles = triangles;

            // Set the texture to render to the new atlas
            AtlasingMaterial.mainTexture = textures[i];
            // Activate the material
            AtlasingMaterial.SetPass(0);

            // Draw the texture to the atlas
            Graphics.DrawMeshNow(mesh, Matrix4x4.identity);
            MarkTextureForReusage();
        }

        // Cleanup the mesh, otherwise it will leak, unity won't do this on his own
        Object.Destroy(mesh);
    }
コード例 #17
0
    public override void AddDynamicSpriteData(string spriteName, Texture2D textureReference, DynamicSpriteData.eSpriteResTypes type, bool is_runtime = true)
    {
        //EB.Debug.LogError("AddDynamicSpriteData   spriteName  : {0}", spriteName);
        DynamicSpriteData data = GetSpriteData(spriteName);

        if (data != null)
        {
            EB.Debug.LogError("AddDynamicSpriteData: spriteName {0} exists", spriteName);
            data.AddReference();
            return;
        }

        data      = new DynamicSpriteData();
        data.name = spriteName;
        data.SpriteResourceType = type;
        data.UsageReference     = is_runtime ? 1 : 0;
        data.RuntimeFlag        = is_runtime;
        data.TextureReference   = textureReference;

        m_DynamicSpriteDatas.Add(data);
        if (!m_SpritePacker.PackSprites(m_DynamicSpriteDatas))
        {
            //Debug.Log("AddDynamicSpriteData: CAN NOT pack all of the sprites in the atlas, try reuse unused node");

            //failed to pack, get free space from current page.
            var freeData = FindFreeSpriteData(textureReference.width, textureReference.height);
            if (freeData == null)
            {
                //Debug.LogWarning("AddDynamicSpriteData: CAN NOT pack all of the sprites in the atlas, try remove unused node");

                for (int i = m_DynamicSpriteDatas.size - 1; i >= 0; --i)
                {
                    if (m_DynamicSpriteDatas[i].RuntimeFlag && m_DynamicSpriteDatas[i].UsageReference <= 0)
                    {
                        m_DynamicSpriteDatas[i].UnloadTexture();
                        m_DynamicSpriteDatas.RemoveAt(i);
                    }
                }

                EB.Debug.LogError("==================   m_DynamicSpriteDatas : {0}", m_DynamicSpriteDatas.size);
            }
            else
            {
                //Debug.LogFormat("AddDynamicSpriteData: {0} reuse node {1}", spriteName, freeData.name);

                m_DynamicSpriteDatas.Remove(data);
                if (freeData.TextureReference != textureReference)
                {
                    freeData.UnloadTexture();
                }

                freeData.name = spriteName;
                freeData.SpriteResourceType = type;
                freeData.UsageReference     = is_runtime ? 1 : 0;
                freeData.RuntimeFlag        = is_runtime;
                freeData.TextureReference   = textureReference;
            }

            m_Packed = false;
        }
        else
        {
            m_Packed = true;
        }
    }