Unity 刮刮乐(优化极简)

废话不多说上代码,上图片,欢迎对Unity有兴趣的伙伴和我一起探讨学习

using UnityEngine;
using UnityEngine.UI;

public class ScratchCardWithSpriteRenderer : MonoBehaviour
{
    // 公开背景和遮罩的Sprite Renderer组件
    public SpriteRenderer backgroundRenderer;
    public SpriteRenderer maskRenderer;

    // 遮罩原图的副本,用于保存未刮开状态
    private Texture2D originalMaskTexture;

    // 刮擦笔刷大小
    public float brushSize = 50f;

    void Start()
    {
        // 获取遮罩原图纹理并保存副本
        Texture2D maskTexture = maskRenderer.sprite.texture as Texture2D;
        originalMaskTexture = new Texture2D(maskTexture.width, maskTexture.height);
        originalMaskTexture.SetPixels(maskTexture.GetPixels());
        originalMaskTexture.Apply();
    }

    void Update()
    {
        // 当鼠标左键按下时
        if (Input.GetMouseButtonDown(0))
        {
            // 获取当前鼠标屏幕坐标
            Vector3 mousePosition = Input.mousePosition;

            // 转换为世界坐标,并投影到遮罩renderer所在的平面
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(mousePosition);
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, LayerMask.GetMask("笔触"))) 
            {
                Vector2 localPoint = hit.textureCoord;
                ApplyScratch(localPoint);
            }
        }
    }

    void ApplyScratch(Vector2 touchPosition)
    {
        // 获取当前遮罩的Texture2D
        Texture2D currentMaskTexture = new Texture2D(maskRenderer.sprite.texture.width, maskRenderer.sprite.texture.height);
        currentMaskTexture.SetPixels(maskRenderer.sprite.texture.GetPixels());

        // 计算刮擦矩形区域
        int xMin = Mathf.FloorToInt(touchPosition.x * currentMaskTexture.width - brushSize / 2);
        int xMax = Mathf.CeilToInt(touchPosition.x * currentMaskTexture.width + brushSize / 2);
        int yMin = Mathf.FloorToInt(touchPosition.y * currentMaskTexture.height - brushSize / 2);
        int yMax = Mathf.CeilToInt(touchPosition.y * currentMaskTexture.height + brushSize / 2);

        // 限制刮擦区域在纹理内
        xMin = Mathf.Clamp(xMin, 0, currentMaskTexture.width);
        xMax = Mathf.Clamp(xMax, 0, currentMaskTexture.width);
        yMin = Mathf.Clamp(yMin, 0, currentMaskTexture.height);
        yMax = Mathf.Clamp(yMax, 0, currentMaskTexture.height);

        // 对刮擦区域设置透明色
        for (int x = xMin; x < xMax; x++)
        {
            for (int y = yMin; y < yMax; y++)
            {
                currentMaskTexture.SetPixel(x, y, Color.clear);
            }
        }

        // 应用刮擦结果
        currentMaskTexture.Apply();
        maskRenderer.sprite = Sprite.Create(currentMaskTexture, new Rect(0, 0, currentMaskTexture.width, currentMaskTexture.height), Vector2.one * 0.5f);
    }
}

  • 15
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南無忘码至尊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值