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

网站要求wordpress域名查询站长之家

网站要求wordpress,域名查询站长之家,wordpress标签logo,外链 推网站怎么做笔刷大小调节 上面的代码中其实我们已经提供了笔刷大小的字段,即brushSize,现在只需要将该字段和界面中的Slider绑定即可,Slider值的范围我们设置为1~20 代码中只需要做如下改动: public Slider brushSizeSlider; //控制笔刷大…

笔刷大小调节

上面的代码中其实我们已经提供了笔刷大小的字段,即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.tj-hxxt.cn/news/80099.html

相关文章:

  • 如何计算网站pv关键词优化一般收费价格
  • 搜索热门关键词网站推广seo
  • 家居企业网站建设策划引擎网站
  • 网站建设合同书模板seo网站优化师
  • 单位建设网站申请信用卡吗收录优美图片topit
  • 孔家庄网站建设aso优化报价
  • 公司做网站需要提供什么条件公司想做个网站怎么办
  • c# 网站开发实例教程淘宝运营培训课程
  • 什么是网站内容建设每日新闻快报
  • 类似于凡科的网站网络营销课程感悟
  • 中国建设银行网站的社保板块在哪里如何做好线上推广和引流
  • 如何撰写网站建设方案优化网络
  • 受欢迎的网站建设平台营销手段有哪些
  • 网站ui用什么做网络工程师是干什么的
  • 创立网站做电商百度网络推广
  • 英国做deal的网站b2b电商平台有哪些
  • 做用户名和密码网站页面seo内容优化心得
  • 百度一下浏览器乐陵seo优化
  • 品牌策划公司属于什么行业小果seo实战培训课程
  • 政府网站的构建与运作seo会被取代吗
  • 制作网站的公司哪家比较好广州网站推广软件
  • 做医药代表去什么招聘网站郑州网站seo外包
  • 网站用什么软件做软文营销方法有哪些
  • 电商类网站建设价格网站制作 网站建设
  • WordPress邀请码功能seo推广价格
  • 外包做网站不付尾款网络推广外包一年多少钱
  • 上海天权信息科技有限公司点击排名优化
  • 对用户1万的网站做性能测试百度推广账户优化方案
  • 网站ftp密码怎么修改磁力bt种子搜索
  • 做网站的接私活犯法吗seo优化对网店的推广的作用为