Background
First or first declare oneself is a relatively stupid person, always can not find efficient learning methods, the current way of learning Unity3d is mainly two, one is to directly look at high-quality source code, the other is the light can not also do their own hands, write some representative of the small program, this is the origin of this article.
Admittedly, now is not the unity3d development of the golden period, a lot of knowledge points have been a lot of friends summed up sharing, but because the objectivity of Unity3d is to rely on the plug-in is very powerful, so will be the plug-in flying, versatility is not strong, there are outdated risks, There are many friends posted on the Internet many of the posts are not learning summary, but for the use of Unity3d IDE and some of the API list, or some of the game demo complete tutorial. I have always wanted to learn from another perspective, that is, purely from the perspective of the game (we often say the angle of business logic), in fact, many VC game tutorials are this way of thinking, is from a very small function to learn, a knowledge point accumulation of the final completion of a relatively complex complete demo program. So this article starts with the smallest thing that moves the object up.
Description
The perspective of the article is basically focused on learning from simple to complex functions, so it is mainly based on 2D, that is, starting from untiy2d, but also interspersed with some 3D parts,
So it's best to use the Unity3d client with the Unity2d function, like after 4.3.
Zhen Lou
Requirements Description and Analysis
The requirement in this chapter is simple: "To control an object to move in a 2d view by clicking the mouse in the screen." In other platforms and engines, the need for Hello world is a bit tricky in unity3d, and there are a few problems that plague me,
1, IO input is how;
2, the mouse event is how to receive;
3, the mobile animation is how to achieve;
"The principle is very simple, the reality is very cruel", Unity3d's openness caused the so-called plug-in and the library many, appears messy. Here or in accordance with their own inertial thinking, according to other platform of the tutorial,
A, first of all to have a container,
B. Place the sprite that needs to be moved on the container,
C, receive mouse events through the container,
D. Event-driven sprite movement
Here Ugui the concept of container (or I do not know other libraries also), so first implemented in Ugui,
The first step is to add a Panel object
The second step, import a sprite of the picture resources
The third step, add a 2d image object, set its sprite as the picture resource just imported
The fourth step is to bind a script to the Panel object, Panelcontroller,
Tree structure
After adding resources to the map
Binding code
The specific code is as follows
Using unityengine;using system.collections;using unityengine.eventsystems;public class Panelcontroller: Monobehaviour, ipointerclickhandler{private Transform childplaytransform; Private Transform Parentcanvastransform; Use the This for Initializationvoid Start () {//Get image transform Childplaytransform = transform. Find ("Image"); Parentcanvastransform = transform.parent;} Update is called once per framevoid Update () {} public void Onpointerclick (Pointereventdata eventData) { Debug.Log (String. Format ("X:{0},y:{1}", Eventdata.position.x, EVENTDATA.POSITION.Y)); Debug.Log (String. Format ("button X:{0},y:{1}", Childplaytransform.localposition.x, CHILDPLAYTRANSFORM.LOCALPOSITION.Y)); Vector2 Localpoint; Detection in rectangular range, global mouse click Point, to Local point conversion recttransformutility.screenpointtolocalpointinrectangle (transform as Recttransform, Eventdata.position, Eventdata.entereventcamera, out localpoint); Childplaytransform.localpOsition = Localpoint; }}
Although the code is small, the core knowledge point is the transformation of the world coordinates, camera coordinates and local coordinates, which is the function below.
//Abstract://Does The Recttransform contain the screen point as seen from the given camera? //parameter: // rect://The recttransform to test with. // Screenpoint://The screen point to test. // Cam: //The camera from which the test was performed from. //Return Result: // True If the point is inside the rectangle. public static bool Rectanglecontainsscreenpoint (Recttransform rect, Vector2 screenpoint, Camera cam); public static bool Screenpointtolocalpointinrectangle (Recttransform rect, Vector2 screenpoint, Camera Cam, out Vector2 Lo Calpoint);
Summarize
Although the function is very few, but also has several knowledge point, one is Unity3d coordinate system, one is the basic knowledge of coordinate transformation, one is Unity3d GUI's event processing way, hereby records. Functionally a little bit of a flaw is that the movement of the genie is instantaneous, that is, where the mouse pointer to where to move (indeed I did not see what is not smooth), save to the next chapter to continue.
Source
"Unity3d base" lets objects move ①--ugui mouse click to move