From a0f0405ae084fbf9218ccb19ba3f745437e2f759 Mon Sep 17 00:00:00 2001 From: shenjianxing <”315615051@qq.com“> Date: Mon, 24 Feb 2025 17:37:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=94=BB=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Art/UIPrefab/UIDraw.prefab | 191 ++++++++++++++++++++++- Assets/Draw/Scripts/ScreenShotPainter.cs | 100 ++++++++---- Assets/Scripts/UI/UIDraw.Designer.cs | 8 +- Assets/Scripts/UI/UIDraw.cs | 2 +- 4 files changed, 266 insertions(+), 35 deletions(-) diff --git a/Assets/Art/UIPrefab/UIDraw.prefab b/Assets/Art/UIPrefab/UIDraw.prefab index dbc3e5dc..d257f81c 100644 --- a/Assets/Art/UIPrefab/UIDraw.prefab +++ b/Assets/Art/UIPrefab/UIDraw.prefab @@ -166,6 +166,8 @@ RectTransform: - {fileID: 2078712266289789712} - {fileID: 8464573970498514004} - {fileID: 6174875567737795578} + - {fileID: 7982209487117141860} + - {fileID: 8910459249382205206} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} @@ -187,7 +189,6 @@ MonoBehaviour: m_EditorClassIdentifier: captureBGImg: {fileID: 1613795782999759057} RawImg: {fileID: 3350589312635769069} - TempImg: {fileID: 0} Content: {fileID: 6174875567737795578} ClearBtn: {fileID: 2761790485595999517} BackBtn: {fileID: 8433557736356431636} @@ -216,6 +217,8 @@ MonoBehaviour: Purple: {fileID: 944934755680367845} White: {fileID: 8867501838096098682} More: {fileID: 5620078665499499056} + CirCleImg: {fileID: 2203796515819680784} + RectImg: {fileID: 1594643944354670706} --- !u!1 &572013391981846340 GameObject: m_ObjectHideFlags: 0 @@ -2173,6 +2176,98 @@ MonoBehaviour: m_hasFontAssetChanged: 0 m_baseMaterial: {fileID: 0} m_maskOffset: {x: 0, y: 0, z: 0, w: 0} +--- !u!1 &3169509151414725848 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 8910459249382205206} + - component: {fileID: 31084923243399467} + - component: {fileID: 1594643944354670706} + - component: {fileID: 3673863312826482967} + m_Layer: 0 + m_Name: RectImg + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 0 +--- !u!224 &8910459249382205206 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3169509151414725848} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 5248266707218333243} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0.5, y: 0.5} + m_AnchorMax: {x: 0.5, y: 0.5} + m_AnchoredPosition: {x: -50, y: 50} + m_SizeDelta: {x: -150.6967, y: -62.7747} + m_Pivot: {x: 0, y: 1} +--- !u!222 &31084923243399467 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3169509151414725848} + m_CullTransparentMesh: 1 +--- !u!114 &1594643944354670706 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3169509151414725848} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 10911, guid: 0000000000000000f000000000000000, type: 0} + m_Type: 1 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 + m_UseSpriteMesh: 0 + m_PixelsPerUnitMultiplier: 1 +--- !u!114 &3673863312826482967 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3169509151414725848} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 0d51f3a7c41ab0346b49ae50d456bece, type: 3} + m_Name: + m_EditorClassIdentifier: + MarkType: 0 + CustomComponentName: RectImg + CustomComment: + mComponentName: UnityEngine.UI.Image --- !u!1 &3214264869913376332 GameObject: m_ObjectHideFlags: 0 @@ -6679,6 +6774,98 @@ RectTransform: m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: -20, y: 0} m_Pivot: {x: 0.5, y: 0.5} +--- !u!1 &7432898069020291876 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 7982209487117141860} + - component: {fileID: 8828149263381877660} + - component: {fileID: 2203796515819680784} + - component: {fileID: 8683794650347190296} + m_Layer: 0 + m_Name: CirCleImg + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 0 +--- !u!224 &7982209487117141860 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7432898069020291876} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 5248266707218333243} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0.5, y: 0.5} + m_AnchorMax: {x: 0.5, y: 0.5} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 200, y: 200} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!222 &8828149263381877660 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7432898069020291876} + m_CullTransparentMesh: 1 +--- !u!114 &2203796515819680784 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7432898069020291876} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 21300000, guid: db4a67f07ed284f48b1c84975dacbd91, type: 3} + m_Type: 0 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 + m_UseSpriteMesh: 0 + m_PixelsPerUnitMultiplier: 1 +--- !u!114 &8683794650347190296 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7432898069020291876} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 0d51f3a7c41ab0346b49ae50d456bece, type: 3} + m_Name: + m_EditorClassIdentifier: + MarkType: 0 + CustomComponentName: CirCleImg + CustomComment: + mComponentName: UnityEngine.UI.Image --- !u!1 &7458362517572896503 GameObject: m_ObjectHideFlags: 0 @@ -6697,7 +6884,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!224 &8464573970498514004 RectTransform: m_ObjectHideFlags: 0 diff --git a/Assets/Draw/Scripts/ScreenShotPainter.cs b/Assets/Draw/Scripts/ScreenShotPainter.cs index 35346780..11473809 100644 --- a/Assets/Draw/Scripts/ScreenShotPainter.cs +++ b/Assets/Draw/Scripts/ScreenShotPainter.cs @@ -240,16 +240,21 @@ public class ScreenShotPainter : MonoBehaviour private float _lastPaintTime; private float _paintInterval = 0.02f; // 每0.02秒绘制一次 + Image circleImg; + Image rectImg; void Awake() { instance = this; } - public void Init(RawImage drawImg, RawImage bgRawImg) + public void Init(RawImage drawImg, RawImage bgRawImg, Image circleImg, Image rectImg) { if (isInited == false) { isInited = true; + this.circleImg = circleImg; + this.rectImg = rectImg; + _commandBuffer = new CommandBuffer(); _commandBuffer.name = "PaintCommandBuffer"; @@ -642,10 +647,17 @@ public class ScreenShotPainter : MonoBehaviour case Status.Circle: _currentLine = new LineSegment(_paintBrushMat.GetColor("_Color"), _brushSize, _eraserFlag); _lastPoint = Input.mousePosition; + circleImg.gameObject.SetActive(true); + // TODO: 这样会使UI位置有偏差 后续修改 + circleImg.transform.position = Input.mousePosition; break; case Status.Rect: - fixedPoint = Input.mousePosition; - prePoint = fixedPoint; + _currentLine = new LineSegment(_paintBrushMat.GetColor("_Color"), _brushSize, _eraserFlag); + _lastPoint = Input.mousePosition; + //rectImg.gameObject.SetActive(true); + // TODO: 这样会使UI位置有偏差 后续修改 + //rectImg.transform.position = Input.mousePosition; + isRect = true; break; } } @@ -657,10 +669,6 @@ public class ScreenShotPainter : MonoBehaviour ChangeToEraser(this.status == Status.Eraser); } - Vector2 fixedPoint = default; - Vector2 prePoint = default; - bool needUndo = false; - private void PaintDragging(BaseEventData data) { if (_enabled && _haveRegion) @@ -682,34 +690,65 @@ public class ScreenShotPainter : MonoBehaviour LerpPaint(Input.mousePosition, _eraserFlag); break; case Status.Rect: - if (needUndo) - { - if (_lineStack.Count > 0) - { - Undo(); - } - else - { - Clear(); - } - needUndo = false; - } - if (Vector2.Distance(prePoint, Input.mousePosition) > 1) - { - prePoint = Input.mousePosition; - _currentLine = new LineSegment(_paintBrushMat.GetColor("_Color"), _brushSize, _eraserFlag); - _lastPoint = fixedPoint; - RectFactory(); - needUndo = true; - } + break; + case Status.Circle: + var dis = Vector2.Distance(_lastPoint, Input.mousePosition) * 2; + circleImg.rectTransform.sizeDelta = new Vector2(dis, dis); + break; + } } } } } + bool isRect = false; + private void OnGUI() + { + if (isRect) + { + var points = GenerateRectanglePoints(_lastPoint, Input.mousePosition); + float minX = float.MaxValue; + float minY = float.MaxValue; + float maxX = float.MinValue; + float maxY = float.MinValue; + + // 找出最小和最大的 x、y 坐标 + foreach (Vector2 point in points) + { + if (point.x < minX) + { + minX = point.x; + } + if (point.x > maxX) + { + maxX = point.x; + } + if (point.y < minY) + { + minY = point.y; + } + if (point.y > maxY) + { + maxY = point.y; + } + } + + float x = minX; + float y = maxY; + float width = maxX - minX; + float height = maxY - minY; + + Rect rect = new Rect(x, y, width, height); + + GUI.backgroundColor = Color.blue; + GUI.color = Color.yellow; + GUI.Box(rect, "我帮你绘制了个 Rect Box"); + } + } + private void OnPaintEndDrag(BaseEventData data) { if (_enabled && _haveRegion) @@ -734,8 +773,8 @@ public class ScreenShotPainter : MonoBehaviour break; case Status.Rect: - fixedPoint = default; - needUndo = false; + isRect = false; + RectFactory(); break; case Status.Circle: @@ -752,7 +791,7 @@ public class ScreenShotPainter : MonoBehaviour LerpPaint(points[0], false); FinishedRaw(); - + circleImg.gameObject.SetActive(false); } break; @@ -910,7 +949,6 @@ public class ScreenShotPainter : MonoBehaviour vector2s[i] = _lastPoint + dir * (i + 1) * _brushLerpSize; Paint(isEraser, vector2s[i]); } - Debug.LogError(num); } _lastPoint = point; } diff --git a/Assets/Scripts/UI/UIDraw.Designer.cs b/Assets/Scripts/UI/UIDraw.Designer.cs index 6e3578b2..bcea6d73 100644 --- a/Assets/Scripts/UI/UIDraw.Designer.cs +++ b/Assets/Scripts/UI/UIDraw.Designer.cs @@ -5,7 +5,7 @@ using QFramework; namespace QFramework.Example { - // Generate Id:f3b60a04-6dce-45f6-968a-cb101a31a0c9 + // Generate Id:28bba342-def7-4cb6-b857-4ec16c1da4ca public partial class UIDraw { public const string Name = "UIDraw"; @@ -70,6 +70,10 @@ namespace QFramework.Example public UnityEngine.UI.Toggle White; [SerializeField] public UnityEngine.UI.Image More; + [SerializeField] + public UnityEngine.UI.Image CirCleImg; + [SerializeField] + public UnityEngine.UI.Image RectImg; private UIDrawData mPrivateData = null; @@ -105,6 +109,8 @@ namespace QFramework.Example Purple = null; White = null; More = null; + CirCleImg = null; + RectImg = null; mData = null; } diff --git a/Assets/Scripts/UI/UIDraw.cs b/Assets/Scripts/UI/UIDraw.cs index 719b7dd3..a40de843 100644 --- a/Assets/Scripts/UI/UIDraw.cs +++ b/Assets/Scripts/UI/UIDraw.cs @@ -230,7 +230,7 @@ namespace QFramework.Example { Show3DCamera.instance.lockMove = true; DrawController.instance.gameObject.SetActive(true); - ScreenShotPainter.instance.Init(RawImg, captureBGImg); + ScreenShotPainter.instance.Init(RawImg, captureBGImg,CirCleImg,RectImg); ScreenShotPainter.instance.SwitchOn(true); ScreenShotPainter.instance.SetPaintColor(Icon.color); RefreshAlphaSet();