当前位置: 首页 > news >正文

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

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

// 当前正在绘制的 LineRendererprivate 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);// 增加顶点到当前的 LineRenderercurrentLineRenderer.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 组件!");}}

相关文章:

  • 【网络编程】网络编程基础和Socket套接字
  • AF3 generate_chain_data_cache脚本解读
  • TDengine 与其他时序数据库对比:InfluxDB/TimescaleDB 选型指南(一)
  • AI智能体小结
  • 主流Embedding模型优劣势解析与技术选型指南(2025年4月)
  • npm和npx的作用和区别
  • 数据服务化 VS 数据中台:战略演进中的价值重构
  • 1×1卷积与GoogleNet
  • 天润融通AI Agent重塑零售连锁行业客户服务竞争力
  • 【行业树选择器组件:基于Vue3与Element Plus的高性能树形选择组件优化与重构】
  • 微服务1--服务架构
  • 计算机视觉算法实现——疲劳驾驶检测
  • Windows 下实现 PHP 多版本动态切换管理(适配 phpStudy)+ 一键切换工具源码分享
  • 基于JSP+MySQL实现用户注册登录及短信发送功能
  • Swift —— delegate 设计模式
  • 宝塔面板面试内容整理-Web服务器优化
  • Google最新《Prompt Engineering》白皮书全解析
  • 多线程、JUC——面试问题自我总结
  • 住宅代理是什么?2025详细指南
  • 基于前端技术的QR码API开发实战:从原理到部署
  • 美国季度GDP时隔三年再现负增长,特朗普政府关税政策对美国经济负面影响或将持续
  • 美乌矿产协议预计最早于今日签署
  • “上博号”彩绘大飞机今日启航:万米高空传播中国古代文化
  • “五一”假期逛上海车展请提前购票,展会现场不售当日票
  • 黄宾虹诞辰160周年|一次宾翁精品的大集结
  • 阿迪达斯一季度营收增近13%,称美国加征关税对业绩带来不确定性