From c70d130760176919095a5eae288b62e76cfa0959 Mon Sep 17 00:00:00 2001 From: Oliver Smith <991572+all-iver@users.noreply.github.com> Date: Mon, 13 Feb 2023 15:04:46 -0800 Subject: [PATCH 1/2] Attempt to make SpriteMask on child GameObjects work during PNG render --- Assets/Shapes2D/Editor/ShapeEditor.cs | 45 ++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/Assets/Shapes2D/Editor/ShapeEditor.cs b/Assets/Shapes2D/Editor/ShapeEditor.cs index b62b310..d389083 100644 --- a/Assets/Shapes2D/Editor/ShapeEditor.cs +++ b/Assets/Shapes2D/Editor/ShapeEditor.cs @@ -213,6 +213,23 @@ private static Vector2 RenderToTexture2D(string path, Shape shape, float pixelsP RenderTexture oldRT = RenderTexture.active; RenderTexture.active = rt; + // create a backup of the shape's layer and enabled state so we can manipulate + // them and restore later. set all shapes to layer 31 and disable their + // graphics component. this leaves any other components enabled so e.g. + // SpriteMask on a child will still affect a parent. + List<(int oldLayer, bool wasEnabled)> backup = new List<(int oldLayer, bool wasEnabled)>(); + foreach (Shape s in shapes) { + if (canvas) { + backup.Add((s.gameObject.layer, s.GetComponent().enabled)); + s.gameObject.layer = 31; + s.GetComponent().enabled = true; + } else { + backup.Add((s.gameObject.layer, s.gameObject.GetComponent().enabled)); + s.gameObject.layer = 31; + s.gameObject.GetComponent().enabled = false; + } + } + // draw each shape with blending turned off, and layer them on top of each // other with blending between shapes but not between a shape and the // camera's background color, which is what would happen if we just let the @@ -224,7 +241,16 @@ private static Vector2 RenderToTexture2D(string path, Shape shape, float pixelsP // shapes but not between a shape and the background color, so that's why // we have to do it manually. Texture2D dstTex = null; - foreach (Shape s in shapes) { + for (int i = 0; i < shapes.Count; i++) { + if (!backup[i].wasEnabled) + continue; + // set this shape's graphic's enabled state to true so it will render + var s = shapes[i]; + if (canvas) { + s.GetComponent().enabled = true; + } else { + s.gameObject.GetComponent().enabled = true; + } if (canvas) { Graphic g = s.GetComponent(); g.enabled = true; @@ -249,6 +275,23 @@ private static Vector2 RenderToTexture2D(string path, Shape shape, float pixelsP s.GetComponent().showMaskGraphic = false; } } + // set the graphic's enabled back to false + if (canvas) { + s.GetComponent().enabled = false; + } else { + s.gameObject.GetComponent().enabled = false; + } + } + + // restore the layers and graphic enabled states + for (int i = 0; i < shapes.Count; i++) { + var s = shapes[i]; + s.gameObject.layer = backup[i].oldLayer; + if (canvas) { + s.GetComponent().enabled = backup[i].wasEnabled; + } else { + s.gameObject.GetComponent().enabled = backup[i].wasEnabled; + } } // put the old render texture back From 7ba2c35d62a9942019953b82a6064ec00fe8a8e5 Mon Sep 17 00:00:00 2001 From: Oliver Smith <991572+all-iver@users.noreply.github.com> Date: Mon, 13 Feb 2023 15:10:52 -0800 Subject: [PATCH 2/2] Fix typo for UI shapes --- Assets/Shapes2D/Editor/ShapeEditor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Shapes2D/Editor/ShapeEditor.cs b/Assets/Shapes2D/Editor/ShapeEditor.cs index d389083..53e9608 100644 --- a/Assets/Shapes2D/Editor/ShapeEditor.cs +++ b/Assets/Shapes2D/Editor/ShapeEditor.cs @@ -222,7 +222,7 @@ private static Vector2 RenderToTexture2D(string path, Shape shape, float pixelsP if (canvas) { backup.Add((s.gameObject.layer, s.GetComponent().enabled)); s.gameObject.layer = 31; - s.GetComponent().enabled = true; + s.GetComponent().enabled = false; } else { backup.Add((s.gameObject.layer, s.gameObject.GetComponent().enabled)); s.gameObject.layer = 31;