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

linerender的标记参考

unity linerenderer在Game窗口中任意画线_游戏内编辑linerender-CSDN博客

让生成的标记转化为ARMarks游戏对象的子物体

LineMark.cs

cs 复制代码
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);//设置宽度  
			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");

		}

	}
}
相关推荐
SmalBox2 小时前
【URP】Shader绘制棋盘格对比内置管线
unity·渲染
郝学胜-神的一滴1 天前
基于OpenGL封装摄像机类:视图矩阵与透视矩阵的实现
c++·qt·线性代数·矩阵·游戏引擎·图形渲染
EQ-雪梨蛋花汤1 天前
【Unity笔记】Unity 编辑器扩展:打造一个可切换 Config.assets 的顶部菜单插件
unity·编辑器·游戏引擎
SmalBox1 天前
【URP】UnityHLSL顶点片元语义详解
unity·渲染
在路上看风景2 天前
9. Mono项目与Unity的关系
unity
在路上看风景2 天前
1.12 Memory Profiler Package - Summary
unity
SmalBox2 天前
【URP】Unity Shader Tags
unity·渲染
极客柒2 天前
Unity 塔防自用可视化路点寻路编辑器
unity·编辑器·游戏引擎
程序猿多布2 天前
Unity AssetBundle详解
unity·assetbundle
萘柰奈2 天前
Unity学习----【进阶】Addressables(二)--加载资源与打包及更新
学习·unity