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

		}

	}
}
相关推荐
玄米乌龙茶1231 小时前
LLM成长笔记(十三):系统设计与项目实战
笔记·unity·游戏引擎
_Evan_Yao8 小时前
游戏和编程两不误:用Unity做一个简单小游戏
后端·游戏·unity·游戏引擎
2601_956002819 小时前
钢铁雄心4/Hearts of Iron IV2026官方正版最新版pc免费下载(看到请立即转存 资源随时失效)手机版通用
大数据·游戏·游戏引擎·动画·游戏策划
冰凌糕10 小时前
Unity3D Shader 渲染状态详解
unity
郝学胜-神的一滴10 小时前
[简化版 GAMES 101] 计算机图形学 10:反走样与深度缓冲核心解析
c++·unity·godot·图形渲染·three.js·unreal engine·opengl
技术探讨者1 天前
极境导表工具 —— 让配置数据成为游戏开发的效率引擎
unity·编辑器·ai编程·游戏策划
游乐码1 天前
Unity基础(六)小案例
游戏·unity·游戏引擎
Sator11 天前
Unity2022版接入MCP
unity·ai编程
相信神话20212 天前
第四章:创建《酒魂》项目与场景结构
游戏·游戏引擎·godot·2d游戏开发
mxwin2 天前
Unity Shader URP 使用模板测试 · 深度测试实现秘境空间效果
unity·游戏引擎·shader