[Unity3D] Unity3D game development: Unity3D Native2D feature research, unity3dnative2d

Source: Internet
Author: User

[Unity3D] Unity3D game development: Unity3D Native2D feature research, unity3dnative2d

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei.

Reprinted please indicate the source, Author: Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/40452019

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Hello everyone, I'm Qin Yuanpei. Welcome to follow my blog. My blog address is blog.csdn.net/qinyuanpei. After Unity4.3, Unity3D released a 2D component based on Box2D, making Unity3D a game engine that supports 2D game development. Before Unity3D launched this function, we have done a lot of research on developing 2D games using Unity3D. Therefore, the release of the 2D component in Unity3D is not a huge breakthrough in nature, because the difference between the 2D component and the 3D component is that the 3D component has one more Z axis than the 2D component. If we fix the Z axis, this is a two-dimensional world. Previously, orthogonal projection methods were widely used, that is, the camera was projected perpendicular to the XY plane, so that the 3D engine could be used to achieve the effect of 2D games. Now that Unity supports native 2D game development, why don't we try it? Previously, the blogger planned to take time to study the Native2D feature of Unity3D. However, there was no time to study the Native2D feature for various reasons, so now we can use this weekend to study it!


1. Sprite and Sprite

Although we turn the 2D feature of Unity3D into Native2D, in fact, Natvite is only relative to a third-party plug-in. In essence, the 2D feature of Unity3D is still in the category of 3D. Sprite is the first component we walk into Native2D. Therefore, the 2D feature is based on this component. Friends who have learned 2D game development should know that the genie is actually a texture in the 2D world. Now, let's create a new project to demonstrate how to use the Sprite component! The first exciting feature is that we can decide whether a project is a 3D game or a 2D game when creating a project. Here we choose 2D directly, because what we want to explore today is the Native2D feature of Unity3D.


After entering Unity, we will notice that there will be a 2D/3D option button on the toolbar of Unity and the 2D button is activated in the current scenario, which means this is a 2D project, by switching this button, we can find that the Native2D of Unity3D is the effect after the Z axis of Unity3D is fixed. Therefore, in essence, the Native2D of Unity3D is an implementation of the 3D engine in the 2D effect.


Bo master and slave gave some materials to the Internet and made the following scenario:


We noticed that the Sprite component has one of the most important SpriteRenderer components, which is responsible for rendering the Sprite. We can specify the rendering resources by specifying the Sprite attribute. Select an image resource and you can see its Properties window:


TextureType indicates the Texture type. Here we select the Sprite type, because only this type of texture can be provided to the Sprite component for use. SpriteMod is used to specify whether the Sprite is a single image or a series of gallery images. We noticed that this image is a frame animation sequence, so we should select the Multiple type. Next, click the SpriteEditor button to open the SpriteEditor to split these sprite maps into a single image. If you have read the articles written by bloggers when they first started learning Unity3D, you must note that the bloggers used PhotoShop to split a Frame Animation Sequence Chart into slices, you can use the Paster method to implement frame animation. Is this very troublesome? It doesn't matter. The genie editor provided by Unity3D Nativie2D can help you quickly complete this task. Open the wizard Editor:


As you can see, the blogger splits the image into 16 images. Here, you can use the Trim button to obtain a graph block of the same size, because the genie editor can help you determine the border of the graph. The advantage of this is that Unity can help you generate 16 frames of the animation sequence, so that you can call a certain frame of the animation at will. However, the resource manager does not generate the corresponding file, this saves the project resources.


Now, let's write a script to show you how to use this set of genie animations:

Using UnityEngine; using System. collections; public class SpriteScript: MonoBehaviour {// public Sprite [] UpSprites in the upward Sprite set; // public Sprite [] DownSprites in the downward Sprite set; // set the left Sprite public Sprite [] LeftSprites; // set the right Sprite public Sprite [] RightSprites; // private Sprite [] lastSprites, the last Genie set used; // private Sprite [] currentSprites in use; // private int index of the current frame sequence = 0; // Number of frames per second private float fps = 10; // current experience time private float curre NtTime = 0; // the current state of the role private PlayerState; // The private SpriteRenderer renderer = null; void Start () {// initialize the role state = PlayerState. idle; // initialize the role sprite set currentSprites = UpSprites; lastSprites = currentSprites; // get the sprite renderer = GetComponent <SpriteRenderer> ();} void FixedUpdate () {if (Input. getAxis ("Horizontal") = 1) {state = PlayerState. walk; SetSprites (RightSprites); MoveTo (new Vector2 (Time. deltaTime * 2.5F, 0);} els E if (Input. getAxis ("Horizontal") =-1) {state = PlayerState. walk; SetSprites (LeftSprites); MoveTo (new Vector2 (-Time. deltaTime * 2.5F, 0);} else if (Input. getAxis ("Vertical") = 1) {state = PlayerState. walk; SetSprites (UpSprites); MoveTo (new Vector2 (0, Time. deltaTime * 2.5F);} else if (Input. getAxis ("Vertical") =-1) {state = PlayerState. walk; SetSprites (DownSprites); MoveTo (new Vector2 (0,-Time. deltaTime * 2.5F);} else I F (! Input. anyKey) {state = PlayerState. idle;} DrawSprites (currentSprites);} // role mobile private void MoveTo (Vector2 offest) {// calculates the role position float x = transform Based on the offset. position. x + offest. x; float y = transform. position. y + offest. y; // use the 2D rigid body component to move the Genie this. rigidbody2D. movePosition (new Vector2 (x, y);} // set the current Sprite set private void SetSprites (Sprite [] sprites) {currentSprites = sprites; // if the current Genie set does not match the previous Genie set, it indicates that you want to switch the Genie set if (currentSprites! = LastSprites) {lastSprites = currentSprites; index = 0 ;}}// draw the current Sprite set private void DrawSprites (Sprite [] sprites) {// if the role is standing, the first frame if (state = PlayerState. idle) {renderer. sprite = sprites [0];} else {currentTime + = Time. deltaTime; // if the current time is greater than the Frame Animation rendering time, a new frame animation if (currentTime> 1/fps) needs to be rendered) {// increase the index and clear the current time so as to re-count index + = 1; currentTime = 0; // make the index continuous screen loop if (index> = sprites. length) {index = 0 ;}}// render renderer. sprite = sprites [index] ;}# region role state enumeration definition # enum PlayerState {Walk, Idle }# endregion}
So let's take a look at the actual running effect:



This is an animation that controls the movement of a person along the top, bottom, left, and right directions. We can easily achieve this. However, we seem to have written a lot of code. Is there a better way? When Unity3D does not launch a 2D component, we can draw a Sprite in the form of a texture. For this frame animation sequence image, we can determine the position to be displayed on the texture through offset. However, this method seems to only work for common pasters and is powerless for Sprite. Bloggers still like this method. After all, with the Wizard Editor, it is very easy to cut the graph. Next, we will provide a general script that can achieve loop playback of any continuous frame animation. This script is suitable for playing the static effect relative to players in the game, for example, the flag flying in the game, the flying birds, and so on:

Using UnityEngine; using System. collections; public class FightScript: MonoBehaviour {// sequence Frame Animation set public Sprite [] Animations; // The current frame sequence index private int index = 0; // Number of frames per second public float fps = 10; // current experience time private float currentTime = 0; // The genie renderer private SpriteRenderer renderer; void Start () {// obtain the sprite renderer = GetComponent <SpriteRenderer> ();} void FixedUpdate () {DrawSprite ();} // provides an external interface to change the animation public void SetAnimations (Sprite [] sprites, int index) {this. animations = sprites; this. index = index;} // play the animation void DrawSprite () {currentTime + = Time based on the sprite set. deltaTime; // if the current time is greater than the Frame Animation rendering time, a new frame animation if (currentTime> 1/fps) needs to be rendered) {// increase the index and clear the current time so as to re-count index + = 1; currentTime = 0; // make the index continuous screen loop if (index> = Animations. length) {index = 0 ;}// render renderer. sprite = Animations [index];}
This script allows an animation to be played cyclically from a frame as long as a series of frame animation sequences are specified. Next, we will add two Sprite with attack effects:

How is it? Good results, haha. Now let's talk about the sprite Gallery! As the project continues to grow, more and more image resources will be used in the project. If you do not pay attention to the control, the entire game will become larger and larger. To solve this problem, Unity provides the Sprite Packer packaging function of the Sprite Gallery. The so-called sprite gallery is actually to centralize scattered image resources into one image, which can reduce the size of image resources. This is actually a bit similar to the gallery in NGUI. Some resources of "Legend of the legend" were also released by the blogger some time ago, this method is also used for processing image resources in the game. Now let's take a look at how to pack the genie in Unity3D! First, you must enable the Sprite Packer function in Unity:

Next, open the Sprite Packer Window through Window-> Sprite Packer, select the image resource to be packaged in the project resource, and set its Packing Tag to the same name, such as enemys, in this way, they will be packaged on the same image.

Next, click Pack and we will find these images are merged into one image. If we modify the Packing Tag of any image, the image will disappear from the current Gallery.

Now, the first part of the article is over. Let's take a rest. Let's start with the second part of this article: the 2D physical world is as beautiful as ever.


2. 2D physics and Box2D

When it comes to 2D game engines, you cannot leave Box2D alone. Box2D is a C ++ engine used to simulate 2D rigid objects. It is usually used as a physical engine in 2D game engines. Therefore, it can be found in many game engines, unity3D Native2D uses the Box2D engine. You can understand the details of this engine. In short, if the game world lacks a collision, the game world will be boring! Life is like an angry bird. When you fail, you will always laugh. We still have to face the problem. Even if we are hit by a storm in reality, such a life is always worth chasing, because of the cold and lonely life, after all, it is not as vigorous as death. Let's take a look at the 2D physics in Unity3D. In Unity3D, all components related to 2D Physics are put in the parent menu of Physics 2d, so we can find the relevant 2D physical components here. There are three main types of 2D physical components in Unity: rigid body, collision body, and joint. Currently, we only need to focus on the rigid body and the collision body. It is estimated that many friends in 2D physics will think it doesn't matter. It's a big deal. The blogger talked with a friend before. He was doing a good blow. ARPG mobile games never collided from start to end, and almost all the logic was written by himself, because 2D physics is really simple, you only need to calculate the distance. But as a cool programmer, after a deep understanding of the meaning of not repeating wheel manufacturing, will he persistently write collision detection? Therefore, we can directly use the 2D components provided by Unity. First, add a rigid Rigidbody 2D component to the protagonist and check Fixed Anglg. This is because angle changes are not required during the collision process. Similarly, we do not need Gravity, so we need to set Gravity Angle to 0.


Next, we will add the Box Collider 2D collision tool to the main character, two combat roles, and one NPC respectively, and write the following scripts to test the collision tool and trigger respectively. here we need to take a look at the details, if you need a strong effect after an object collision, you need to add a rigid body component to the object, because the force is mutually applied. The script is provided below:

Using UnityEngine; using System. collections; public class CollisionCheck: MonoBehaviour {void OnCollisionEnter2D (Collision2D Coll2D) {if (Coll2D. gameObject. tag = "Fight Player") {Debug. log ("Please stay away from me. I am practicing peerless martial arts! ") ;}} Void OnTriggerEnter2D (Collider2D Coll2D) {if (Coll2D. gameObject. tag = "NPC Player") {Debug. log ("Although I am an NPC, I still need a play! ");}}}
The collision detection in the 2D physical world is basically the same as that in the 3D physical world. You can refer to the latest official API documentation, because the API documentation translated in China basically does not have this part. Okay. Let's take a look at the running effect!


Well, today's content is like this. I hope everyone will like it. Why is it so tired every time I write my blog? Is it because you want to check a large amount of information? If you don't know anything, you can leave a message for me. I try to solve it for you. I hope you can pay attention to and support the blog of the blogger so that the blogger has the courage to keep writing it. Okay, that's it!


Daily proverbs: all the setbacks and pains in your life, and all the experiences are used to train you. Don't always say that the time is cruel. It's actually gentle.




Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei.

Reprinted please indicate the source, Author: Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/40452019

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



Questions about unity3D Game Development

I have been familiar with virtools and unity, and I feel that the engine has its own advantages. As long as I master all of them, I personally feel that unity is in line with my style. Not very gorgeous, but all game functions can be implemented. There are a lot of resources available for download in the unity store, and it is also very convenient to make 2d games.
You can also export your own game to your mobile phone. If you are narcissistic, you will feel satisfied. You can combine art and programming very well. There are very few talents in this field. It is not difficult to learn about unity. As long as you complete several game production examples with video, you can achieve most of the functions. To be proficient, you must study it.
There are many cracked versions of unity on the Internet, and 3.0 is enough. The cracked version is still stable. Occasional problems.
I feel that about 5000 of my laptop can meet the requirements, and I need to use a desktop computer to make a precision model.

What games have unity3d developed? Better to be famous

There are not many games developed by unity3d! Most of the games, especially Chinese games, are developed online games and first-person games. The large games on pc seem to only have the new xianjian Qixia Chuan OL. Other online games are mostly foreign games in South Korea, Europe, and America, most unity3d games are used on mobile phones such as iphone and android! We are famous for the samurai series games! Graffiti bowling, Castle warriors, 3D square maps, and many zombie games. At the edge of the border, the Honorary Medal airborne troops, the biochemical odd soldiers, and the virtual arena are all developed using UDK. Do not confuse UDK with another 3D engine! Udks are more powerful and scalable than unity3d functions, and have much stronger effects than unity3d images. However, they require a much higher configuration than unity3d, but they are less efficient than unity3d and faster to create games with unity3d. Better cross-platform and targeted. You can choose android, IOS, pc, ps, psp, xbox and other development games from the Startup menu. unity3d allows you to create Web 3d games and export flash functions! Although unity3d does not support Chinese characters, unity3d is more widely used in China than UDK! The Chinese version of UDK also provides Chinese subtitle tutorials.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.