OSN Unity (ii)

Source: Internet
Author: User
Tags tag name

Iii. Introduction to the Unity Editor

Unity is a commercial-grade 3d game engine. The degree of professionalism of an engine is not in fact the second-generation effect of how cow B is now, to be honest, the effects are even if you don't use them. Because no phone is a second-generation.

The professional level of the game engine is now two aspects, one is the integrity of the editor, the second is the integrity of the infrastructure and the degree of optimization (for example, asynchronous resource loading, file packaging system, scene management efficiency. Crop performance. Rendering optimization, performance analysis, etc.). There is no doubt that unity is excellent in these areas. In addition to writing code, you can use your own custom editor, and everything else can be done in a unification editor provided by Unity. Including scene editing, game debugging, resource management, animation editing and so on. Even you can easily extend the functionality of the editor. Let it provide features such as level editing, monster editing, skill editing, and more.


This is the Unity editor. At the top is a row of menus that can be easily extended by code.

The first part is the scene object browse form. All the scene objects will be shown here, assuming the game is executed. The dynamically created objects are displayed here as well. You can click on one of the objects to view and change its properties.

The second part is the game execution form, the ability to view the game execution results, game execution can also switch to scene paging to view the current scene. The scene camera and scene objects can be manipulated in the scene viewport.

The third part is the object properties form. Ability to view the properties of the currently selected object or resource. Each object must have a transform attribute. Controls the size, rotation, and scaling of objects, where property changes are directly fed into the game being executed. Note, however, that property changes at execution time are not saved. Properties are restored when the execution is stopped.

Part IV is the resource form. It shows the actual folders and files under the current Assets folder.

Here are two points to note, click here and then change the properties corresponding to the file changes. There is no difference in game execution or not.

Move the files as much as possible within unity so that materials and scripts are properly correlated. However, if you change it under the Windows browser, it is very likely that the associations will be lost and you will have to bind or set them again.


Iv. Unity's development language

Unity's development language is C #. Of course, if you are familiar with JavaScript or do not like C #, you can also choose to use JS for development.

In my opinion, JS, in addition to personal language habits and no other advantages, in order to improve the efficiency of implementation. JS in Unity is not a pure JS, but a change (seemingly Microsoft did). And JS is compiled into a DLL before it is loaded into execution. This means that it is not possible, like JS in Cocos2d-x, to be dynamically updated and loaded as a purely scripted script.

C # was originally Microsoft's. NET official language, is unique to Windows.

The great gods of the open source world have launched a project called Mono, which is used to port C # to non-Windows platforms such as Linux.

And unity is based on the Mono project. Makes it an extraordinary cross-platform porting capability.

Unity runs C # by first compiling it into a DLL (regardless of which platform is a DLL, the dynamic library is not the kind of dynamic library that the program loads directly into, but is loaded by the Mono library) and then loaded into the run.

This means that you cannot update the contents of the code (the details will be discussed later when you are actively updating), because the code file is already included in the package, but without updating the contents of the code. A prefabricated (Prefab. Later in this article) which script to bind and the script open parameter configuration are all capable of being arbitrarily updated.


V. Basic ELEMENTS of Unity

All objects in the unity scene are gameobject. Gameobject is an object (such as a box, or a monster). At the same time it is also a node, for example you can create an empty gameobject and then hook up all the monsters under this node. The scene object form can see the hierarchical relationship of all objects directly.

Each gameobject can be added to a random component, and the component is a fundamental element of unity. Being able to say everything is a component, the process of writing code in unity is the process of creating a script that is attached to a gameobject to manipulate its properties.

You can simply create a gameobject and not extend it through inheritance, but you can expand its functionality arbitrarily by component. This is the component-based programming model. The habit will find its flexibility and strength.

Gameobject has a prerequisite component is transform, which describes the rotation, offset, and scale of the object, which is displayed in the attribute form relative to the parent node, assuming you set the transform.position directly in the code. Unity will voluntarily change the absolute coordinates to the coordinates relative to the parent node.

Some of the other frequently used system components are:

The mesh render, Skin mesh render model will have this component, which is responsible for the display of the model. It can be said that only what can be displayed, whether it is a complex character model, or simple geometry. Or simply a picture, there will be mesh render this component.

Material components. Materials are associated with shader. Select a dependent material in Mesh render. Select the shader used in the material. Select the appropriate texture in the shader. These things fit together to form a rendered object.

Collider components, collision boxes (box Collider), Model Collider (Mesh Collider), etc. are all part of this component. You must set this component to make a collision.

Rigidbody components, rigid body components, control the quality of an object, resistance and so on.

To collide, the two parties must have an object that has a rigid body assembly.

Charactor controller components. This is particularly special, it is equivalent to the rigid body components, used to simulate the RPG or FPS of the protagonist's physical motion effect, can collide. But it is not affected by real physics.

Animator components to control character animation playback.

These are the most frequently used system components in unity. The rest are peculiar to special objects. For example, camera control, lighting, terrain, and so on.


Vi. Functions of Unity Script development

Unity has a very smart choice of C # as a script that can reduce the development threshold on the basis of guaranteed efficiency.

C # has three very good quality, garbage collection, intact base libraries and reflection mechanisms. These are very difficult for C + + (not to say C + + does not have, just to implement and promote C + + based garbage collection and reflection is not a very easy thing, like boost more than 10 years and not really popular).

All scripts are inherited from Monobehaviour, and Monobehaviour is inherited from component, which is a component.

You can create the script arbitrarily, then hook it up to Gameobject, and the script will be run.

Monobehaviour provides a lot of basic functionality, and when you're familiar with these features, Unity's scripting is a cinch.

Awake Start Destroy onenable ondisable Update Ongui, these are some of the most frequently used functions. You just need to create such a function in the script, it will be called at the appropriate time. The invocation is based on reflection and is not based on virtual functions.

So whether the function is public and what the return value is is irrelevant. But be careful. Assuming that you write the override of a virtual function, you must declare the function as virtual in the base class and declare it as override in the sub-class. Suppose forget to write. The compiler does not error, but does not run the correct code.

Awake is called when a script is just bound (in most cases, when Gameobject is instantiated well). This time all the open to the editor configuration parameters will be initialized well. is the code in which the script is first run.

Start is the object that is called when the first frame is rendered, and sometimes you instantiate an object and then set some of the parameters in the code. The code that relies on such operations can be placed in start.

Update is a function that will be called every frame. Similarly, there are fixedupdate. This function is fixed interval call, time speed, frame rate speed, whether in the background is not related.

Basically can feel in addition to physical simulation and other special circumstances, otherwise can not use fixedupdate, can not write update do not keep this function in the script.

Ongui is called when the UI of unity is drawn. Generally this function is of little use. It's easier than testing (like any two lines of code to write a button.) Then click to trigger a test effect). The GUI plug-ins dumped a couple of blocks, both in terms of convenience and efficiency.

Here are two additional functions Oncollisionenter and Ontriggerenter, two of which are related to collision detection. If there is a collision box on the object, then this function in the object binding script is called. Assuming that the Istrigger option is checked by collider, collisions and collision displacements will not occur. Purely as a trigger. Calls are functions of the trigger series.  Otherwise, the function of the collision series is called. Each series has enter stay exist three functions respectively corresponding to the object of the collision, the continuous collision, the separation of objects.


Vii. attributes and messaging of Unity script development

Each component (in fact, inside the script) has some column properties that make it easy to get to the object we want. Attributes are Gameobject, transform, Rigidbody, collider, audio, Render, and so on. The most frequently used and must have is gameobject and transform, which is the object that this script hooks up. Suppose this object has a rigid body or a collision box or a sound component added to it at the same time. Then the direct rigidbody and so on the system by default provides the good property to be able to use.

Of course, you can also manually use getcomponent< to find the class name > () in your code to get the component.

There are attributes such as transform and Rigidbody in Gameobject. Transform and Rigidbody have the same gameobject attribute. These referential relationships can easily be used to find what you need with random objects.

You can use Gameobject.find directly ("Object name") to find an object whose name can include a hierarchical path. Gameobject can also use getcomponent or Getcomponentinchildren to get the appropriate script in the current or child node. The related functions are rich, and you can get one or more objects depending on the object type or tag tag name.

Within the script, you can use Invoke ("function name", delay time) to invoke a function inside the current script asynchronously. or use SendMessage ("function name", "parameter") to invoke a function, SendMessage very handy is that it can be called by Gameobject. Only if you get an object, you can use SendMessage to invoke a function that is called only if there is a function with the same name in the script that binds the object. Of course, flexibility is also a cost, either invoke or SendMessage is more inefficient than the actual function call or commit (delegate), so do not misuse (for example, used in update).

Familiar with these points of knowledge. Then you'll be able to write code that fits the unity style. All that remains is the understanding of basic mathematics, algorithms, graphics, and design patterns. This knowledge is irrelevant to the engine. You can write it with Ogre. Unity is also easy to implement.

(The second part is here to the point where it describes how unity was developed in spite of the basics.) But after a period of time the accumulation of code is summed up, as a primer, it more face the essence. I always thought. The difference between the person who will learn and the one who does not learn is in the summary and grasp of the essence. The person who learns can grasp the essence of things, can sum up these things have what few knowledge points. The principle is what, the reason is what, but does not learn the person only will tiger, can realize the function but is no avail, knows it but does not know its why

OSN Unity (ii)

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.