From 0b2c99e4a563d5f0cc648ec1ff67905fd60de47b Mon Sep 17 00:00:00 2001 From: uurha Date: Fri, 9 Dec 2022 00:46:57 +0000 Subject: [PATCH] Merge pull request #40 from uurha/dev Dev --- .../Preview/BaseWrappers/PreviewWrapper.cs | 13 +++--- Editor/EditorAddons/Helpers/EditorPopup.cs | 44 +++++++++++++++---- Samples~/TestSamples/New Prefab.prefab | 2 +- Samples~/TestSamples/Scripts/Test.cs | 2 +- Samples~/TestSamples/logo-social.png.meta | 16 +++---- package.json | 2 +- 6 files changed, 54 insertions(+), 25 deletions(-) diff --git a/Editor/EditorAddons/Drawers/Preview/BaseWrappers/PreviewWrapper.cs b/Editor/EditorAddons/Drawers/Preview/BaseWrappers/PreviewWrapper.cs index 2aca2c2..bb861b2 100644 --- a/Editor/EditorAddons/Drawers/Preview/BaseWrappers/PreviewWrapper.cs +++ b/Editor/EditorAddons/Drawers/Preview/BaseWrappers/PreviewWrapper.cs @@ -50,13 +50,13 @@ public override void OnGUI(Rect position, SerializedProperty serializedProperty, CheckInteraction(position, serializedProperty, size); } - private async void UpdateTextureLoop(CancellationToken cancellationToken) + private async void UpdateTextureLoop(EditorPopup editorPopup, CancellationToken cancellationToken) { await Task.Yield(); while (_isMouseDown && !cancellationToken.IsCancellationRequested) { UpdateTexture(); - EditorPopup.UpdatePosition(_currentScreenMousePosition); + editorPopup.UpdatePosition(_currentScreenMousePosition); await Task.Yield(); if (cancellationToken.IsCancellationRequested) break; } @@ -72,7 +72,8 @@ private void CheckInteraction(Rect position, SerializedProperty serializedProper else { var isLeftDrag = DrawersHelper.IsMouseButton(EventType.MouseDrag, DrawersHelper.MouseButtonLeft); - if (DrawersHelper.IsLeftButtonUp() || isLeftDrag && !contains && _isMouseDown) + var isLeftUp = DrawersHelper.IsLeftButtonUp(); + if (isLeftUp || isLeftDrag && !contains && _isMouseDown) { Deconstruct(); } @@ -86,12 +87,12 @@ private void MouseDownCase(SerializedProperty serializedProperty, float size) _previewScene.Construct(); var texture = GenerateTexture(serializedProperty.objectReferenceValue, size); if (texture == null) return; - EditorPopup.Initialize(texture, - new Rect(_currentScreenMousePosition, Vector2.one * size)); + var popup = EditorPopup.Initialize(texture, + new Rect(_currentScreenMousePosition, Vector2.one * size), true); _isMouseDown = true; _cancellation = new CancellationTokenSource(); - UpdateTextureLoop(_cancellation.Token); + UpdateTextureLoop(popup, _cancellation.Token); } } } diff --git a/Editor/EditorAddons/Helpers/EditorPopup.cs b/Editor/EditorAddons/Helpers/EditorPopup.cs index 92fc3c6..5639cb2 100644 --- a/Editor/EditorAddons/Helpers/EditorPopup.cs +++ b/Editor/EditorAddons/Helpers/EditorPopup.cs @@ -1,4 +1,5 @@ -using UnityEditor; +using System; +using UnityEditor; using UnityEngine; namespace Better.Attributes.EditorAddons.Helpers @@ -6,18 +7,37 @@ namespace Better.Attributes.EditorAddons.Helpers public class EditorPopup : EditorWindow { private Texture _texture; + private bool _needUpdate = true; + private bool _destroyTexture; + public event Action Closed; + public event Action FocusLost; - public static void Initialize(Texture texture, Rect position) + public static EditorPopup Initialize(Texture texture, Rect position, bool needUpdate, bool destroyTexture = false) { var window = HasOpenInstances() ? GetWindow() : CreateInstance(); window.position = position; window._texture = texture; + window._needUpdate = needUpdate; + window._destroyTexture = destroyTexture; window.ShowPopup(); + return window; + } + + public static EditorPopup InitializeAsWindow(Texture texture, Rect position, bool needUpdate, bool destroyTexture = false) + { + var window = HasOpenInstances() ? GetWindow() : CreateInstance(); + window.position = position; + window._texture = texture; + window._needUpdate = needUpdate; + window._destroyTexture = destroyTexture; + window.ShowUtility(); + return window; } private void Update() { - Repaint(); + if (_needUpdate) + Repaint(); } private void OnGUI() @@ -26,20 +46,28 @@ private void OnGUI() GUI.DrawTexture(new Rect(0, 0, position.width, position.height), _texture, ScaleMode.ScaleToFit, true); } + private void OnLostFocus() + { + FocusLost?.Invoke(); + } + public static void CloseInstance() { if (!HasOpenInstances()) return; var window = GetWindow(); + window.Closed?.Invoke(); + if (window._destroyTexture) + { + Destroy(window._texture); + } window.Close(); } - public static void UpdatePosition(Vector2 newPosition) + public void UpdatePosition(Vector2 newPosition) { - if (!HasOpenInstances()) return; - var window = GetWindow(); - var rect = window.position; + var rect = position; rect.position = newPosition; - window.position = rect; + position = rect; } } } \ No newline at end of file diff --git a/Samples~/TestSamples/New Prefab.prefab b/Samples~/TestSamples/New Prefab.prefab index ad18c31..f837e70 100644 --- a/Samples~/TestSamples/New Prefab.prefab +++ b/Samples~/TestSamples/New Prefab.prefab @@ -436,4 +436,4 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 48e1bdd289dc4c29bb25138e2231ffe7, type: 3} m_Name: m_EditorClassIdentifier: - sprite: {fileID: 10911, guid: 0000000000000000f000000000000000, type: 0} + sprite: {fileID: 21300000, guid: 78b772f3f305147419de3cc97ca72bd7, type: 3} diff --git a/Samples~/TestSamples/Scripts/Test.cs b/Samples~/TestSamples/Scripts/Test.cs index c71b587..2e65f41 100644 --- a/Samples~/TestSamples/Scripts/Test.cs +++ b/Samples~/TestSamples/Scripts/Test.cs @@ -24,7 +24,7 @@ public enum MyFlagEnum public class Test : MonoBehaviour { - [IconHeader("78b772f3f305147419de3cc97ca72bd7")] + [IconHeader("78b772f3f305147419de3cc97ca72bd7")] [SelectEnum] [SerializeField] private KeyCode keyCode; [SelectEnum] [SerializeField] private MyFlagEnum myFlagEnumTest; diff --git a/Samples~/TestSamples/logo-social.png.meta b/Samples~/TestSamples/logo-social.png.meta index 1cae15a..4e4bca7 100644 --- a/Samples~/TestSamples/logo-social.png.meta +++ b/Samples~/TestSamples/logo-social.png.meta @@ -6,7 +6,7 @@ TextureImporter: serializedVersion: 11 mipmaps: mipMapMode: 0 - enableMipMap: 1 + enableMipMap: 0 sRGBTexture: 1 linearTexture: 0 fadeOut: 0 @@ -36,13 +36,13 @@ TextureImporter: filterMode: 1 aniso: 1 mipBias: 0 - wrapU: 0 - wrapV: 0 + wrapU: 1 + wrapV: 1 wrapW: 0 - nPOTScale: 1 + nPOTScale: 0 lightmap: 0 compressionQuality: 50 - spriteMode: 0 + spriteMode: 1 spriteExtrude: 1 spriteMeshType: 1 alignment: 0 @@ -51,9 +51,9 @@ TextureImporter: spriteBorder: {x: 0, y: 0, z: 0, w: 0} spriteGenerateFallbackPhysicsShape: 1 alphaUsage: 1 - alphaIsTransparency: 0 + alphaIsTransparency: 1 spriteTessellationDetail: -1 - textureType: 0 + textureType: 8 textureShape: 1 singleChannelComponent: 0 flipbookRows: 1 @@ -130,7 +130,7 @@ TextureImporter: outline: [] physicsShape: [] bones: [] - spriteID: + spriteID: 5e97eb03825dee720800000000000000 internalID: 0 vertices: [] indices: diff --git a/package.json b/package.json index 95e1c4f..466cdb4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.uurha.betterattributes", "displayName": "Better Attributes", - "version": "0.2.5", + "version": "0.2.6", "unity": "2018.3", "description": "Unity attributes, allows to serialize interfaces, draw handles for Vector3/Vector2/Quaternion/Bounds, create read only fields.", "dependencies": {