Exemplo n.º 1
0
 public static Sprite CreateSprite(this Texture2D texture, RectInt rect, Vector2?pivot = null, float pixelsPerUnit = 100)
 {
     if (pivot == null)
     {
         pivot = new Vector2(0.5f, 0.5f);
     }
     ;
     return(Sprite.Create(texture, rect.ToRect(), (Vector2)pivot, pixelsPerUnit));
 }
Exemplo n.º 2
0
        /// <summary>
        /// 复制为2D材质
        /// </summary>
        /// <param name="value">目标渲染材质</param>
        /// <param name="rect">截取区域</param>
        /// <returns></returns>
        public static Texture2D CopyAsTexture2D(this RenderTexture value, RectInt rect)
        {
            var result = new Texture2D(rect.width, rect.height, TextureFormat.ARGB32, false);
            var currentRenderTarget = RenderTexture.active;

            RenderTexture.active = value;
            result.ReadPixels(rect.ToRect(), 0, 0); // 从当前RenderTexture读取数据,天知道Unity为何把函数叫这个名
            result.Apply();
            RenderTexture.active = currentRenderTarget;
            return(result);
        }
        private static SpriteMetaData CreateSpriteMetaData(int frame, SpritesheetData data, string name, TextureImporter textureImporter, SpritesheetDataImporter spritesheetImporter, Texture2D texture)
        {
            int rowSubdivisions    = spritesheetImporter.subdivideSprites ? spritesheetImporter.subdivisions.y : 1;
            int columnSubdivisions = spritesheetImporter.subdivideSprites ? spritesheetImporter.subdivisions.x : 1;
            int numRows            = data.numRows * rowSubdivisions;
            int numColumns         = data.numColumns * columnSubdivisions;
            int columnWidth        = data.spriteWidth / columnSubdivisions;
            int rowHeight          = data.spriteHeight / rowSubdivisions;

            // Our spritesheets and Unity's sprite coordinate system are both row-major; however, Unity's origin is in the bottom left
            // of the texture, and ours is in the top left. We just have to do a small transformation to match.
            int row    = numRows - frame / numColumns - 1;
            int column = frame % numColumns;

            // The input texture might have padding applied, which would be at the right and the bottom. Since our coordinate system
            // and Unity's both start on the left, we don't care about the horizontal padding.
            Rect spriteRect = new Rect(columnWidth * column, data.paddingHeight + rowHeight * row, columnWidth, rowHeight);

            Log($"Frame {frame} ({name}) is in row {row} and column {column} (Unity coordinates); its subrect is {spriteRect}", LogLevel.Verbose);

            if (spritesheetImporter.trimIndividualSprites)
            {
                RectInt trimmedRect = texture.GetTrimRegion(spritesheetImporter.trimAlphaThreshold, spriteRect.ToRectInt());
                Log($"Trimmed sprite rect from {spriteRect} to {trimmedRect}", LogLevel.Verbose);

                spriteRect = trimmedRect.ToRect();
            }

            var metadata = new SpriteMetaData()
            {
                alignment = (int)spritesheetImporter.pivotPlacement,
                border    = Vector4.zero,
                name      = name,
                rect      = spriteRect
            };

            if (spritesheetImporter.pivotPlacement == SpriteAlignment.Custom)
            {
                metadata.pivot = FindCustomPivotPoint(metadata, textureImporter, spritesheetImporter);
            }

            return(metadata);
        }
Exemplo n.º 4
0
        private void OnGUI()
        {
            imageFolder = EditorGUILayout.TextField("Character Image Folder", imageFolder);

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Load Selected Folder"))
            {
                imageFolder = EditorUtils.GetSelectedDirectory();
                uncropped   = imageFolder.ToLower().Contains("uncrop");
                layers.Clear();
                dirty = true;
            }

            uncropped = GUILayout.Toggle(uncropped, "Uncropped");
            GUILayout.EndHorizontal();

            if (!uncropped)
            {
                referenceSize = EditorGUILayout.Vector2IntField("Reference Size", referenceSize);
                pixelsPerUnit = EditorGUILayout.FloatField("Pixels Per Unit", pixelsPerUnit);
            }

            reorderableList.DoLayoutList();

            if (GUILayout.Button("Refresh"))
            {
                dirty = true;
            }

            if (dirty)
            {
                dirty = false;

                var sprites = layers.Where(p => !string.IsNullOrEmpty(p.name)).Select(p => p.sprite).ToList();
                if (sprites.Count == 0)
                {
                    texture = null;
                }
                else
                {
                    if (uncropped)
                    {
                        var sprite = sprites[0].sprite;
                        merger.referenceSize = new Vector2Int(sprite.texture.width, sprite.texture.height);
                        merger.pixelsPerUnit = sprite.pixelsPerUnit;
                    }
                    else
                    {
                        merger.referenceSize = referenceSize;
                        merger.pixelsPerUnit = pixelsPerUnit;
                    }

                    texture = merger.GetMergedTexture(name, sprites);
                }
            }

            if (texture == null)
            {
                return;
            }

            GUILayout.Label("Composed Pose Lua Table", EditorStyles.boldLabel);
            var luaTable = LayersToLuaTable(layers);

            EditorGUILayout.SelectableLabel(luaTable);
            if (GUILayout.Button("Copy"))
            {
                EditorGUIUtility.systemCopyBuffer = luaTable;
            }

            GUILayout.Label("Preview", EditorStyles.boldLabel);

            useCaptureBox = GUILayout.Toggle(useCaptureBox, "Use Capture Box");
            if (useCaptureBox)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("Capture Box");
                captureBox = EditorGUILayout.RectIntField(captureBox);
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                captureDest = EditorGUILayout.TextField("Capture Destination", captureDest);

                if (GUILayout.Button("Capture"))
                {
                    Texture2D tex = new Texture2D(captureBox.width, captureBox.height, TextureFormat.ARGB32, false);
                    RenderTexture.active = texture as RenderTexture;
                    tex.ReadPixels(new Rect(captureBox.x, captureBox.y, captureBox.width, captureBox.height), 0, 0);
                    RenderTexture.active = null;
                    tex.Apply();

                    var absoluteCaptureDest = Path.Combine(Path.GetDirectoryName(Application.dataPath), captureDest);
                    Directory.CreateDirectory(Path.GetDirectoryName(absoluteCaptureDest));
                    File.WriteAllBytes(absoluteCaptureDest, tex.EncodeToPNG());
                    EditorUtility.DisplayDialog("Capture Finished", $"Saved at {absoluteCaptureDest}", "OK");
                }

                GUILayout.EndHorizontal();
            }

            var previewRect =
                EditorGUILayout.GetControlRect(false, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));

            previewRect.size = Utils.GetContentSize(previewRect.size, (float)texture.width / texture.height);
            var scale = previewRect.width / texture.width;

            EditorGUI.DrawTextureTransparent(previewRect, texture);

            if (useCaptureBox)
            {
                EditorUtils.DrawPreviewCaptureFrame(previewRect, captureBox.ToRect(), scale, false, Color.red);
            }
        }