Question by
carter-carl30 · Sep 27, 2016 at 07:29 PM ·
c#scriptingproblemdrawingpaintingsetpixels
simple drawing script
hi all, I am using the script below to enable player to draw a simple picture :
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour {
public Camera cam;
void Start() {
cam = GetComponent<Camera>();
}
void Update() {
if (!Input.GetMouseButton(0))
return;
RaycastHit hit;
if (!Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit))
return;
Renderer rend = hit.transform.GetComponent<Renderer>();
MeshCollider meshCollider = hit.collider as MeshCollider;
if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null)
return;
Texture2D tex = rend.material.mainTexture as Texture2D;
Vector2 pixelUV = hit.textureCoord;
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
tex.SetPixel((int)pixelUV.x, (int)pixelUV.y, Color.red);
//tex.SetPixel((int)pixelUV.x, (int)pixelUV.y, Color.black);
tex.Apply();
}
}
the two problems I am having are 1) how do I reset the texture once drawn on? (so it is a blank page again as when first loaded and 2) at the moment the line isn't smooth it is staggered dots
can anyone please help me? thankyou for looking
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612093606im_/https://answers.unity.com/themes/thub/images/avi.jpg)