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");

		}

	}
}
相关推荐
老朱佩琪!2 小时前
Unity备忘录模式
java·unity·备忘录模式
一帘多啦A梦3 小时前
解决unity2022.3.x版本项目使用vs无法生成解决方案的问题
unity·vs
weixin_424294675 小时前
Unity LocalPosition 和 Position 的区别?还有其他的Position 没?
unity·游戏引擎
UX20175 小时前
Git LFS 管理 Unity 大文件
git·unity
nnsix6 小时前
Unity WebGL端调用Windows窗口选择文件
unity·游戏引擎·webgl
向宇it6 小时前
【unity游戏开发——网络】使用Unity+PurrNet+Heathens+Steam,在 Unity 中通过 Steam与你的朋友建立联系
网络·游戏·unity·c#·游戏引擎·steam
nnsix6 小时前
Unity 反编译dll(Windows平台)
unity
XR技术研习社7 小时前
四种安装特定版本Package的方法
unity·ar·xr·vr
dzj20218 小时前
Unity行为脚本与编辑器脚本的解耦例子之一
unity·解耦·编辑器脚本·行为脚本
技术小甜甜9 小时前
【Godot】【入门】编辑器界面速通:场景/节点/Inspector/信号(30 分钟上手不迷路)
编辑器·游戏引擎·godot