当前位置:网站首页>Unity 实现简单画板画画功能(笔记)
Unity 实现简单画板画画功能(笔记)
2022-07-27 21:05:00 【X-q-X】
一、搭好场景
- 创建一个plan
- 摄像机调成正交摄像机
- 创建三个cube,附上材质
- 再创建一个cube,找个色板,
- 创建一个脚本,挂在非失活物体上
- 创建一个slider,放在旁边

二、写代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Draw : MonoBehaviour {
LineRenderer line;
Material mat;
public Slider slider;
int num = 0;//总共画画点数
Color c;
// Use this for initialization
void Start () {
slider.value = 0.1f;
}
// Update is called once per frame
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit))
{
if (Input.GetMouseButtonDown(0))
{
if (c==null)
{
return;
}
GameObject obj = new GameObject();
line= obj.AddComponent<LineRenderer>();
line.material.color= c;
line.widthMultiplier = slider.value;//宽度
line.SetPosition(0,hit.point);
line.SetPosition(1, hit.point);
num = 0;
}
if (Input.GetMouseButton(0))
{
num++;
line.positionCount = num;
line.SetPosition(num - 1, hit.point+Vector3.up*0.2f);
}
if (Input.GetMouseButtonDown(1))
{
StartCoroutine(ChangeColor());
}
}
}
IEnumerator ChangeColor()
{
yield return new WaitForEndOfFrame();
Texture2D texture2D = new Texture2D(Screen.width,Screen.height,TextureFormat.RGB24,true);
texture2D.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture2D.Apply();
c = texture2D.GetPixel((int)Input.mousePosition.x, (int)Input.mousePosition.y);
}
}
三、效果
Unity 画板
边栏推荐
- Using the optical fingerprint scheme under the huiding screen, Samsung Galaxy a71 5g is listed
- 虚拟存储器与Cache的比较
- Redis hash underlying data structure
- [C language] address book (dynamic version)
- 采用汇顶屏下光学指纹方案,三星Galaxy A71 5G上市
- Technical certification | Tupo software and Huawei cloud create a new situation of win-win cooperation
- Tita 的OKR系统与其他同类型产品,或者是共享文档等相比,有什么优势?
- 2022/7/26
- File & recursion 14.1
- Lua基础语法学习
猜你喜欢

Common Taylor expansion
![[number recognition] handwritten number recognition based on knowledge base with matlab code](/img/06/6adab955a339f453249543baab1dc6.png)
[number recognition] handwritten number recognition based on knowledge base with matlab code

详解分布式系统的幂等

CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021

【12月海口】2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)

Is it really hard to understand? What level of cache is the recyclerview caching mechanism?

华为鸿蒙 3 正式发布,这个安全功能解决了一大痛点

NDK series (6): let's talk about the way and time to register JNI functions

Explain the idempotence of distributed system in detail

NDK series (6): let's talk about the way and time to register JNI functions
随机推荐
Nature review: preferential effects in the formation of microbial communities
总投资600亿!富士康半导体高端封测项目正式落户青岛
Character stream learning 14.3
真的很难理解?RecyclerView 缓存机制到底是几级缓存?
技术认证 | 图扑软件携手华为云再创合作共赢新局面
字符流学习14.3
smartRefresh嵌套多个RecycleView滑动冲突及布局显示不全
Find and leverage xxE – XML external entity injection
Those "experiences and traps" in the data center
MySQL之数据查询(WHERE)
使用灰度滤镜
[signal denoising] signal denoising based on Kalman filter with matlab code
Elk log analysis system installation and deployment
Learn more about xxE injection
[image defogging] image defogging based on dark channel and non-mean filtering with matlab code
详解分布式系统的幂等
Binary conversion method
日产1500万只!比亚迪口罩拿下美国加州10亿美元订单
Spark 离线开发框架设计与实现
Normality of confidence interval