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, even if you use it, you won't use it 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 outside. 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 that the game is executed, then the dynamically created objects will be shown here. You can click on one of the objects to view and change its properties.

The second part is the game execution form. Ability to view game execution results. The game can also be switched to scene paging to view the current scene, allowing the scene camera and scene objects to 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. Property changes here can be directly fed into the game being executed, but note that property changes are not saved at execution time, and 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 far as possible within unity. Such materials and scripts will be 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 #, assuming that 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, Unity JS is not the pure JS. It's changed (it looks like Microsoft did it), and JS is compiled into a DLL before it's loaded into execution. This means that it is impossible to be like JS in Cocos2d-x. As a purely scripted dynamic update and load.

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. Unity is based on the Mono project, which makes it an exceptional cross-platform porting capability.

Unity's principle of running C # is to compile the DLL first (regardless of which platform is a DLL). This dynamic library is not the kind of dynamic library that the program loads directly into. Instead, it is loaded into the run by the Mono Library). Then load 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 content of the code. A pre-fabricated (Prefab, described later) binding which script and 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. Components are a fundamental element in unity that also has a core. Can say that everything is a component. The process of writing code in unity is the process of creating a script to manipulate its properties as a component on top of Gameobject. You can only create a gameobject and not extend it through inheritance, but you can expand its functionality arbitrarily by component, which is a component-based programming pattern.

The habit will find its flexibility and strength.

Gameobject has an essential component that is transform. It describes the rotation, offset, and scaling of objects, which are displayed in the attribute form relative to the parent, assuming you set the transform.position directly in your 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 the dependent material in the Mesh render, select the appropriate texture in the shader,shader used in the material. 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.

The Charactor controller component, which is special, is equivalent to a rigid body component. Used to simulate the physical motion of the protagonist in RPG or FPS, which 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 is peculiar to special objects, such as camera control, lighting, topography, 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 qualities, garbage collection, good base libraries, and reflection mechanisms, which are very difficult for C + + (not to say C + + does not.) Just implementing and promoting C + + based garbage collection and reflection is not a very easy thing to do, like boost more than 10 years and not really popular.

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

You can create scripts arbitrarily. Then hook it up to gameobject. The script will then 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 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. Note, however, that the assumption is based on the override of the virtual function written by yourself. It is necessary to declare the function as virtual in the base class. The subclass is declared as override, and if you forget to write, the compiler will not error, but it won't run to the correct code.

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

Start is the object that is called when the first frame is rendered. Sometimes you instantiate an object and then set a number of parameters in the code, and the code that relies on such an operation can be placed in start.

Update is a function that will be called every frame. Similar to the fixedupdate, this function is called at a fixed interval. Time, speed, frame rate, 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 drawing Unity's UI, which is generally useless. It's easier than testing (like any two lines of code to write a button.) Then click to trigger a test effect), no matter from the convenience or efficiency of those GUI plug-ins dumped a few streets.

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. Suppose Collider checked the Istrigger option. So collisions and no collision displacements, purely as triggers, call the 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. Assuming that the object has a rigid body or a collision box or a sound component at the same time, the direct rigidbody system can be used by default to provide good properties. 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, which makes it easy 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, just to get an object, you can use SendMessage to invoke a function. Only the script with this object bound has a function of the same name, then it is called. 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).

By being familiar with these points of knowledge, you will be able to write code that conforms to unity style. All that remains is the understanding of basic mathematics, algorithms, graphics, design patterns, which are independent of the engine, and you can write it out with ogre, and unity can easily be implemented.

(The second part here concludes with a description of what unity development is all about, although the basics are summed up after a period of code accumulation, as an entry point.) It is more face to 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.

Those who learn can grasp the nature of things. It can be summed up what kind of knowledge points these things have. The principle is what, the reason is, and the person who does not learn is only Tiger. can realize the function but no avail, know it but do not know the reason 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.