Skip to content

Instantly share code, notes, and snippets.

@shoridevel shoridevel/Scratch.cs
Last active Aug 13, 2017

Embed
What would you like to do?
Scratch
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Scratch : MonoBehaviour, IPointerClickHandler, IBeginDragHandler, IDragHandler {
[SerializeField]
private Image m_Image;
private Texture2D m_TmpTexture;
private RectTransform m_ImageRT;
void Start ()
{
if (m_Image == null)
{
m_Image = GetComponent<Image>();
}
if (m_Image == null)
{
Debug.Log("Image not found!!");
return;
}
m_ImageRT = m_Image.GetComponent<RectTransform>();
InitTexture(m_ImageRT.sizeDelta);
}
private void InitTexture(Vector2 size)
{
m_TmpTexture = CreateTexture2D((int)size.x, (int)size.y);
ResetTexture();
}
private void ResetTexture()
{
for (int x = 0; x < m_TmpTexture.width; x++)
{
for (int y = 0; y < m_TmpTexture.height; y++)
{
Color pixel = Color.white;
pixel.a = 0f;
m_TmpTexture.SetPixel(x, y, pixel);
}
}
m_TmpTexture.Apply();
m_Image.sprite = Sprite.Create(m_TmpTexture, new Rect(0, 0, m_TmpTexture.width, m_TmpTexture.height), Vector2.zero);
}
private void UpdateTexture2D(int coordinateX, int coordinateY, float radius)
{
if (coordinateX < 0 || coordinateX >= m_TmpTexture.width || coordinateY < 0 || coordinateY >= m_TmpTexture.height)
{
return;
}
for (int x = 0; x < m_TmpTexture.width; x++)
{
for (int y = 0; y < m_TmpTexture.height; y++)
{
double dx = x - coordinateX;
double dy = y - coordinateY;
double distanceSquared = dx * dx + dy * dy;
if (distanceSquared <= radius * radius)
{
m_TmpTexture.SetPixel(x, y, Color.white);
}
}
}
//m_Pixels[(coordinateY * m_TmpTexture.width) + coordinateX] = Color.white;
//m_TmpTexture.SetPixels(m_Pixels);
m_TmpTexture.Apply();
m_Image.sprite = Sprite.Create(m_TmpTexture, new Rect(0, 0, m_TmpTexture.width, m_TmpTexture.height), Vector2.zero);
}
public static Texture2D CreateTexture2D(int width, int height)
{
Texture2D texture = new Texture2D (width, height, TextureFormat.RGBA32, false);
texture.filterMode = FilterMode.Point;
return texture;
}
public void OnPointerClick(PointerEventData pointerEventData)
{
if (pointerEventData.button == PointerEventData.InputButton.Left)
{
UpdateByWorldSpacePos(pointerEventData.position, 30);
}
else
{
ResetTexture();
}
}
public void OnBeginDrag (PointerEventData pointerEventData)
{
UpdateByWorldSpacePos(pointerEventData.position, 30);
}
public void OnDrag(PointerEventData pointerEventData)
{
UpdateByWorldSpacePos(pointerEventData.position, 30);
}
public void UpdateByWorldSpacePos(Vector2 pos, int radius)
{
var localPosition = transform.InverseTransformPoint(pos);
var coordinate = new Vector2(localPosition.x + (m_ImageRT.sizeDelta.x * m_ImageRT.pivot.x), localPosition.y + (m_ImageRT.sizeDelta.y * m_ImageRT.pivot.y));
UpdateTexture2D((int)coordinate.x, (int)coordinate.y, radius);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.