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

		}

	}
}
相关推荐
山檐雾16 小时前
OctreeNode
unity·c#·八叉树
WarPigs17 小时前
Unity协程返回值的解决方案
unity·游戏引擎
WarPigs1 天前
Unity单例笔记
unity·游戏引擎
风酥糖2 天前
Godot游戏练习01-第24节-多人游戏暂停菜单,游戏优化
游戏·游戏引擎·godot
Allen74742 天前
ComfyUI 自动化生产 3D资产 工作流笔记
图像处理·opencv·unity·自然语言处理·3d模型生成·confyui
nnsix2 天前
Unity Windows11 打字中文显示不出来输入法的候选框
unity
SCLchuck2 天前
Godot 4 2D 物理引擎位置初始化踩坑:add_child() 和 position 到底谁先? (错误位置触发物理事件)
游戏引擎·godot·游戏开发·物理引擎
adogai3 天前
unity mcp接入 实现一句话生成游戏!
游戏·unity·游戏引擎
mxwin3 天前
Unity Shader 逐像素光照 vs 逐顶点光照性能与画质的权衡策略
unity·游戏引擎·shader·着色器
CDN3603 天前
游戏盾导致 Unity/UE 引擎崩溃的主要原因排查?
游戏·unity·游戏引擎