Page with transparent region

For example you want to make the app where you want to paint the t-shirt, so you don't need to show anything exept of t-shirt, and you don't want to allow user paint anywhere outside of it.

Step by step guide

  1. Open CanvasController.cs in editor and make sure that DoSaveChangesToDisk and LoadFromDiskOrClear looks like this

  2. IEnumerator DoSaveChangesToDisk()
            {
                yield return new WaitForEndOfFrame();
                RenderTexture tmp = RenderTexture.active;
                RenderTexture.active = BackLayerController.RenderTexture;
    
                TmpTexture2D.ReadPixels(new Rect(0,0,BackLayerController.RenderTexture.width, BackLayerController.RenderTexture.height),0,0,false );
    
                File.WriteAllBytes(SaveFilePath, TmpTexture2D.EncodeToPNG());
    
                RenderTexture downscaledRT = RenderTexture.GetTemporary(440, 330);
                Graphics.Blit(CanvasCameraController.Camera.targetTexture, downscaledRT);
                RenderTexture.active = downscaledRT;
    
                TmpTextureIcon2D.ReadPixels(new Rect(0, 0, 440, 330), 0, 0, false);
                File.WriteAllBytes(PageConfig.IconSavePath, TmpTextureIcon2D.EncodeToPNG());
                RenderTexture.ReleaseTemporary(downscaledRT);
                RenderTexture.active = tmp;
            }
    
            public bool LoadFromDiskOrClear()
            {
                if (File.Exists(SaveFilePath) && !string.IsNullOrEmpty(PageConfig.name))
                {
                    if (TmpTexture2D.LoadImage(File.ReadAllBytes(SaveFilePath)))
                    {
                        CanvasCameraController.Camera.targetTexture.DiscardContents();
                        Graphics.Blit(TmpTexture2D, CanvasCameraController.Camera.targetTexture, new Material(Shader.Find("Unlit/Transparent")));
                        SetupTmpTextureSize();
                        return true;
                    }
                }
    
                ClearCanvas();
                return false;                        
            }
  3. In region file of your page config region which you don't want to paint must be transparent, another regions the same random color as usual

  4. You have to Use advanced eraser instead of normal. so when you will erase image it will erase in to default image rather than transparent color.

Last updated