我正在创建一个项目,其中可以通过单击鼠标/拖动来擦除图像的一部分。我已经点击了鼠标。但是,当快速拖动鼠标时,被擦除的部分之间会有间隙,例如 this example。
有解决方案吗?以下是我的代码,摘自here。
void Update () {
hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);
if (Input.GetMouseButton (0) && hit) {
UpdateTexture ();
}
}
public void UpdateTexture() {
spriteRend = gameObject.GetComponent<SpriteRenderer> ();
tex = CopyTexture (spriteRend.sprite.texture);
string tempName = spriteRend.sprite.name;
spriteRend.sprite = Sprite.Create (tex, spriteRend.sprite.rect, new Vector2 (0.5f, 0.5f));
spriteRend.sprite.name = tempName;
}
public Texture2D CopyTexture(Texture2D copiedTexture) {
float dX, dY;
Texture2D newTex = new Texture2D (copiedTexture.width, copiedTexture.height);
newTex.filterMode = FilterMode.Bilinear;
newTex.wrapMode = TextureWrapMode.Clamp;
int mX = (int)((hit.point.x - hit.collider.bounds.min.x) * (copiedTexture.width / hit.collider.bounds.size.x));
int mY = (int)((hit.point.y - hit.collider.bounds.min.y) * (copiedTexture.height / hit.collider.bounds.size.y));
for (int x = 0; x < newTex.width; x++) {
for (int y = 0; y < newTex.height; y++) {
dX = x - mX;
dY = y - mY;
if (dX * dX + dY * dY <= erSize * erSize) {
newTex.SetPixel (x, y, zeroAlpha);
} else {
newTex.SetPixel (x, y, copiedTexture.GetPixel (x, y));
}
}
}
newTex.Apply ();
return newTex;
}