- Home /
Drawing a pixel on a texture works on PC but not Android
I'm trying to create a simple way to draw a pixel onto a texture when the user is pressing on the screen (the goal is to make a simple paint app). I took from the UnityPaint example, but found although it worked on PC, it didn't work on my Android device when translating it to touch input.
Since then I have dumbed down the code as much as possible to try and debug what's going on, but even though the touch input is detecting and correctly noting the position of the player's finger and states, it simply wont draw any pixel.
Below is the code used. All I did was create a 2D project, and attach this script to the camera (I also added a Canvas and Text to debug the information). I have tried on both a normal Android device as well as Bluestacks
using UnityEngine;
using System.Collections;
public class Paint : MonoBehaviour
{
public UnityEngine.UI.Text DebugText = null;
Texture2D Texture;
// Use this for initialization
void Start ()
{
Texture = new Texture2D(Screen.width, Screen.height);
}
// Update is called once per frame
void Update ()
{
ProcessTouch();
}
void OnGUI()
{
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Texture);
}
void ProcessTouch()
{
int iNumTouches = Input.touchCount;
if (iNumTouches > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
if (DebugText)
DebugText.text = touch.fingerId + " STARTED";
}
if (touch.phase == TouchPhase.Moved)
{
if (DebugText)
DebugText.text = touch.fingerId + " MOVING";
Texture.SetPixel((int)touch.position.x, (int)touch.position.y, new Color(255, 0, 0));
}
if (touch.phase == TouchPhase.Stationary)
{
if (DebugText)
DebugText.text = touch.fingerId + " STOPPED";
Texture.SetPixel((int)touch.position.x, (int)touch.position.y, new Color(255, 0, 0));
}
if (touch.phase == TouchPhase.Ended)
{
if (DebugText)
DebugText.text = touch.fingerId + " ENDED";
}
}
}
}
Is anyone able to help with why this might not be working on an Android compared to working on PC? My initial thought was that the texture resolution and touch position might be different, but I've found they're not.
Another piece of useful information is that if I spam the touch enough times, it may eventually draw a single pixel, but only very rarely.
Thanks
Answer by AbdoEmary · Apr 12, 2020 at 07:11 PM
@squidgemelent I'm wondering .. Have you ever managed to solve this problem ?
Your answer
Follow this Question
Related Questions
How to paint color on plane in runtime on mouse position? 0 Answers
How can I show my SetPixels() texture changes without having to zoom my orthographic camera? 1 Answer
HELP android laggs 3 Answers
Ui text in Scrollvieww problem (on android) 0 Answers
Open a local html file in the Android's default browser 1 Answer