Unity 画笔功能
using UnityEngine; using System.Collections; using GoWorldUnity3D; using Valve.VR.Extras; using Valve.VR; /// <summary> /// 画笔画线 /// </summary> public class Line : MonoBehaviour { public LineRenderer line; //LineRenderer private LineRenderer lineRenderer; //定义一个Vector3,用来存储鼠标点击的位置 private Vector3 position; //用来索引端点 private int index = 0; //端点数 private int LengthOfLineRenderer = 0; //LineRenderer个数标志 private int LineIndex = 1; //画笔颜色 public Color color; //画笔大小 public float Size; private ListAttr list; //private ListAttr pos; private ListAttr rgb; private SteamVR_Behaviour_Pose pose; public SteamVR_Action_Boolean teleport = SteamVR_Input.GetBooleanAction("InteractUI"); void Start() { list = new ListAttr(); rgb = new ListAttr(); CommonData.materialLine = line; if (CommonData.isVR) pose = GameObject.Find("RightHand").GetComponent<SteamVR_Behaviour_Pose>(); } void LineParameterSetting() { //设置材质 // lineRenderer.material = new Material(Shader.Find("Particles/Additive")); //设置颜色 lineRenderer.startColor = color; lineRenderer.endColor = color; float width = Size /100-0.007f; //设置宽度 lineRenderer.startWidth = width; lineRenderer.endWidth = width; } private void OnEnable() { if(CommonData.isVR) GetComponent<MeshRenderer>().materials[0].color = color; } void Update() { if (!CommonData.isVR) { #region PCLine //鼠标左击 if (Input.GetMouseButton(0)) { if (Input.GetMouseButtonDown(0)) { // lineRenderer = new GameObject("Line" + LineIndex).AddComponent<LineRenderer>(); lineRenderer = GameObject.Instantiate(line); lineRenderer.gameObject.SetActive(true); lineRenderer.name = ("Line" + LineIndex); LineParameterSetting(); LineIndex++; color = lineRenderer.startColor; Debug.Log(color.r + "------" + color.b + "------" + color.g); int r = (int)color.r; int b = (int)color.b; int g = (int)color.g; rgb.append(r); rgb.append(b); rgb.append(g); } //将鼠标点击的屏幕坐标转换为世界坐标,然后存储到position中 position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1f)); //端点数+1 LengthOfLineRenderer++; //设置线段的端点数 lineRenderer.positionCount = LengthOfLineRenderer; } //连续绘制线段 while (index < LengthOfLineRenderer) { //两点确定一条直线,所以我们依次绘制点就可以形成线段了 lineRenderer.SetPosition(index, position); index++; ListAttr pos = new ListAttr(); pos.append(position.x); pos.append(position.y); pos.append(position.z); list.append(pos); } if (Input.GetMouseButtonUp(0) && index != 0) { GoWorldUnity3D.GoWorld.ClientOwner.CallServer("SyncBrushData", list, rgb); list.RemoveAll(); rgb.RemoveAll(); index = 0; LengthOfLineRenderer = 0; } if (Input.GetKeyDown(KeyCode.Delete)) { if (LineIndex > 0) { Destroy(GameObject.Find("Line" + --LineIndex)); } } #endregion } else { #region VRLine if (pose == null) pose = GameObject.Find("RightHand").GetComponent<SteamVR_Behaviour_Pose>(); if (teleport.GetStateDown(pose.inputSource)) { Debug.Log("按下右扳机"); lineRenderer = GameObject.Instantiate(line); lineRenderer.gameObject.SetActive(true); lineRenderer.name = ("Line" + LineIndex); LineParameterSetting(); LineIndex++; Debug.Log(color.r + "------" + color.b + "------" + color.g); int r = (int)color.r; int b = (int)color.b; int g = (int)color.g; rgb.append(r); rgb.append(b); rgb.append(g); } if (teleport.GetState(pose.inputSource)) { LengthOfLineRenderer++; lineRenderer.positionCount = LengthOfLineRenderer; position = transform.position; while (index < LengthOfLineRenderer) { lineRenderer.SetPosition(index, position); index++; ListAttr pos = new ListAttr(); pos.append(position.x); pos.append(position.y); pos.append(position.z); list.append(pos); } } if (teleport.GetStateUp(pose.inputSource)) { GoWorldUnity3D.GoWorld.ClientOwner.CallServer("SyncBrushData", list, rgb); list.RemoveAll(); rgb.RemoveAll(); index = 0; LengthOfLineRenderer = 0; } if (Input.GetKeyDown(KeyCode.Delete)) { if (LineIndex > 0) { Destroy(GameObject.Find("Line" + --LineIndex)); } } #endregion } } }