最近用Unity(c#)開發一個功能,在3d界面上繪制多邊形區域。因為是第一次弄unity,對機制不很熟悉,遇到一個問題沒能解決。
我的做法是每次點擊滑鼠左鍵,獲取滑鼠位置,然后將滑鼠點擊的位置坐標Vector3存盤到一個靜態的list中,通過這個list快取的坐標點,連線形成區域。(備注:獲取滑鼠點擊坐標是通過update方法里的射線hit.point獲取的)
問題描述:通過日志發現一個很奇怪的現象,每次繪制區域的時候(OnClickEndDrawLine函式),list中的坐標值被改變,和點擊的時候的坐標值不一致(x,z值都不一樣,y值一樣)。甚至還有數量和點擊數量不一致的情況。
下面是實作代碼,還請大神指導!!!
private void Update()
{
if (ApplicationCommon.LoadDataType == "1")
{
//不繪制區域
return;
}
//初始化
InitArea();
SetPositions();
//中鍵釋放,畫區域
if (Input.GetMouseButtonUp(2))
{
if (DrawAreaCommon.CurrentPositions.Count < 3)
{
DrawAreaCommon.CurrentPositions.Clear();
isMouseDown = false;
IsAdd = false;
createArea.GetComponent<Image>().sprite = expan;
Messagebox.MessageBox(IntPtr.Zero, "創建區域點的數量不能少于3個!", "提醒", 0);
return;
}
//校驗區域繪制
//if (CheckAreaOverlap(DrawAreaCommon.CurrentPositions))
//{
// //RightArea.HideDrawArea(guid);
// DrawAreaCommon.CurrentPositions.Clear();
// isMouseDown = false;
// IsAdd = false;
// createArea.GetComponent<Image>().sprite = expan;
// Messagebox.MessageBox(IntPtr.Zero, "區域不能重疊,請重新創建!", "提醒", 0);
// return;
//}
Vector3 vector3 = GetMousePoint();
mousePoint = vector3;
//畫線
string guid = Guid.NewGuid().ToString();
Transform lineRec = null;
OnClickEndDrawLine(guid, out lineRec);
CurrentGuid = guid;
areaCameraInputField.transform.Find("NameText").GetComponent<Text>().text = string.Empty;
DrawAreaCommon.BindingLeftCamereList = true;
////修改快取
//Unity_DrawArea area = new Unity_DrawArea();
//area.Guid = guid;
//area.IsDeleted = 0;
//area.
//DrawAreaCommon.AreaList.Add(area);
IsAdd = true;
isMouseDown = false;
//顯示添加區域名稱
HideArea.areaNameSave.SetActive(true);
DrawAreaCommon.BindingLeftCamereList = true;
createArea.GetComponent<Image>().sprite = expan;
}
}
//***************************設定坐標點********************************************
private static void SetPositions()
{
if (isMouseDown)
{
if (Input.GetMouseButtonDown(0))
{
//Vector3 posi = GetMousePoint();
//穿透過來的點擊
if (EventSystem.current.IsPointerOverGameObject())
{
}
else
{
//存盤滑鼠點擊的點
var posi = GetMousePoint();
if (posi == Vector3.zero)
{
Messagebox.MessageBox(IntPtr.Zero, "請點擊場景中的位置!", "提醒", 0);
return;
}
posi.z -= 0.01f;
if (DrawAreaCommon.CurrentPositions.Find(t => t.Vector.x == posi.x && t.Vector.y == posi.y && t.Vector.z == posi.z) == null)
{
LineVector vc = new LineVector();
vc.SortID = DrawAreaCommon.CurrentPositions.Count + 1;
vc.Vector = posi;
LogHelper.Log("增加坐標點:" + vc.SortID + "X:" + vc.Vector.x + "y:" + vc.Vector.y + "z:" + vc.Vector.z);
DrawAreaCommon.CurrentPositions.Add(vc);
DrawAreaCommon.CurrentPositionsString.Add(posi.x + "," + posi.y + "," + posi.z);
}
}
}
}
}
//********************繪制區域**********************************
/// <summary>
/// 結束畫線
/// </summary>
public void OnClickEndDrawLine(string guid, out Transform lineRect)
{
LineRenderer currentLine;
//解除所有禁用的功能
GameObject.Find("Main Camera").GetComponent<TreeViewTest>().enabled = true;
GameObject.Find("Main Camera").GetComponent<DeviceTreeView>().enabled = true;
GameObject.Find("Main Camera").GetComponent<StandTreeView>().enabled = true;
//對線屬性的基本設定
GameObject ga = new GameObject();
Transform areaRoot = FloorHelper.GetCameraAreawRootByFloor(ApplicationCommon.Floor.ToString());
//ga.transform.SetParent(transform);
ga.transform.SetParent(areaRoot);
currentLine = ga.AddComponent<LineRenderer>();
currentLine.material = lineMaterial;
currentLine.startWidth = lineSize;
currentLine.endWidth = lineSize;
currentLine.startColor = lineColor;
currentLine.endColor = lineColor;
//名稱
currentLine.name = guid + "W" + "New Game Object";
//記錄第一個點,再次賦值給List,為了連接第一個點和最后的一個點
currentLine.positionCount = DrawAreaCommon.CurrentPositions.Count + 1;
foreach (var position in DrawAreaCommon.CurrentPositions)
{
if (position.SortID == 1)
{
startPos = position.Vector;
}
else
continue;
}
LineVector vc = new LineVector();
vc.SortID = DrawAreaCommon.CurrentPositions.Count + 1;
vc.Vector = startPos;
DrawAreaCommon.CurrentPositions.Add(vc);
List<Vector3> points = new List<Vector3>();
//Positions.Sort(new PointsComparer());
foreach (var ps in DrawAreaCommon.CurrentPositions)
{
LogHelper.Log("畫圖的坐標點:" + " " + ps.SortID + "X:" + ps.Vector.x + "y:" + ps.Vector.y + "z:" + ps.Vector.z);
points.Add(ps.Vector);
}
currentLine.SetPositions(points.ToArray());
points.Clear();
//DrawAreaCommon.CurrentPositions.Clear();
//DrawAreaCommon.CurrentPositionsString.Clear();
//DrawCameraArea.CameraList.Clear();
//給創建的物件添加boxcollider
ga.AddComponent<BoxCollider>();
//為物件添加雙擊腳本
ga.AddComponent<DoubleArea>();
lineRect = currentLine.transform;// currentLine.transform.parent.GetComponent<RectTransform>();
}
//************************下方是列印出來的日志,可以很明顯看出來獲取的點擊坐標和繪圖時候的值不一樣,而且點還做了*********************************************
時間:2/27/2020 9:13:38 AM 增加坐標點:1X:3.74903y:0.418z:-7.331994
時間:2/27/2020 9:13:38 AM 增加坐標點:2X:6.585877y:0.418z:-7.234606
時間:2/27/2020 9:13:39 AM 增加坐標點:3X:7.268704y:0.418z:-2.926811
時間:2/27/2020 9:13:39 AM 增加坐標點:8X:4.644091y:0.418z:-3.254912
時間:2/27/2020 9:13:40 AM 畫圖的坐標點: 1X:-5.95851y:0.418z:-6.972
時間:2/27/2020 9:13:40 AM 畫圖的坐標點: 2X:-4.54279y:0.418z:-4.29494
時間:2/27/2020 9:13:40 AM 畫圖的坐標點: 3X:-1.67278y:0.418z:-2.87906
時間:2/27/2020 9:13:40 AM 畫圖的坐標點: 4X:3.28416y:0.418z:-3.52266
時間:2/27/2020 9:13:40 AM 畫圖的坐標點: 5X:2.05923y:0.418z:-6.79627
時間:2/27/2020 9:13:40 AM 畫圖的坐標點: 6X:-0.701536y:0.418z:-7.494
時間:2/27/2020 9:13:40 AM 畫圖的坐標點: 7X:-5.95851y:0.418z:-6.972
時間:2/27/2020 9:13:40 AM 畫圖的坐標點: 8X:4.644091y:0.418z:-3.254912
時間:2/27/2020 9:13:40 AM 畫圖的坐標點: 9X:-5.95851y:0.418z:-6.972
uj5u.com熱心網友回復:
頂起來頂起來頂起來頂起來轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/17298.html
標籤:Unity3D
