根据点击触摸画线
代码中有一个物体是拥有Linedrend组件的,自行设置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
public class TouchListener : MonoBehaviour, IPointerDownHandler, IPointerExitHandler
{
private Image image;
public Text text;
private bool isEnter = false;
private bool isSlider = false;
//带有LineRender物体
public GameObject target;
public void OnPointerDown(PointerEventData eventData)
{
if (isEnter == false)
isEnter = true;
}
public void OnPointerExit(PointerEventData eventData)
{
if (isSlider == true)
{
isEnter = false;
isSlider = false;
int count = Convert.ToInt32(text.text) + 1;
if (count >= 10)
return;
text.text = count.ToString();
StartCoroutine(DeleteLine());
}
}
void Start()
{
image = GetComponent<Image>();
text = UnityHelp.GettheChildNodeComponentScripts<Text>(this.gameObject, "Text");
target = GameObject.Find("GameObject");
}
private LineRenderer line;
int i = 0;
// Update is called once per frame
void Update()
{
//if (isEnter && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
if (isEnter && Input.GetMouseButton(0))
{
isSlider = true;
line = target.GetComponent<LineRenderer>();
line.startColor = Color.white;
line.endColor = Color.white;
line.startWidth = 0.5f;
line.endWidth = 0.5f;
i++;
// 设置顶点数
line.positionCount = i;
//设置顶点位置(顶点的索引,将鼠标点击的屏幕坐标转换为世界坐标)
//line.SetPosition(i - 1, Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 15)));
line.SetPosition(i - 1, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15)));
}
}
IEnumerator DeleteLine()
{
yield return new WaitForSeconds(2.0f);
line.positionCount = 0;
i = 0;
}
}