Unity as one of the game development platform, there are a lot of practical tips, today to learn how to use two ways to show the cut in the fruit game:
1. Normal display of the following:
What do you mean by normal display? The cut fruit games we've been exposed to are generally 2D games, and we know that 2D games can be done directly with unity, and can be implemented using Ngui, Ugui, or other methods through the UI.
So our first approach is not to use the UI.
First of all, look at the material of our knife: (Students who need to save the right button.) ^_^)
Open Unity:
Create a new empty game body named "BackGround" and add the component GUI Texture in the palette: (Create an object don't forget to reset it!)
To find the Texture property, click the back circle and select a background image for it:
Background material:
After adding a good background image, we looked at the scenes panel and the game panel and found two questions:
1. In the scenes panel, add up the picture can not see;
The position of the picture in the 2.Game panel is a bit wrong.
Scenes can not see the situation, because I am also small white, do not understand the principle, but I guess is because Guitexture is part of the UI, so in the scenes panel sometimes display effect is not good, as to how to set to let it show, I do not know yet, I am now referring to the display on the game panel.
The game panel is not displayed, we set the position under it transform to (0.5,0.5,0):
That's all you can do.
Next we create a new empty game body, name it knife, and add the Linerenderer component to it:
We can find the materials property in the image above, with size and element 0 below.
Because materials is an array of type materials, size is the length of the array, element is the value of the array element, and size is 1, then it has only one element, with an index value of 0,element 0.
The game body we just created is used to show the knife marks, so we're going to give it our blade footage above.
You will find that dragging the image directly to element 0 is unsuccessful because element 0 is of type material, and the picture is not a material, it is now just a picture, the corresponding material has not been generated.
We drag the picture directly to the knife game body, unity will automatically create a material material for this image, and assign this material to element 0;
You can see it in the scenes panel.
Next, we find the Position property under line renderer and find that it is also an array, what is this array for? He is the key attribute used to draw the line renderer.
In fact line renderer is like this: you give me a few coordinates, I put the corresponding material in a few coordinates between the rendering. Now: element 0 is (0,0,0) element 1 is (0,0,1), that is, the picture is displayed in the coordinates (0,0,0) and (0,0,1), because two points to determine a straight line ~ right?
Now that we know the principle of line renderer, then we know, I want to use the mouse to draw a wire, I just get to the left mouse button press the coordinates and the mouse left button lifted when the coordinates, and then give them to element 0 and element 1, then this line is not painted it?
So let's start writing the script:
usingUnityengine;usingSystem.Collections; Public classtes:monobehaviour {Vector3 pos1;//define start coordinatesVector3 Pos2;//define end coordinatesLinerenderer Mylinrenderer;//Define a Linerenderer object // Use this for initialization voidStart () {mylinrenderer = getcomponent<linerenderer> ();//Initialize the Linerenderer object}//Update is called once per frame voidUpdate () {if(Input.getmousebuttondown (0))//When the left mouse button is pressed{pos1 = Camera.main.ScreenToWorldPoint (NewVector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y,1));//Record the starting coordinates}if(Input.getmousebuttonup (0) {Pos2 = Camera.main.ScreenToWorldPoint (NewVector3 (INPUT.MOUSEPOSITION.X,INPUT.MOUSEPOSITION.Y,1));//Record the end coordinatesMylinrenderer.setposition (0, POS1); Mylinrenderer.setposition (1, Pos2);//The starting and ending coordinates are paid to the corresponding variables respectively} }}
After writing it, let's think about it, Linrenderer is to set the coordinates for it to start rendering, and that's the script we've written that means: when I release the mouse, this line can be drawn, and when the mouse has been pressed, this line we can not see.
We give this script to the game body knife we created, after running:
As we can see, there really is something like a knife in the scenes panel, but there are two problems:
The color doesn't seem to be the same as ours, because here:
With the Parameters property, you can set the start width and end width of the blade, the transition from the start color to the end color, and the default is white.
Another serious problem is that the knife is not displayed in the game panel at all, and the scenes panel on the left is clearly visible!
We look at the time of operation, the position of the background is (0.5,0.5,0), the camera position is (0,1,-10), and is the orthogonal camera:
At this point the two coordinates of the blade Z are-9, stating that the problem is not a script:
The blades are supposed to be fully visible, and we'll check the layer layers again, even if we re-set a layer.
It seems to be a little crazy ...
After a series of checks, and finally found the crux of the problem, from the beginning we were wrong, remember the beginning we said that the background in the scenes panel, right? Because we are adding guitexture, here we do wrong, should not add guitexture components, but should be spriterenderer:
Now we can see it under the Scenes panel and the game panel:
The game is not displayed, we adjust the scale of background to (2.4,2.4,1):
Then we change the layer of background and knife to the default (because people may find that their layer is Ngui, because I didn't change it when I was doing one of the latter methods.) ), while the cullingmask of the camera remains the default everything.
Let's run it again:
The mouse presses, and moves a distance, without reacting:
When you release the mouse:
Ah haha haha!
It's finally thick!
And handsome, right?
Yes, that's it.
Before I found out the problem, why did I not revise the previous content, but in the article with a lot of space to find errors? That's because I think every mistake we make is very meaningful to us, and when I look back on this one day, I will be very impressed with Guitexture and spriterenderer, the sauce.
Anyway
However, we found a problem: there is only one of the knife, and I now draw the knife is not a straight line, but a inflection point? It's just a knife that shows a connection between the starting point and the end point, and none of the inflection points appear.
In this case, then we have to record the inflection point in the middle is not good?
Let's think about this: when I press the left mouse button, the position of the mouse is recorded every 0.2 seconds, and then it is added to the position coordinate array:
In fact, this position coordinates more like a generic, because it has the properties of the array, but there is no length limit:
We can do this, every 0.2 seconds, let the length of the generic add 1, and then fill in the coordinates, as long as two or two more coordinates set, this line can be drawn:
Modify the code as follows:
usingUnityengine;usingSystem.Collections; Public classtes:monobehaviour {Vector3 pos1; Vector3 Pos2; Linerenderer Mylinrenderer;intCount=0;//Define a variable to modify the length of the generic floatTime =0.2F//Define a time interval // Use this for initialization voidStart () {mylinrenderer = getcomponent<linerenderer> (); }//Update is called once per frame voidUpdate () {if(Input.getmousebuttondown (0) {pos1 = Camera.main.ScreenToWorldPoint (NewVector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y,1)); Mylinrenderer.setvertexcount (1);//When you press the left mouse button for the first time, there should be a point, so set the number of nodes to 1Mylinrenderer.setposition (0, POS1);//have a node to set the coordinates, rather than simply record coordinates}if(Input.getmousebutton (0))//When the mouse has been pressed{Time-= Time.deltatime;//Time to decrement if(Time <=0)//When the time is reduced to 0 or below 0,{time =0.2F//Reset the timecount++;//number of nodes self-incrementVector3 pos = Camera.main.ScreenToWorldPoint (NewVector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y,1));//record The coordinate value at this timeMylinrenderer.setvertexcount (count);//Every 0.2 seconds, add a node, the way to add a point is the Setvertexcount () methodcount--;//count is also self-reducing, because the index is 1 smaller than the number of nodes when setting the position.Mylinrenderer.setposition (Count,pos);//Set the location of this added nodecount++;//Finally, because a node is added, count is incremented by 1} }if(Input.getmousebuttonup (0))//When the left mouse button is lifted, the coordinates of the last point are still recorded, and the count is eventually reset to 0{count++; Pos2 = Camera.main.ScreenToWorldPoint (NewVector3 (INPUT.MOUSEPOSITION.X,INPUT.MOUSEPOSITION.Y,1)); Mylinrenderer.setvertexcount (count); count--; Mylinrenderer.setposition (COUNT,POS2); Count =0; } }}
The place where comments are added to the code is where the changes are, and for Count's worth, I'm using this stupid method, which needs to be considered to clarify.
Save the Run, you get:
The effect has come out, however, linerenderer in the corner of the place to handle their own bad, and sometimes become like this:
This is because the width changes when the turning is involved, and this is the case with the width variation.
We set different values for start width and endwidth under the Parameters property of the blade, for example, one is 1 and one is 3:
In the first script to hang up, run it:
We will find that it is not a straight line, because the starting width is 1, the end width is 3,1 to 3 is not a natural transition, so it is like a sudden lack of a piece in the middle, this is the problem of linerenderer, in the same way, we encountered in the turn of the problem similar to it.
However, it is possible to resize their widths to make them more natural, such as starting and ending widths are set to 0.3:
This is a little bit better, but the fundamental problem can not be solved, to completely solve the need to write their own scripts to control.
Here we have finished the first method, then say the second method: in the Ngui to achieve mouse painting knife:
CTRL + N Create a new scenes, name it casually, import Ngui package.
Find Ngui->open->prefab Toolbar,
Drag the background of the upper left corner into the hierarchy panel:
Find Bg-stripes and delete it:
Open in turn:
Click Create, select the directory and name it, click Save, do not close this window, find the background image in the project panel, we will find that the Create button becomes add/create, click on it. This background image appears after the update Word:
Now it's time to close this window.
Select Control-background in hierarchy, find Uisprite in the Inspector panel on the right, click Atlas:
The popup window is as follows:
Find the prefab you just named, if not, click Show All below, then click on the corresponding select, then click on the sprite:
Select the background picture in the pop-up panel and double-click it.
We find it dark:
Click on the Colortint (click on the black area on the right):
The following window pops up:
Use the mouse to move the white circle in the lower left corner of the box to the white area on the upper left:
The results can be displayed normally:
Above are some nonsense, basically everyone will operate.
Next, create the blade in the same way as above and drag the previous script to it (make a little change):
usingUnityengine;usingSystem.Collections; Public classtes:monobehaviour {Vector3 pos1; Vector3 Pos2; Linerenderer Mylinrenderer;intCount=0;floatTime =0.2F PublicCamera Mycamera;//Declare a public camera type (God, what types have ...) ) // Use this for initialization voidStart () {mylinrenderer = getcomponent<linerenderer> (); }//Update is called once per frame voidUpdate () {if(Input.getmousebuttondown (0) {pos1 = Mycamera.screentoworldpoint (NewVector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y,-1));//change is to be made hereMylinrenderer.setvertexcount (1); Mylinrenderer.setposition (0, POS1); }if(Input.getmousebutton (0)) {Time-= Time.deltatime;if(Time <=0) {time =0.2F count++; Vector3 pos = Mycamera.screentoworldpoint (NewVector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y,-1));//change is to be made hereMylinrenderer.setvertexcount (count); count--; Mylinrenderer.setposition (count, POS); count++; } }if(Input.getmousebuttonup (0)) {count++; Pos2 = Mycamera.screentoworldpoint (NewVector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y,-1));//change is to be made hereMylinrenderer.setvertexcount (count); count--; Mylinrenderer.setposition (COUNT,POS2); Count =0; } }}
Annotated place is the place to change, all Camera.main.ScreenToWorldPoint to Mycamera.screentoworldpoint (Mycamera depends on your specific name. )
and the z-axis value of the back is also changed to-1.
Ctrl+s Save the script, go back to unity, select the blade, and you'll see one more mycamera:
Drag the camera on the left side along the arrow:
Run:
Width yourself, if not shown, set the knife layer and the camera's culling mask just fine.
Just modify a little bit of script to be able to use it in Ngui.
Well, this is my personal now to achieve cutting fruit cut two ways, if you have any good way to welcome the exchange Oh ~
At the same time, there is something wrong in the article, welcome everyone to criticize correct! Thank you!
Unity Knowledge Three: two ways to achieve cutting fruit cut