using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class SharedARManager : MonoBehaviour
{
private bool istouchUI;
public GameObject linePrefab;
public float distance = 0.005f;
private LineRenderer currentLine;
private float lineWidth = 0.01f;
public Color lineColor;
private List
private List
private int lineVertexIndex = 2;
private bool isMouseDown;
private Vector3 lastposition;
// Use this for initialization
void Start()
{
Slider slider = GameObject.Find("Canvas/Slider").GetComponent
slider.onValueChanged.AddListener(onSliderValueChanged);
Button button1 = GameObject.Find("Canvas/Button1").GetComponent
button1.onClick.AddListener(onClickButton1);
Button button2 = GameObject.Find("Canvas/Button2").GetComponent
button2.onClick.AddListener(onClickButton2);
}
void onSliderValueChanged(float temp)
{
lineWidth = temp / 10.0f;
}
void onClickButton1()
{
RemoveAllLine();
}
void onClickButton2()
{
CancelOneLine();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
{
#if IPHONE || ANDROID
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
if (EventSystem.current.IsPointerOverGameObject())
#endif
istouchUI = true;//Debug.Log("当前触摸在UI上");
else
istouchUI = false;//Debug.Log("当前没有触摸在UI上");
}
if (Input.GetMouseButtonDown(0) && !istouchUI)
{
// GameObject go = new GameObject("SharedARLineRenderer");
// go.transform.SetParent(gameObject.transform);
// currentLine = go.AddComponent
currentLine = Instantiate(linePrefab).GetComponent
currentLine.transform.parent = transform;
currentLine.material = new Material(Shader.Find("Sprites/Default"));
currentLine.startWidth = lineWidth;
currentLine.endWidth = lineWidth;
currentLine.startColor = lineColor;
currentLine.endColor = lineColor;
currentLine.numCornerVertices = 5;
currentLine.numCapVertices = 5;
Vector3 position = GetMousePosition();
// Debug.Log(position);
lineVertexIndex = 2;
currentLine.SetPosition(0, position);
currentLine.SetPosition(1, position);
lastposition = position;
linesList.Add(currentLine.gameObject);
isMouseDown = true;
}
if (Input.GetMouseButton(0))
{
StartCoroutine(DrawLines());
}
if (Input.GetMouseButtonUp(0))
{
isMouseDown = false;
}
}
Vector3 GetMousePosition()
{
#if UNITY_EDITOR
Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f));
return point;
#else
#endif
}
IEnumerator DrawLines()
{
while (isMouseDown && !istouchUI)
{
yield return new WaitForEndOfFrame();
Vector3 position = GetMousePosition();
// Debug.Log(position);
if (Vector3.Distance(position, lastposition) > distance)
{
lineVertexIndex++;
currentLine.positionCount = lineVertexIndex;
currentLine.SetPosition(lineVertexIndex - 1, position);
lastposition = position;
}
}
}
public void CancelOneLine()
{
if (linesList.Count > 0)
{
Destroy(linesList[linesList.Count - 1]);
linesList.RemoveAt(linesList.Count - 1);
}
}
public void RemoveAllLine()
{
for (int i = 0; i < transform.childCount; i++)
{
Destroy(transform.GetChild(i).gameObject);
}
linesList.Clear();
}
}