unity鼠标标记生成与清楚,一次只清除一个

unity 鼠标标记 左键长按生成标记右键长按清除标记,对象转化为子物体-CSDN博客

将一次清除所有标记修改为一次只清除一个,从最后一个开始。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LineMark : MonoBehaviour
{

	private GameObject clone;
	private LineRenderer line;
	private int i;

	public GameObject obs;
	public GameObject arMarks;

	// Use this for initialization
	void Start()
	{
		//arMarks = new GameObject("ARMarks");
	}

	// Update is called once per frame  
	void Update()
	{
		//0是鼠标左键  按下时重新生成标记
		if (Input.GetMouseButtonDown(0))
		{
			clone = (GameObject)Instantiate(obs, obs.transform.position, transform.rotation);//克隆一个带有LineRender的物体   
			line = clone.GetComponent<LineRenderer>();//获得该物体上的LineRender组件  
			line.SetColors(Color.blue, Color.red);//设置颜色  
            //line.SetWidth(0.2f, 0.1f);//设置宽度  
            line.SetWidth(2.0f, 2.0f);//设置宽度  
            i = 0;

			//将标记设为子物体
			print("11111111111111111");

		}
		//长按左键绘制标记
		if (Input.GetMouseButton(0))
		{
			i++;
			line.SetVertexCount(i);//设置顶点数  
			line.SetPosition(i - 1, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15)));//设置顶点位置   

			//GameObject lines_mark = GameObject.Find(clone.name);
			clone.layer = 3;
			clone.transform.parent = arMarks.transform;
			print("222222222222222222222");

		}
		//1是鼠标右键  按右键清除标记
		//if (Input.GetMouseButton(1))
  //      {
		//	//         for (int i = 0; i < arMarks.transform.childCount; i++)
		//	//         {
		//	//	Destroy(arMarks.transform.GetChild(i).gameObject);
		//	//         }
		//	//print("33333333333333333333333");


		//	
		//	///因为一次按下后要执行几十帧所以这样也可以清除
		//	if (arMarks.transform.childCount > 0)
  //          {
		//		print(arMarks.transform.childCount);
		//		Destroy(arMarks.transform.GetChild(arMarks.transform.childCount - 1).gameObject);
		//	}
		//	print("hello");

		//}

		//当鼠标右键按下时清楚一个
		if (Input.GetMouseButtonDown(1))
		{

			if (arMarks.transform.childCount > 0)
			{
				print(arMarks.transform.childCount);
				Destroy(arMarks.transform.GetChild(arMarks.transform.childCount - 1).gameObject);
			}
			print("hello");

		}

		//可以一次清除一个标记,从最后一个开始
		if (Input.GetKeyDown(KeyCode.A))
        {
			if (arMarks.transform.childCount > 0)
			{
				print(arMarks.transform.childCount);
				Destroy(arMarks.transform.GetChild(arMarks.transform.childCount - 1).gameObject);
			}
		}

	}
}

猜你喜欢

转载自blog.csdn.net/moonlightpeng/article/details/134077767