"Dog planing Learning Net"
IntroThe last time we talked about Monobehaviour's past life , we learned that every gameobjec in the game is controlled by script, this time we start to native the sprite in unity in 2D, and use animation to get the sprite moving.
in the next few blogs, I will be doing a Flappy Bird to explain the use of components in unity, the source of the project is here:Unity Flappy Bird. You are welcome to fork and star.
How to create a sprite
to create a sprite, follow these steps
- Add a sprite resource by dragging a picture to a folder in Project view.
- Modify the properties of the sprite resource and, if it is Spritesheet, split it in the Sprite editor.
- Drag a sprite resource into the scene view, or hold down the CTRL key while you drag several pictures in Spritesheet to the scene view, which creates an animation at the same time.
in the process of creating a Sprite, please note that:
- A sprite is added to a gameobject as a property of the sprite renderer component.
- The sorting Layer property in the sprite renderer allows you to set the priority of the sprite being rendered.
- Spriterenderer uses the texture in the Sprite property, but using shader (shader) in material, the sprites/default of the default selection is ignored by the illumination, and Sprites/diffuse This shader is not to ignore the light. If you set material to none, you will find that the sprite cannot be displayed because you are not coloring the sprite.
detailed settings for sprite
Spritesheet: As the name implies, it means putting multiple sprites in a single picture. In general, a frame animation is placed in a picture, the game engine will split the Spritesheet, take turns to play each frame in the Spritesheet, forming an animation sequence. such as:
corresponds to 1, 2, 33 points, I say the notes separately
- When the sprite mode is single, you can set the pivot point of the sprite in inspector, and if you have used cocos2d-x before, you will know that this pivot Point is the equivalent of anchor point in Cocos2d-x. In scene, we will use this pivot point as the center to rotate the sprite, translate the transform settings. If you are using Spritesheet, you can set pivot settings for each sprite in the sprite editor.
- Since the units are unit in unity, in order to guarantee 3D and 2D consistency, we also use unit as the unit of measure in 2D games. Pixel to units a few of this conversion ratio. For example, a sprite picture with a width of 100, displayed in scene with a length of 1Unit.
- In the sprite editor can be automatic and grid two forms of cutting, in general, Automatic has been able to better solve the problem, once encountered we need a few sprite picture size consistent, you can use Grid (GRID) segmentation, To limit the size of each sprite.
How to add a animation to a sprite
There are two ways to create animations, one we've said above, to drag several sprites from Spritesheet to the scene view at the same time.
The other is in the scene view, select the gameobject you want to animate, and in Animation view, click Add curve to animate the various properties of the Gameobject.
in Unity, animation is a relatively broad concept and is not limited to frame animations. It includes dynamic changes in the various properties of the Gameobject. If you click on Add curve, you will see this interface:
in the above picture you can see that from position to color, from rotation to Sprite, it is possible to animation, because animation in unity is actually adding a change curve. Rather than the animation we generally understand.
Another explanation is the difference between animator and animation, be aware that if you want to lay a animation to move, then you must need two assest resources, respectively, animation clip and animator Controller two kinds of resources. In project, it is the following representation:
Animator is an animated state machine that controls the switching between the various animation. In the following blog we will talk about its use. =w=
detailed settings for animation
In the diagram above, I have highlighted some of the important cases in the animation view.
- KeyFrames are the turning point of the animation, where you set some of the sprite's points of change, or a turning point for position data.
- An animated event can be set at a frame of the animation, and the so-called animated event that can be set in this frame is the non-overloaded function of all the script components on this gameobject (that is, functions such as removing onupdate,start). After the animation event is set. You'll notice that every time the animation loops over, the function is called at that frame.
- Add a change curve, as stated above, to transform any property.
- Sample refers to the sampling, that is, in the 1s set how many frames, in the figure we can see the 1s interval of 12 dashed lines, which means that in this second, we set 12 frames to adjust the animation. Note that the sample here is not related to the refresh frame rate that we set for the game.
Summary in general, it's very handy to edit animations in Unity's animation, and it's kind of niaoqianghuanpao when you used cocos2d-x to edit animations. But the support of the sprite itself is a bit uncomfortable, because do 2D, or not used to unity in the German coordinate system, because the production of 2D games we often killed the pixel pixel as the most important unit of measurement, Unity replaced unit units, feel a little overwhelmed, But this is a change in the way the 3D engine thinks.
More Highlights:Www.gopedu.com
Disclaimer: This document is from the "Dog Planing Learning Network" community, is the Netizen self-published Unity3d study articles, if there is any content violated your relevant interests, please communicate with the official, we will deal with the real-time.
"Unity3d Basic Tutorial" (iii): Learn native 2D by making Flappy Bird