Пример #1
0
        private static bool DrawTexture(SerializedProperty sequence, Rect firstFrameRect, SerializedProperty frames, SerializedProperty fps, Texture2D firstFrameTexture, SerializedProperty firstFrame, SerializedProperty stickerName, SerializedObject sticker, ref bool updateIndexes)
        {
            if (sequence.boolValue)
            {
                GUI.Box(firstFrameRect, GUIContent.none);
                var frameIndex = StickerEditor.AnimatedIndex(frames, fps);

                var texture = firstFrameTexture;
                if (texture == null)
                {
                    return(true);
                }
                if (frameIndex < frames.arraySize)
                {
                    texture = frames.GetArrayElementAtIndex(frameIndex).objectReferenceValue as Texture2D ?? firstFrameTexture;
                }
                EditorGUI.DrawTextureTransparent(firstFrameRect, texture);
            }
            else
            {
                EditorGUI.BeginChangeCheck();
                var texture = EditorGUI.ObjectField(firstFrameRect, firstFrame.objectReferenceValue as Texture2D, typeof(Texture2D), false);
                if (EditorGUI.EndChangeCheck())
                {
                    firstFrame.objectReferenceValue = texture;
                    if (texture != null)
                    {
                        stickerName.stringValue   = texture.name;
                        sticker.targetObject.name = texture.name;
                        updateIndexes             = true;
                    }
                }
            }
            return(false);
        }
Пример #2
0
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (r.width < 60 || r.height < 60)
            {
                return;
            }

            var firstSticker       = stickers.GetArrayElementAtIndex(0);
            var firstStickerObject = new SerializedObject(firstSticker.objectReferenceValue);

            var frames = firstStickerObject.FindProperty("Frames");

            var firstFrame = frames.GetArrayElementAtIndex(0);

            var firstFrameTexture = firstFrame.objectReferenceValue as Texture2D;

            var size = new Vector2(firstFrameTexture != null ? firstFrameTexture.width : 0, firstFrameTexture != null ? firstFrameTexture.height : 0);

            var sizeIndex = 0;

            for (var index = 0; index < ValidSizes.Length; index++)
            {
                var validSize = ValidSizes[index];
                if (validSize <= size.x)
                {
                    sizeIndex = index;
                }
            }

            var stickersToDrawPerRow = StickerPerRow[sizeIndex];

            var viewRect = new Rect(r);

            viewRect.width -= 18;
            var pixelWidth     = viewRect.width / stickersToDrawPerRow;
            var x              = viewRect.xMin;
            var y              = viewRect.yMin;
            var stickerCounter = 0;

            viewRect.height = Mathf.CeilToInt(stickers.arraySize / 3f) * pixelWidth;
            previewScroll   = GUI.BeginScrollView(r, previewScroll, viewRect, false, true);

            var sequence = false;

            for (int i = 0; i < stickers.arraySize; i++)
            {
                var sticker = stickers.GetArrayElementAtIndex(i);
                firstStickerObject = new SerializedObject(sticker.objectReferenceValue);
                frames             = firstStickerObject.FindProperty("Frames");
                var fps = firstStickerObject.FindProperty("Fps");

                GUI.Box(new Rect(x, y, pixelWidth, pixelWidth), GUIContent.none);
                var firstFrameRect = new Rect(x + 2, y + 2, pixelWidth - 4, pixelWidth - 4);
                var frameIndex     = StickerEditor.AnimatedIndex(frames, fps);

                var texture = firstFrameTexture;
                if (texture == null)
                {
                    continue;
                }
                if (frameIndex < frames.arraySize)
                {
                    texture = frames.GetArrayElementAtIndex(frameIndex).objectReferenceValue as Texture2D ?? firstFrameTexture;
                }

                if (firstStickerObject.FindProperty("Sequence").boolValue)
                {
                    sequence = true;
                }
                EditorGUI.DrawTextureTransparent(firstFrameRect, texture);
                x += pixelWidth;

                stickerCounter++;

                if (stickerCounter >= stickersToDrawPerRow)
                {
                    x  = r.xMin;
                    y += pixelWidth;
                    stickerCounter = 0;
                }
            }
            GUI.EndScrollView();

            if (sequence)
            {
                Repaint();
            }
        }