Esempio n. 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);
        }
Esempio n. 2
0
        private void DrawStickerElement(Rect rect, int index, bool isActive, bool isFocused)
        {
            rect.y     += 2;
            rect.width -= ImageSize + ImagePadding;
            var stickerAsset = stickers.GetArrayElementAtIndex(index);

            var sticker = new SerializedObject(stickerAsset.objectReferenceValue);

            var frames      = sticker.FindProperty("Frames");
            var stickerName = sticker.FindProperty("Name");
            var sequence    = sticker.FindProperty("Sequence");
            var fps         = sticker.FindProperty("Fps");
            var reps        = sticker.FindProperty("Repetitions");

            Profiler.BeginSample("Sticker Element #" + index, stickerAsset.objectReferenceValue);
            {
                var nameRect = new Rect(rect);
                nameRect.y     += 1;
                nameRect.height = FieldHeight;

                EditorGUI.PropertyField(nameRect, stickerName);

                var sequenceRect = new Rect(rect);
                sequenceRect.y     += ButtonHeight + 2;
                sequenceRect.height = ButtonHeight;
                sequenceRect.width -= 150;
                EditorGUI.PropertyField(sequenceRect, sequence);

                EditorGUI.BeginDisabledGroup(!sequence.boolValue);
                var buttonRect = new Rect(rect);
                buttonRect.y   += ButtonHeight;
                buttonRect.xMin = EditorGUIUtility.labelWidth + 50;
                if (GUI.Button(buttonRect, LoadFromFolder))
                {
                    StickerEditor.AddStickerSequence(sequence, stickerName, fps, frames);
                }

                var fieldRect = new Rect(rect);
                fieldRect.y     += FieldPadding + ButtonPadding;
                fieldRect.height = FieldHeight;
                EditorGUI.PropertyField(fieldRect, fps);
                fieldRect.y += FieldPadding;
                EditorGUI.PropertyField(fieldRect, reps);
                EditorGUI.EndDisabledGroup();

                if (frames.arraySize == 0)
                {
                    frames.InsertArrayElementAtIndex(0);
                }

                if (!sequence.boolValue && frames.arraySize > 1)
                {
                    frames.arraySize = 1;
                }

                fieldRect.y += FieldPadding;

                var firstFrameRect = new Rect(rect);
                firstFrameRect.height = ImageSize;
                firstFrameRect.x      = firstFrameRect.xMax + 4;
                firstFrameRect.y     += 1f;
                firstFrameRect.width  = ImageSize;

                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);

                Profiler.BeginSample("Size Help Box");
                {
                    DrawSizeHelpBox(fieldRect, size);
                }
                Profiler.EndSample();
                fieldRect.y += FieldPadding;

                Profiler.BeginSample("Disk Size Help Box");
                {
                    DrawDiskSizeHelpBox(fieldRect, stickerAsset);
                }
                Profiler.EndSample();
                var updateIndexes = false;

                Profiler.BeginSample("Draw texture");
                {
                    if (DrawTexture(sequence, firstFrameRect, frames, fps, firstFrameTexture, firstFrame, stickerName, sticker, ref updateIndexes))
                    {
                        return;
                    }
                }
                Profiler.EndSample();

                if (sticker.ApplyModifiedProperties() && updateIndexes)
                {
                    UpdatedIndexes(list);
                }
            }
            Profiler.EndSample();

            if (sequence.boolValue && frames.arraySize > 1)
            {
                RepaintView();
            }
        }
Esempio n. 3
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();
            }
        }