• 大小: 8.63MB
    文件类型: .7z
    金币: 1
    下载: 0 次
    发布日期: 2023-08-22
  • 语言: 其他
  • 标签: Unity3d  C#  Manager  

资源简介

在图片上绘制颜色,可实现画笔功能和刮刮乐效果,工程里有DEMO直接使用就行了,,,Demo是我用来给客户看效果的,做了就分享下!

资源截图

代码片段和文件信息

using UnityEngine;

namespace FreeDraw
{
    // 1. 将其附加到读/写的sprite图像上
    // 2. 将绘图层设置为在光线投射中使用
    // 3. 将绘图层设置为在光线投射中使用
    // 4. 按住鼠标左键画出这个纹理!
    public class Drawable : MonoBehaviour
    {
        [Tooltip(“彩笔颜色【橡皮擦就是透明彩笔】“)] 
        public Color Pen_Colour = new Color(255f 255f 255f 0f);
        [Tooltip(“钢笔宽度(实际上,它是一个半径,以像素为单位)“)]
        public int Pen_Width = 20;
        [Tooltip(“图层面板“)]
        public layerMask Drawing_layers;

        // 在Unity的文件编辑器中必须有读/写权限
        Sprite drawable_sprite;
        Texture2D drawable_texture;

        Vector2 previous_drag_position;
        Color[] clean_colours_array;
        Color transparent;
        Color32[] cur_colors;
        bool mouse_was_previously_held_down = false;
        bool no_drawing_on_current_drag = false;


        void Awake()
        {
            drawable_sprite = this.GetComponent().sprite;
            drawable_texture = drawable_sprite.texture;

            // 保存图片所有颜色
            clean_colours_array = drawable_texture.GetPixels(0 0 1920 1080);

        }


        void Update()
        {
            // 用户按住鼠标左键吗?
            bool mouse_held_down = Input.GetMouseButton(0);
            if (mouse_held_down && !no_drawing_on_current_drag)
            {
                // 将鼠标坐标转换为世界坐标
                Vector2 mouse_world_position = Camera.main.ScreenToWorldPoint(Input.mousePosition);

                // 检查当前鼠标位置是否与我们的图像重叠
                Collider2D hit = Physics2D.OverlapPoint(mouse_world_position Drawing_layers.value);
                if (hit != null && hit.transform != null)
                    // 我们已经超越了我们所描绘的纹理!改变像素的颜色
                    ChangeColourAtPoint(mouse_world_position);
                else
                {
                    // 我们还没有结束我们的目标纹理
                    previous_drag_position = Vector2.zero;
                    if (!mouse_was_previously_held_down)
                    {
                        //这是一个新的拖动用户在画布上点击离开
                        //在新的阻力开始之前,确保不会发生绘图
                        no_drawing_on_current_drag = true;
                    }
                }
            }
            // 鼠标释放
            else if (!mouse_held_down)
            {
                previous_drag_position = Vector2.zero;
                no_drawing_on_current_drag = false;
            }
            mouse_was_previously_held_down = mouse_held_down;
        }


        // 在世界坐标中传递一个点
        // 改变世界周围的像素点指向静态的钢笔颜色
        public void ChangeColourAtPoint(Vector2 world_point)
        {
            // 改变坐标到这个图像的局部坐标
            Vector3 local_pos = transform.InverseTransformPoint(world_point);

            // 将这些转换为像素坐标
            float pixelWidth = drawable_sprite.rect.width;
            float pixelHeight = drawable_sprite.rect.height;
            float unitsToPixels = pixelWidth / drawable_sprite.bounds.size.x * transform.localScale.x;

            // 需要把我们的坐标居中
            f

评论

共有 条评论