Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity UI Attempting to make a simple paint surface - two problems

Discussion in 'UGUI & TextMesh Pro' started by vecima, Aug 22, 2017.

  1. vecima

    vecima

    Joined:
    Jun 6, 2017
    Posts:
    16
    I'm making a game that is entirely in unity UI. I'm trying to make a surface that the user can paint on. I have the basics of this working, but there are two things currently wrong with it:
    1. Whatever I paint on the desired surface appears on every UI surface with an "Image" component with "None" selected as the source. The below image shows a panel on the right that I painted on, and another panel on the left that should not be updated, however it is.

    2. I'm Using a Texture2D to "paint" on, and even though the surface that I use to set it up is after everything else in the hierarchy, the painted texture appears behind other elements. This image shows my painted lines moving behind the grid image, even though in the hierarchy my paint surface is after the grid image. The grid is supposed to be a "Background".

    My hierarchy looks like this:


    "Draw Surface" is the last sibling in the hierarchy, yet the Texture2D always displays behind "Background Image".

    Here is my script for drawing:

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class DrawSurface : MonoBehaviour {
    7.  
    8.     public int drawWidth;
    9.  
    10.     private RectTransform drawSurfaceRectTransform;
    11.     private Texture2D drawSurfaceTexture;
    12.     private float drawSurfaceWidth;
    13.     private float drawSurfaceHeight;
    14.  
    15.     private Vector2 localPointerPosition;
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         drawSurfaceRectTransform = this.gameObject.GetComponent<RectTransform>();
    20.         drawSurfaceWidth = drawSurfaceRectTransform.rect.width;
    21.         drawSurfaceHeight = drawSurfaceRectTransform.rect.height;
    22.         drawSurfaceTexture = new Texture2D((int) drawSurfaceWidth, (int) drawSurfaceHeight);
    23.         this.gameObject.GetComponent<Image>().material.mainTexture = drawSurfaceTexture;
    24.  
    25.         // Reset all pixels color to transparent
    26.         Color32 resetColor = new Color32(255, 255, 255, 255);
    27.         Color32[] resetColorArray = drawSurfaceTexture.GetPixels32();
    28.  
    29.         for (int i = 0; i < resetColorArray.Length; i++) {
    30.             resetColorArray[i] = resetColor;
    31.         }
    32.  
    33.         drawSurfaceTexture.SetPixels32(resetColorArray);
    34.         drawSurfaceTexture.Apply();
    35.      
    36.         //Debug.Log(GetInstanceID() + " - Started");
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update () {
    41.         if (Input.GetMouseButton(0) || Input.GetMouseButton(1)) {
    42.             Color drawColor = Color.black;
    43.             if (Input.GetMouseButton(1)) {
    44.                 drawColor = Color.red;
    45.             }
    46.  
    47.             if (RectTransformUtility.RectangleContainsScreenPoint(drawSurfaceRectTransform, Input.mousePosition, null))
    48.             {
    49.                 RectTransformUtility.ScreenPointToLocalPointInRectangle (drawSurfaceRectTransform, Input.mousePosition, null, out localPointerPosition);
    50.                 for (int i = -(drawWidth / 2); i < (drawWidth / 2) ; i++)
    51.                 {
    52.                     for (int j = -(drawWidth / 2); j < (drawWidth / 2) ; j++)
    53.                     {
    54.                         drawSurfaceTexture.SetPixel((int) (localPointerPosition.x + (drawSurfaceWidth / 2) + i), (int) (localPointerPosition.y + (drawSurfaceHeight / 2) + j), drawColor);
    55.                     }
    56.                 }
    57.                 drawSurfaceTexture.Apply();
    58.                 Debug.Log(drawSurfaceTexture.GetInstanceID() + " - Drawn");
    59.                 //Debug.Log(GetInstanceID() + " - Drawn");
    60.             }
    61.         }
    62.     }
    63. }
     
    Last edited: Aug 22, 2017
    gotiobg and OleksandrMartysh like this.
  2. OleksandrMartysh

    OleksandrMartysh

    Joined:
    Dec 13, 2015
    Posts:
    25
    Hey man. Thank you al ot for this code. I was used it as tutorial for making UI painting.
     
    vecima likes this.