Unity开发绘画板——04.笔刷大小调节

ops/2024/10/19 3:15:55/

笔刷大小调节

上面的代码中其实我们已经提供了笔刷大小的字段,即brushSize,现在只需要将该字段和界面中的Slider绑定即可,Slider值的范围我们设置为1~20

代码中只需要做如下改动:

public Slider brushSizeSlider; //控制笔刷大小的滑动条
public float brushSize => brushSizeSlider.value;    //笔刷大小取滑动条的值

完整代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class Painter : MonoBehaviour
{public RenderTexture renderTexture;public Slider brushSizeSlider;public Texture2D brushTexture; //笔刷纹理public float brushSize => brushSizeSlider.value;    //笔刷大小public float resolutionMultiplier = 5;  //线性插值密度调节private Vector2 previousMousePos; //记录上一帧鼠标的位置  void Update(){if (Input.GetMouseButtonDown(0)){previousMousePos = Input.mousePosition;}if (Input.GetMouseButton(0)){var mousePosition = Input.mousePosition;DrawLine(previousMousePos, mousePosition);previousMousePos = mousePosition;}}private void DrawLine(Vector2 start, Vector2 end){float distance = Vector2.Distance(start, end);int steps = Mathf.CeilToInt(distance * resolutionMultiplier);for (int i = 0; i <= steps; i++){float t = i / (float)steps;int x = Mathf.FloorToInt(Mathf.Lerp(start.x, end.x, t));int y = Mathf.FloorToInt(Mathf.Lerp(start.y, end.y, t));DrawBrush(x, y);}}private void DrawBrush(int x, int y){Rect brushRect = new Rect(x, y, brushSize, brushSize);Graphics.SetRenderTarget(renderTexture);GL.PushMatrix();GL.LoadPixelMatrix(0, renderTexture.width, 0, renderTexture.height);Graphics.DrawTexture(brushRect, brushTexture);GL.PopMatrix();Graphics.SetRenderTarget(null);}}

效果优化 

运行效果如下图,可见虽然笔刷粗细已可以调节,但是在调节过程中,滑动的同时滑动条周围有绘制的线条,这本不该出现的,出现的原因是因为我们监听了鼠标按下的事件来绘制,这导致在操作滑动条的时候,绘制依然在进行。

操作UI时屏蔽绘制

我们需要检测当前的鼠标是否在UI元素上,如果在,则不进行绘制,这可以使用EventSystem的射线检测来实现,如下我们定义一个函数,当光标在UI元素上的时候返回true,否则返回false:

private bool IsPointerOverUIElement()
{PointerEventData eventData = new PointerEventData(EventSystem.current);eventData.position = Input.mousePosition;List<RaycastResult> results = new List<RaycastResult>();EventSystem.current.RaycastAll(eventData, results);return results.Count > 0;
}

我们在鼠标左键点击的时候判断是否在UI元素上,如果在则禁止绘制;

但有个问题,我们用来绘制的RawImage也是UI元素,它铺满整个屏幕,这会导致我们始终没办法绘制,我们可以将其Raycast Target关掉,这样再操作UI元素的时候就不会进行多余的绘制了

完整代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;public class Painter : MonoBehaviour
{public RenderTexture renderTexture;public Slider brushSizeSlider; //控制笔刷大小的滑动条public Texture2D brushTexture; //笔刷纹理public float brushSize => brushSizeSlider.value;    //笔刷大小取滑动条的值public float resolutionMultiplier = 5;  //线性插值密度调节private Vector2 previousMousePos; //记录上一帧鼠标的位置  private bool startFromUIElement = false; //点击是否是从UI元素上开始的void Update(){//判断光标是否在UI元素上if (Input.GetMouseButtonDown(0)){if (IsPointerOverUIElement()){startFromUIElement = true;}previousMousePos = Input.mousePosition;}if (Input.GetMouseButton(0) && !startFromUIElement){var mousePosition = Input.mousePosition;DrawLine(previousMousePos, mousePosition);previousMousePos = mousePosition;}if (Input.GetMouseButtonUp(0)){startFromUIElement = false;}}private void DrawLine(Vector2 start, Vector2 end){float distance = Vector2.Distance(start, end);int steps = Mathf.CeilToInt(distance * resolutionMultiplier);for (int i = 0; i <= steps; i++){float t = i / (float)steps;int x = Mathf.FloorToInt(Mathf.Lerp(start.x, end.x, t));int y = Mathf.FloorToInt(Mathf.Lerp(start.y, end.y, t));DrawBrush(x, y);}}private void DrawBrush(int x, int y){Rect brushRect = new Rect(x, y, brushSize, brushSize);Graphics.SetRenderTarget(renderTexture);GL.PushMatrix();GL.LoadPixelMatrix(0, renderTexture.width, 0, renderTexture.height);Graphics.DrawTexture(brushRect, brushTexture);GL.PopMatrix();Graphics.SetRenderTarget(null);}private bool IsPointerOverUIElement(){PointerEventData eventData = new PointerEventData(EventSystem.current);eventData.position = Input.mousePosition;List<RaycastResult> results = new List<RaycastResult>();EventSystem.current.RaycastAll(eventData, results);return results.Count > 0;}}

在下一节我将为大家介绍如何调节线条的颜色~


http://www.ppmy.cn/ops/119883.html

相关文章

javax.net.ssl.SSLHandshakeException: Chain validation failed

异常描述&#xff1a; D/OkHttp: <-- HTTP FAILED: javax.net.ssl.SSLHandshakeException: Chain validation failed com.bfmd.okhttpsample I/Main: error: Chain validation failed异常解决&#xff1a; 解决方法一&#xff1a; 解决方法很简单&#xff0c;检查一下设备…

十七、触发器

文章目录 0. 引入1. 触发器概述2. 触发器的创建2.1 触发器的创建2.2 代码举例 3. 查看、删除触发器3.1 查看触发器3.2 删除触发器 4. 触发器的优缺点4.1 优点4.2 缺点4.3 注意点 0. 引入 在实际开发中&#xff0c;我们经常会遇到这样的情况&#xff1a;有 2 个或者多个相互关联…

物联网智能项目研究

物联网&#xff08;IoT&#xff09;作为当今数字化转型的重要推动力&#xff0c;正在改变我们的生活方式和工作模式。从智能家居、智慧城市到工业自动化&#xff0c;物联网技术的应用正在实现人们对智能生活的向往。本文将探讨一个具体的物联网智能项目&#xff0c;通过实际操作…

vue2与vue3知识点

1.vue2&#xff08;optionsAPI&#xff09;选项式API 2.vue3&#xff08;composition API&#xff09;响应式API vue3 setup 中this是未定义&#xff08;undefined&#xff09;vue3中已经开始弱化this vue2通过this可以拿到vue3setup定义得值和方法 setup语法糖 ref > …

3. 轴指令(omron 机器自动化控制器)——>MC_MoveFeed

机器自动化控制器——第三章 轴指令 8 MC_MoveFeed变量▶输入变量▶输出变量▶输入输出变量 功能说明▶指令详情▶时序图▶重启运动指令▶多重启动运动指令▶异常 示例程序▶参数设定▶动作示例▶梯形图▶结构文本(ST) MC_MoveFeed 指定自外部输入的中断输入发生位置起的移动距…

常用设计模式之单例模式、策略模式、工厂模式

单例模式 单例模式属于创建型模式 饿汉模式&#xff1a;立即加载 public class Singleton { private static Singleton instance new Singleton(); private Singleton (){} public static Singleton getInstance() { return instance; } } 懒汉模式&#xff0c;懒加…

推荐一个可以把PDF样本册转换为翻页电子书的网站

​随着互联网的普及&#xff0c;越来越多的企业和个人开始意识到线上展览的重要性。如何将实体样本册转化为线上版本&#xff0c;让更多人了解和欣赏自己的产品与服务&#xff1f; 一、网站简介 这款PDF样本册免费上传网站名为“FLBOOK”&#xff0c;致力于为广大用户提供便捷…