一、新建一个项目
然后创建一个GameObjek对象,然后挂上脚本;
二、创建LineRender对象
三、建立背景
创建一个Quad,在创建一个黑色的材质球,添加到Quad上,把Quad的sizd属性设为10.10.10,让其充满Game窗口整个屏幕。
四、创建一个贴图
可以为自己的笔画添加贴图,让其展现不同的图片效果。
首先添加一个材质球,然后把它的属性设为Particles/Additive;
然后点击预制体,在Line Renderer组件招到Materials,把刚才的贴图材质添加进去即可,想为材质添加什么图片都可以。
然后运行就可以开始画了,效果如下:
脚本代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
public GameObject linePrefab;
List<GameObject> Lines;
GameObject nowline;
// Use this for initialization
void Start () {
Lines = new List<GameObject>();
}
void write()
{
nowline.GetComponent<LineRenderer>().positionCount += 1;
nowline.GetComponent<LineRenderer>().SetPosition(
nowline.GetComponent<LineRenderer>().positionCount - 1,
Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1f))
);
}
// Update is called once per frame
void creatNewLine()
{
nowline = Instantiate(linePrefab, transform.position, new Quaternion(), this.transform);
Lines.Add(nowline);
//设置颜色
nowline.GetComponent<LineRenderer>().startColor = Color.green;
nowline.GetComponent<LineRenderer>().endColor = Color.yellow;
//设置宽度
nowline.GetComponent<LineRenderer>().startWidth = 0.04f;
nowline.GetComponent<LineRenderer>().endWidth = 0.04f;
nowline.GetComponent<LineRenderer>().positionCount = 0;
InvokeRepeating("write", 0, 0.01f);
}
void Update () {
if(Input.GetMouseButtonDown(0))
{
creatNewLine();
}
if (Input.GetMouseButtonDown(1))
{
if(Lines.Count>=1)
{
Destroy(Lines[Lines.Count - 1]);
Lines.RemoveAt(Lines.Count - 1);
}
}
if (Input.GetMouseButtonUp(0))
{
CancelInvoke("write");
}
}
}