Unity 一些小功能(屏幕画画,)

利用 Line Renderer 实现屏幕画画并保存图片

cs 复制代码
// 当前正在绘制的 LineRenderer
    private LineRenderer currentLineRenderer;
    // 用于保存所有笔触的列表
    private List<LineRenderer> allLineRenderers = new List<LineRenderer>();
    // 当前笔触顶点计数器
    private int vertexCount = 0;
    // 鼠标是否处于按下状态
    private bool mouseDown = false;

    // 用于展示生成图片的 UI Image(需要在 Inspector 中绑定)
    public Image displayImage;

    private Color currentColor = Color.black;

    public void ChangeColor(string colorStr)
    {
        if(colorStr == "Red")
        {
            currentColor = Color.red;
        }
        else if(colorStr == "black")
        {
            currentColor = Color.black;
        }
        else if(colorStr == "blue")
        {
            currentColor = Color.blue;
        }
    }

    public void SavePicture()
    {
        StartCoroutine(CaptureAndSetSprite());
    }

    void Update()
    {
        // 鼠标左键按下时开始新的一笔
        if (Input.GetMouseButtonDown(0))
        {
            StartNewLine();
            mouseDown = true;
        }

        // 鼠标左键松开时结束当前绘制
        if (Input.GetMouseButtonUp(0))
        {
            mouseDown = false;
        }

        // 鼠标按下期间实时添加顶点
        if (mouseDown)
        {
            // 将鼠标屏幕坐标转换为世界坐标
            // 此处 z 的值决定平面距离摄像机的距离(根据实际需求调整)
            Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 3f);
            pos = Camera.main.ScreenToWorldPoint(pos);

            // 增加顶点到当前的 LineRenderer
            currentLineRenderer.positionCount = vertexCount + 1;
            currentLineRenderer.SetPosition(vertexCount, pos);
            vertexCount++;
        }
    }

    /// <summary>
    /// 每次开始新的一笔时,创建一个新的 LineRenderer,保证之前的绘制不会被清除
    /// </summary>
    void StartNewLine()
    {
        // 新建一个 GameObject 并添加 LineRenderer 组件
        GameObject lineObj = new GameObject("LineStroke");
        // 将新生成的对象设为当前对象的子物体,便于管理
        lineObj.transform.parent = this.transform;
        currentLineRenderer = lineObj.AddComponent<LineRenderer>();

        // 设置基本属性(宽度、材质、颜色等),可根据需求调整
        currentLineRenderer.widthCurve = AnimationCurve.Constant(0, 1, 0.05f);
        currentLineRenderer.positionCount = 0;
        currentLineRenderer.material = new Material(Shader.Find("Sprites/Default"));
        currentLineRenderer.startColor = currentColor;
        currentLineRenderer.endColor = currentColor;

        // 添加到列表中便于管理后续可能的操作
        allLineRenderers.Add(currentLineRenderer);
        // 重置当前笔触顶点计数器
        vertexCount = 0;
    }

    /// <summary>
    /// 协程:在当前帧渲染结束后,捕获屏幕内容,
    /// 并将生成的图片转为 Sprite,然后赋给 UI Image 组件进行展示
    /// </summary>
    IEnumerator CaptureAndSetSprite()
    {
        // 等待这一帧结束,确保所有渲染都完成
        yield return new WaitForEndOfFrame();

        // 获取当前屏幕尺寸
        int width = Screen.width;
        int height = Screen.height;
        // 新建一个 Texture2D,用于存储截屏数据
        Texture2D texture = new Texture2D(width, height, TextureFormat.RGB24, false);
        // 读取屏幕像素数据
        texture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        texture.Apply();

        // 创建 Sprite:Rect 表示截取的区域,锚点通常设为中心 (0.5, 0.5)
        Sprite newSprite = Sprite.Create(texture, new Rect(0, 0, width, height), new Vector2(0.5f, 0.5f));

        // 将生成的 Sprite 赋给 UI Image 组件进行展示
        if (displayImage != null)
        {
            displayImage.sprite = newSprite;
        }
        else
        {
            Debug.LogWarning("displayImage 没有绑定 UI Image 组件!");
        }
    }
相关推荐
weixin_424294675 天前
Unity 调用Steamworks API 的 SteamUserStats.RequestCurrentStats()报错
unity·游戏引擎·steamwork
HoFunGames5 天前
Unity小地图,Easy Minimap System MT-GPS插件
unity·游戏引擎
wy3258643645 天前
Unity 新输入系统InputSystem(基本操作)
unity·c#·游戏引擎
WarPigs5 天前
着色器multi_compile笔记
unity·着色器
ECHO飞跃 0125 天前
Unity2019 本地推理 通义千问0.5-1.5B微调导入
人工智能·深度学习·unity·llama
Unity游戏资源学习屋5 天前
【Unity UI资源包】GUI Pro - Casual Game 专为休闲手游打造的专业级UI资源包
ui·unity
冰凌糕5 天前
Unity3D Shader 顶点法线外扩实现描边效果
unity
小菱形_5 天前
【Unity】TimeLine
unity·游戏引擎
小贺儿开发6 天前
Unity3D 自动化物流分拣模拟
运维·科技·unity·自动化·人机交互·传送带·物流分拣
EQ-雪梨蛋花汤6 天前
【3D可视化】基于 Unity 的智慧体育馆三维信息可视化大屏实践
3d·unity·信息可视化