diff --git a/Assets/Shapes2D/Editor/ShapeEditor.cs b/Assets/Shapes2D/Editor/ShapeEditor.cs index b62b310..53e9608 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 = false; + } 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