#游戏unity-the country of sound # The illustrated UI in combat system
In wartime, if there is an unfamiliar note beast opposite, we want to be able to query the opposite note beast situation, so we need a combat guide. We need to open the query in real time. So a drag-and-drop guide UI is made, as shown in the following figure (background image is random looking)
This frame is drag-and-drop, holding down the triangle in the lower right corner can adjust the size of the UI, and will not affect the combat effect.
Next, we will explain how is made;
First of all, of course, the new UI system, to place the button and panel, there is no detailed operation;
Then, because of the need to zoom and drag, so you can limit the maximum range to avoid the game interface, a new panel named can be dragged area, in which the child objects new in the following figure
The main need to implement the two functions is to be tied to another script, other such as button click Function, close the dialog box and other functions is very basic not to repeat. implementation of drag-and-drop function
Basically is the implementation of two interface Ipointerdownhandler, Idraghandler, to determine whether the click event is from the screen or button, and the Ipointerdownhandler of the main method is Onpointerdown, You can use the instant data as an incoming parameter. Do not understand the students can go to Unity website to view the API when the Stargate
The main point in the code to write the UI is the transformation of coordinates, mainly about the local coordinates and the relative screen of the coordinates between the conversion, which also need to use the math library method functions, so most of the calculated code. The logic is mainly based on the implementation of the two interfaces to achieve. The code is as follows, binding on the drag Zone
Using Unityengine;
Using Unityengine.ui;
Using Unityengine.eventsystems;
Using System.Collections; public class Dragpanel:monobehaviour, Ipointerdownhandler, Idraghandler {private Vector2 originallocalpointerposit
Ion
Private Vector3 originalpanellocalposition;
Private Recttransform Panelrecttransform;
Private Recttransform Parentrecttransform;
void Awake () {panelrecttransform = transform.parent as Recttransform;
Parentrecttransform = panelrecttransform.parent as Recttransform; } public void Onpointerdown (Pointereventdata data) {originalpanellocalposition = Panelrecttransform.localpo
sition; Recttransformutility.screenpointtolocalpointinrectangle (Parentrecttransform, Data.position,
Data.presseventcamera, out originallocalpointerposition);
public void Ondrag (Pointereventdata data) {if (Panelrecttransform = null | | parentrecttransform = = NULL)
Return Vector2 Localpointerposition; if (Recttransformutility.screenpointtolocalpointinrectangle (Parentrecttransform, Data.position, Data.presseventcamera, out localpointerposition)) {Vector3 offsettooriginal = localpointerposition-original
Localpointerposition;
Panelrecttransform.localposition = originalpanellocalposition + offsettooriginal;
} Clamptowindow ();
} void Clamptowindow () {Vector3 pos = panelrecttransform.localposition;
Vector3 minposition = parentrecttransform.rect.min-panelrecttransform.rect.min;
Vector3 maxposition = Parentrecttransform.rect.max-panelrecttransform.rect.max;
Pos.x = Mathf.clamp (panelrecttransform.localposition.x, minposition.x, maxposition.x);
Pos.y = Mathf.clamp (PANELRECTTRANSFORM.LOCALPOSITION.Y, MINPOSITION.Y, MAXPOSITION.Y);
Panelrecttransform.localposition = pos;
}
}
implementation of the scaling function
Similar to the drag-and-drop function, the two interfaces are implemented; Logically, it is also very clear, mainly or arithmetic, and the code is as follows-
Using Unityengine;
Using Unityengine.ui;
Using Unityengine.eventsystems; public class Resizepanel:monobehaviour, Ipointerdownhandler, Idraghandler {public Vector2 minsize = new Vector2 (1
00, 100);
Public Vector2 maxSize = new Vector2 (400, 400);
Private Recttransform Panelrecttransform;
Private Vector2 originallocalpointerposition;
Private Vector2 Originalsizedelta;
void Awake () {panelrecttransform = transform.parent.getcomponent<recttransform> ();
public void Onpointerdown (Pointereventdata data) {Originalsizedelta = Panelrecttransform.sizedelta; Recttransformutility.screenpointtolocalpointinrectangle (Panelrecttransform, Data.position, Data.pressEventCamera
, out originallocalpointerposition);
public void Ondrag (Pointereventdata data) {if (Panelrecttransform = null) return;
Vector2 localpointerposition; Recttransformutility.screenpointtolocalpointinrectangle (PAnelrecttransform, Data.position, Data.presseventcamera, out localpointerposition);
Vector3 offsettooriginal = localpointerposition-originallocalpointerposition;
Vector2 Sizedelta = Originalsizedelta + new Vector2 (offsettooriginal.x,-OFFSETTOORIGINAL.Y); Sizedelta = new Vector2 (Mathf.clamp (sizedelta.x, minsize.x, maxsize.x), Mathf.clamp (Sizedelta.
Y, Minsize.y, maxsize.y));
Panelrecttransform.sizedelta = Sizedelta; }
}
The above is all the content, want to resource package can comment, bloggers will be private hair ~