一、搭好场景
- 创建一个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 画板文章来源地址https://www.toymoban.com/news/detail-552799.html
文章来源:https://www.toymoban.com/news/detail-552799.html
到了这里,关于Unity 实现简单画板画画功能(笔记)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!