Esempio n. 1
0
    // Called with new atlas.  Guesses the appropriate transparency type for
    // the atlas and select the shader based on this.
    private void PrepNewTexture()
    {
        if (!AutoOptimizeImages && !AutoGuessTransparency && !AutoGuessLayout)
        {
            return;
        }

        // Grab values to check against
        var atlas = Atlas as Texture2D;

        if (atlas == null)
        {
            return;
        }
        if (GetComponent <Renderer>() == null)
        {
            return;
        }
        Material material = GetComponent <Renderer>().sharedMaterial;

        if (material == null)
        {
            return;
        }

        TextureSettings.AddTemporaryReadableToTexture(atlas);

        // Optimize texture properties
        if (AutoOptimizeImages)
        {
            TextureSettings.SetAtlasAsOptimalForBbSprite(Atlas);
        }
        // Determine which shader to use
        if (AutoGuessTransparency)
        {
            if (atlas.GetPixel(0, 0).a != 1)
            {
                material.shader = Shader.Find("Sprites/BbSprites/Alpha");
            }
            else
            {
                Shader defaultColorShader =
                    Shader.Find("Sprites/BbSprites/ColorKey_UL");
                if (
                    material.shader != defaultColorShader &&
                    material.shader !=
                    Shader.Find("Sprites/BbSprites/ColorKey_UR") &&
                    material.shader !=
                    Shader.Find("Sprites/BbSprites/ColorKey_LL") &&
                    material.shader !=
                    Shader.Find("Sprites/BbSprites/ColorKey_LR") &&
                    material.shader !=
                    Shader.Find("Sprites/BbSprites/ColorKey_Custom"))
                {
                    material.shader = defaultColorShader;
                }
            }
        }
        // Determine which layout type to use
        if (AutoGuessLayout)
        {
            var layout = GetComponent <BbSpriteLayout>();
            if (layout == null)
            {
                return;
            }
            var width       = atlas.width;
            var hasKeyColor =
                (material.shader != Shader.Find("Sprites/BbSprites/Alpha"));
            // check for rpgmaker
            if (
                !TextureSettings.CheckTextureColumnForOpacity(
                    atlas, (int)(width * .33333f), hasKeyColor) &&
                !TextureSettings.CheckTextureColumnForOpacity(
                    atlas, (int)(width * .66666f), hasKeyColor)
                )
            {
                layout.MyType = BbSpriteLayout.Type.RpgMaker;
                layout.Update();
            }
            // check for 4 by 4
            else if (
                !TextureSettings.CheckTextureColumnForOpacity(
                    atlas, (int)(width * .5f), hasKeyColor) &&
                !TextureSettings.CheckTextureColumnForOpacity(
                    atlas, (int)(width * .25f), hasKeyColor) &&
                !TextureSettings.CheckTextureColumnForOpacity(
                    atlas, (int)(width * .75f), hasKeyColor)
                )
            {
                layout.MyType = BbSpriteLayout.Type.FourByFour;
                layout.Update();
            }
            // If failed other checks, is single cell
            else
            {
                layout.MyType = BbSpriteLayout.Type.SingleCell;
                layout.Update();
            }
        }

        TextureSettings.RemoveTemporaryReadableFromTexture(atlas);
    }