Esempio n. 1
0
    void CropTexture(RectOptions rectOptions)
    {
        for (int i = 0; i < listTexture2D.Count; i++)
        {
            Debug.Log(listString[i].Replace(Application.dataPath, "Assets"));
            RectInput.width  = widthCrop;
            RectInput.height = heightCrop;
            var res = CropWithRect(rectOptions, listTexture2D[i], RectInput, xMod, yMod, listString[i]);
            listTexture2D[i] = res.texture;
            var path     = listString[i];
            var importer = (TextureImporter)AssetImporter.GetAtPath(path);

            var sprites = new SpriteMetaData [importer.spritesheet.Length];
            Array.Copy(importer.spritesheet, 0, sprites, 0, importer.spritesheet.Length);
            for (var j = 0; j < sprites.Length; j++)
            {
                var rect = sprites[j].rect;
                rect.y          = rect.y - 716;
                rect.x          = rect.x - res.xRect;
                sprites[j].rect = rect;
            }

            importer.spritesheet = sprites;
            Debug.Log($"yRect: {res.yRect}");
            EditorUtility.SetDirty(importer);
            byte[] bytes = listTexture2D[i].EncodeToPNG();

            File.WriteAllBytes(listString[i].Replace(Application.dataPath, "Assets"), bytes);
            importer.SaveAndReimport();
        }

        AssetDatabase.Refresh();
    }
Esempio n. 2
0
    private static Texture2D CropWithRect(RectOptions rectOptions, Texture2D texture, Rect r, int xMod, int yMod)
    {
        if (r.height < 0 || r.width < 0)
        {
            return(texture);
        }
        Texture2D result = new Texture2D((int)r.width, (int)r.height);

        if (r.width != 0 && r.height != 0)
        {
            float xRect      = r.x;
            float yRect      = r.y;
            float widthRect  = r.width;
            float heightRect = r.height;

            switch (rectOptions)
            {
            case RectOptions.Center:
                xRect = (texture.width - r.width) / 2;
                yRect = (texture.height - r.height) / 2;
                break;

            case RectOptions.BottomRight:
                xRect = texture.width - r.width;
                break;

            case RectOptions.BottomLeft:
                break;

            case RectOptions.TopLeft:
                yRect = texture.height - r.height;
                break;

            case RectOptions.TopRight:
                xRect = texture.width - r.width;
                yRect = texture.height - r.height;
                break;

            case RectOptions.Custom:
                float tempWidth  = texture.width - r.width - xMod;
                float tempHeight = texture.height - r.height - yMod;
                xRect = tempWidth > texture.width ? 0 : tempWidth;
                yRect = tempHeight > texture.height ? 0 : tempHeight;
                break;
            }

            if (texture.width < r.x + r.width || texture.height < r.y + r.height || xRect > r.x + texture.width || yRect > r.y + texture.height || xRect < 0 || yRect < 0 || r.width < 0 || r.height < 0)
            {
                //EditorUtility.DisplayDialog("Set value crop", "Set value crop (Width and Height > 0) less than origin texture size \n" + texture.name + " wrong size", "ReSet");
                return(texture);
            }
            result.SetPixels(texture.GetPixels(Mathf.FloorToInt(xRect), Mathf.FloorToInt(yRect), Mathf.FloorToInt(widthRect), Mathf.FloorToInt(heightRect)));
            result.Apply();
        }
        return(result);
    }
Esempio n. 3
0
    void CropTexture(RectOptions rectOptions)
    {
        Debug.Log("crop");
        for (int i = 0; i < listTexture2D.Count; i++)
        {
            Debug.Log(listString[i].Replace(Application.dataPath, "Assets"));
            RectInput.width  = widthCrop;
            RectInput.height = heightCrop;

            listTexture2D[i] = CropWithRect(rectOptions, listTexture2D[i], RectInput, xMod, yMod);
            byte[] bytes = listTexture2D[i].EncodeToPNG();
            File.WriteAllBytes(listString[i].Replace(Application.dataPath, "Assets"), bytes);
        }
        AssetDatabase.Refresh();
    }
        public static IEnumerator CropSquare(
            Texture2D texture, RectOptions rectOptions, System.Action <Texture2D> callback)
        {
            var smallest = texture.width < texture.height ? texture.width : texture.height;
            var rect     = new Rect(0, 0, smallest, smallest);

            if (rect.height < 0 || rect.width < 0)
            {
                throw new System.ArgumentException("Invalid texture size");
            }

            Texture2D result = new Texture2D((int)rect.width, (int)rect.height);

            if (rect.width != 0 && rect.height != 0)
            {
                float xRect      = rect.x;
                float yRect      = rect.y;
                float widthRect  = rect.width;
                float heightRect = rect.height;

                switch (rectOptions)
                {
                case RectOptions.Center:
                    xRect = (texture.width - rect.width) / 2;
                    yRect = (texture.height - rect.height) / 2;
                    break;

                case RectOptions.BottomRight:
                    xRect = texture.width - rect.width;
                    break;

                case RectOptions.BottomLeft:
                    break;

                case RectOptions.TopLeft:
                    yRect = texture.height - rect.height;
                    break;

                case RectOptions.TopRight:
                    xRect = texture.width - rect.width;
                    yRect = texture.height - rect.height;
                    break;

                case RectOptions.Custom:
                    float tempWidth  = texture.width - rect.width;
                    float tempHeight = texture.height - rect.height;
                    xRect = tempWidth > texture.width ? 0 : tempWidth;
                    yRect = tempHeight > texture.height ? 0 : tempHeight;
                    break;
                }

                if (texture.width < rect.x + rect.width || texture.height < rect.y + rect.height ||
                    xRect > rect.x + texture.width || yRect > rect.y + texture.height ||
                    xRect < 0 || yRect < 0 || rect.width < 0 || rect.height < 0)
                {
                    throw new System.ArgumentException("Set value crop less than origin texture size");
                }

                result.SetPixels(texture.GetPixels(Mathf.FloorToInt(xRect), Mathf.FloorToInt(yRect),
                                                   Mathf.FloorToInt(widthRect), Mathf.FloorToInt(heightRect)));
                yield return(null);

                result.Apply();
            }

            yield return(null);

            callback(result);
        }
        public static Texture2D CropWithRect(
            WebCamTexture texture, Rect rect, RectOptions rectOptions, int xMod, int yMod)
        {
            if (rect.height < 0 || rect.width < 0)
            {
                throw new System.ArgumentException("Invalid texture size");
            }

            Texture2D result = new Texture2D((int)rect.width, (int)rect.height);

            if (rect.width != 0 && rect.height != 0)
            {
                float xRect      = rect.x;
                float yRect      = rect.y;
                float widthRect  = rect.width;
                float heightRect = rect.height;

                switch (rectOptions)
                {
                case RectOptions.Center:
                    xRect = (texture.width - rect.width) / 2;
                    yRect = (texture.height - rect.height) / 2;
                    break;

                case RectOptions.BottomRight:
                    xRect = texture.width - rect.width;
                    break;

                case RectOptions.BottomLeft:
                    break;

                case RectOptions.TopLeft:
                    yRect = texture.height - rect.height;
                    break;

                case RectOptions.TopRight:
                    xRect = texture.width - rect.width;
                    yRect = texture.height - rect.height;
                    break;

                case RectOptions.Custom:
                    float tempWidth  = texture.width - rect.width - xMod;
                    float tempHeight = texture.height - rect.height - yMod;
                    xRect = tempWidth > texture.width ? 0 : tempWidth;
                    yRect = tempHeight > texture.height ? 0 : tempHeight;
                    break;
                }

                if (texture.width < rect.x + rect.width || texture.height < rect.y + rect.height ||
                    xRect > rect.x + texture.width || yRect > rect.y + texture.height ||
                    xRect < 0 || yRect < 0 || rect.width < 0 || rect.height < 0)
                {
                    throw new System.ArgumentException("Set value crop less than origin texture size");
                }

                result.SetPixels(texture.GetPixels(Mathf.FloorToInt(xRect), Mathf.FloorToInt(yRect),
                                                   Mathf.FloorToInt(widthRect), Mathf.FloorToInt(heightRect)));
                result.Apply();
            }

            return(result);
        }
Esempio n. 6
0
    void ChooseOptions(Options options)
    {
        switch (options)
        {
        case Options.Resize:
            widthResize  = EditorGUILayout.IntField("Width:", widthResize);
            heightResize = EditorGUILayout.IntField("Height:", heightResize);
            EditorGUILayout.Space();
            if (listTexturesOrigin.Count > 0)
            {
                widthResize  = widthResize <= 0 ? 0 : widthResize;
                heightResize = heightResize <= 0 ? 0 : heightResize;
            }
            int oldwidthResize  = 0;
            int oldheightResize = 0;
            if (oldwidthResize != widthResize || oldheightResize != heightResize)
            {
                Review();
            }
            oldheightResize = heightResize;
            oldwidthResize  = widthResize;
            if (GUILayout.Button("Resize Texture2D"))
            {
                ResizeTexture();
            }
            break;

        case Options.Crop:
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.BeginVertical();
            EditorGUI.BeginDisabledGroup(true);
            EditorGUI.EndDisabledGroup();
            rectOptions = (RectOptions)EditorGUILayout.EnumPopup("Position", rectOptions);
            widthCrop   = EditorGUILayout.IntField("   Width: ", widthCrop);
            heightCrop  = EditorGUILayout.IntField("   Height: ", heightCrop);
            if (listTexturesOrigin.Count > 0)
            {
                widthCrop  = listTexturesOrigin[0].width < widthCrop ? listTexturesOrigin[0].width : widthCrop <= 0 ? 0 : widthCrop;
                heightCrop = listTexturesOrigin[0].height < heightCrop ? listTexturesOrigin[0].height : heightCrop <= 0 ? 0 : heightCrop;
            }
            int oldwidthCrop  = 0;
            int oldheightCrop = 0;
            if (oldwidthCrop != widthCrop || oldheightCrop != heightCrop)
            {
                Review();
            }
            oldheightCrop = heightCrop;
            oldwidthCrop  = widthCrop;
            if (rectOptions == RectOptions.Custom)
            {
                xMod = EditorGUILayout.IntField("   xMod: ", xMod);
                yMod = EditorGUILayout.IntField("   yMod: ", yMod);
                if (listTexturesOrigin.Count > 0)
                {
                    int tempxMod = listTexturesOrigin[0].width - widthCrop;
                    int tempyMod = listTexturesOrigin[0].height - heightCrop;
                    xMod = xMod <= 0 ? 0 : xMod >= tempxMod ? tempxMod : xMod;
                    yMod = yMod <= 0 ? 0 : yMod >= tempyMod ? tempyMod : yMod;
                }
                int oldxMod = 0;
                int oldyMod = 0;
                if (oldxMod != xMod || oldyMod != yMod)
                {
                    Review();
                }
                oldxMod = xMod;
                oldyMod = yMod;
            }
            EditorGUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();
            if (GUILayout.Button("Crop Texture"))
            {
                CropTexture(rectOptions);
            }
            break;
        }
    }
Esempio n. 7
0
    private static Result CropWithRect(RectOptions rectOptions, Texture2D texture, Rect r, int xMod, int yMod,
                                       string path)
    {
        if (r.height < 0 || r.width < 0)
        {
            return(new Result()
            {
                texture = texture, xRect = -1, yRect = -1
            });
        }


        Texture2D result   = new Texture2D((int)r.width, (int)r.height);
        var       importer = (TextureImporter)TextureImporter.GetAtPath(path);
        var       sprites  = new SpriteMetaData [importer.spritesheet.Length];
        float     xRect    = r.x;
        float     yRect    = r.y;

        if (r.width != 0 && r.height != 0)
        {
            float widthRect  = r.width;
            float heightRect = r.height;

            switch (rectOptions)
            {
            case RectOptions.Center:
                xRect = (texture.width - r.width) / 2;
                yRect = (texture.height - r.height) / 2;
                break;

            case RectOptions.BottomRight:
                xRect = texture.width - r.width;
                break;

            case RectOptions.BottomLeft:
                break;

            case RectOptions.TopLeft:
                yRect = texture.height - r.height;
                break;

            case RectOptions.TopRight:
                xRect = texture.width - r.width;
                yRect = texture.height - r.height;
                break;

            case RectOptions.Custom:
                float tempWidth  = texture.width - r.width - xMod;
                float tempHeight = texture.height - r.height - yMod;
                xRect = tempWidth > texture.width ? 0 : tempWidth;
                yRect = tempHeight > texture.height ? 0 : tempHeight;
                break;
            }

            if (texture.width < r.x + r.width || texture.height < r.y + r.height || xRect > r.x + texture.width ||
                yRect > r.y + texture.height || xRect < 0 || yRect < 0 || r.width < 0 || r.height < 0)
            {
                //EditorUtility.DisplayDialog("Set value crop", "Set value crop (Width and Height > 0) less than origin texture size \n" + texture.name + " wrong size", "ReSet");

                return(new Result()
                {
                    texture = result, xRect = xRect, yRect = yRect
                });
            }

            result.SetPixels(texture.GetPixels(Mathf.FloorToInt(xRect), Mathf.FloorToInt(yRect),
                                               Mathf.FloorToInt(widthRect), Mathf.FloorToInt(heightRect)));
            result.Apply();
        }

        return(new Result()
        {
            texture = result, yRect = yRect, xRect = xRect
        });
    }