一.学习的知识1.LineRenderer的用法 以及点击事件
二.画图代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Draw : MonoBehaviour,IPointerDownHandler,IPointerUpHandler,IPointerExitHandler,IPointerEnterHandler
{
LineRenderer line;
bool fg = false;
bool fgs = false;
int inedex = 0;
Vector3 vector;
public void OnPointerDown(PointerEventData eventData)
{
line = new GameObject().AddComponent<LineRenderer>();
line.material = new Material(Shader.Find("Sprites/Default"));
fg = true;
line.startColor = Color.red;
line.endColor = Color.yellow;
line.startWidth = 0.05f;
line.endWidth = 0.05f;
}
public void OnPointerUp(PointerEventData eventData)
{
fg = false;
inedex = 0;
line = null;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(line!=null&&fg&&fgs)
{
vector= Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,1));
line.positionCount = inedex+1;
line.SetPosition(inedex, vector);
inedex++;
}
}
public void OnPointerExit(PointerEventData eventData)
{
fgs = false;
}
public void OnPointerEnter(PointerEventData eventData)
{
fgs = true;
}
}