Home > other >  Emergency help, questions about the mouse to click position of storage
Emergency help, questions about the mouse to click position of storage

Time:11-08

Recently with the Unity (c #) to develop a function, on the 3 d interface mapped polygon area, because Unity is the first time, the mechanism is not very familiar with, there have not been able to solve a problem,
My way is each click left key of mouse, mouse position, and then the mouse to click position coordinates Vector3 stored in a static list, through this list cache the coordinates of the point, line forming regions, (note: to get the mouse to click coordinates is by the rays hit in the update method. The point to get)
Problem description: through the log and found a strange phenomenon, each time you draw area (OnClickEndDrawLine function), the coordinate values in the list is changed, and click on the coordinates of inconsistent (x, z value is different, y value), and even a number and click on the number of inconsistent situation,
The following is the implementation code, also a great god, please guide!!!!!!

Private void the Update ()
{
If (ApplicationCommon LoadDataType=="1")
{
//not draw area
return;
}
//initialize
InitArea ();

SetPositions ();
//the key release, painting area
If (Input. GetMouseButtonUp (2))
{
If (DrawAreaCommon. CurrentPositions. Count & lt; 3)
{
DrawAreaCommon. CurrentPositions. The Clear ();
IsMouseDown=false;
IsAdd=false;
CreateArea. GetComponent (). The Sprite=expan;
Messagebox. Messagebox (IntPtr. Zero, "it may not be less than the number of a 3 point to create regional!" , "remind", 0);

return;
}

//check area map
//if (CheckAreaOverlap (DrawAreaCommon CurrentPositions))
//{
////RightArea HideDrawArea (guid);
//DrawAreaCommon. CurrentPositions. The Clear ();
//isMouseDown=false;
//IsAdd=false;
//createArea. GetComponent (). The Sprite=expan;
//Messagebox. Messagebox (IntPtr. Zero, "area can not overlap, please create again!" , "remind", 0);

//return;
//}

Vector3 Vector3=GetMousePoint ();
MousePoint=vector3;
Draw a line//
String guid=guid. NewGuid (). The ToString ();
The Transform lineRec=null;
OnClickEndDrawLine (guid, out lineRec);

CurrentGuid=guid;
AreaCameraInputField. The transform. The Find (" NameText "). GetComponent (). The text=string. The Empty;
DrawAreaCommon. BindingLeftCamereList=true;


////modify cache
//Unity_DrawArea area=new Unity_DrawArea ();
//area. The Guid=Guid;
//area. The IsDeleted=0;
//area.
//DrawAreaCommon. AreaList. Add (area);

IsAdd=true;
IsMouseDown=false;
//display the domain name, add
HideArea. AreaNameSave. SetActive (true);
DrawAreaCommon. BindingLeftCamereList=true;
CreateArea. GetComponent (). The Sprite=expan;
}
}


//* * * * * * * * * * * * * * * * * * * * * * * * * * * coordinate point set * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Private static void SetPositions ()
{
If (isMouseDown)
{
If (Input. GetMouseButtonDown (0))
{
//Vector3 posi=GetMousePoint ();

//penetrating to click
If (EventSystem. Current. IsPointerOverGameObject ())
{

}
The else
{
//store the mouse to click point
Var posi=GetMousePoint ();

If (posi==Vector3. Zero)
{
Messagebox. Messagebox (IntPtr. Zero, "please click in the scene position!" , "remind", 0);
return;
}
Posi. Z -=0.01 f;

If (DrawAreaCommon. CurrentPositions. Find (t=& gt; T.V ector. X==posi. X & amp; & T.V ector. Y==posi. Y & amp; & T.V ector. Z==posi. Z)==null)
{
LineVector vc=new LineVector ();
Vc. SortID=DrawAreaCommon. CurrentPositions. Count + 1;
Vc. The Vector=posi;
LogHelper. Log (" increase the coordinates of a point: "+ vc. SortID +" X: "+ vc. The Vector, X +" y: "+ vc. Vector. Y." z: "vc + +, Vector. Z);
DrawAreaCommon. CurrentPositions. Add (vc);

DrawAreaCommon. CurrentPositionsString. Add (posi. X + ", "+ posi. Y +", "+ posi. Z);
}
}
}
}
}

//* * * * * * * * * * * * * * * * * * * * draw area of * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
///& lt; Summary>
///the end of the line drawing
///& lt;/summary>
Public void OnClickEndDrawLine (string guid, out the Transform lineRect)
{
LineRenderer currentLine;
//to remove all the function of the disable
GameObject. Find (" Main Camera "). GetComponent (.) enabled=true;
GameObject. Find (" Main Camera "). GetComponent (.) enabled=true;
GameObject. Find (" Main Camera "). GetComponent (.) enabled=true;

//properties to the line of basic Settings
GameObject ga=new GameObject ();
The Transform areaRoot=FloorHelper. GetCameraAreawRootByFloor (ApplicationCommon. Floor. The ToString ());
//ga. The transform. SetParent (transform);
The ga. The transform. SetParent (areaRoot);
CurrentLine=ga. AddComponent (a);
CurrentLine. Material=lineMaterial;
CurrentLine. StartWidth=lineSize;
CurrentLine. EndWidth=lineSize;
CurrentLine. StartColor=lineColor;
CurrentLine. EndColor=lineColor;

//name
CurrentLine. Name=guid + "W", "New Game Object";

//record the first point, again assigned to the List, in order to connect the first point and the final point
CurrentLine. PositionCount=DrawAreaCommon. CurrentPositions. Count + 1;
Foreach (var position in DrawAreaCommon. CurrentPositions)
{
If (position. SortID==1)
{
StartPos=position. The Vector;
}
The else
continue;
}
LineVector vc=new LineVector ();
Vc. SortID=DrawAreaCommon. CurrentPositions. Count + 1;
Vc. The Vector=startPos;

DrawAreaCommon. CurrentPositions. Add (vc);
List//Positions. Sort (new PointsComparer ());
Foreach (var. Ps in DrawAreaCommon CurrentPositions)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related