unity编辑器输出图片透明度占比

[MenuItem("Assets/工具/输出图片透明度占比", false, 10)]
    public static void PutTextureAlphaNum()
    {
        Object[] objects = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);
        float prop = 0;
        if (objects.Length > 0)
        {
            Texture2D texture = objects[0] as Texture2D;
            float allNum = texture.height * texture.width;
            Color[] colors = texture.GetPixels();
            int aa = 0;
            for (int i = 0; i < allNum; i++)
            {
                if (colors[i] != null && colors[i].a == 0)
                {
                    aa++;
                }
            }
            prop = aa / allNum;
        }
        else
        {
            Debug.Log("Selection.gameObjects length is 0");
        }
        Debug.Log("texture alpha0 prop ==" + prop);
    }

 

posted @ 2021-10-26 19:43  搬砖独行者  阅读(143)  评论(0)    收藏  举报