Unity3D (3)

Source: Internet
Author: User

8. Prefab)

This is proposed separately because it is too common. It is also one of the core elements of Unity. In the original Unity, you drag a model to the scene, create a ry, or lighting terrain, and set the offset, rotation, and scaling of the object. Then, bind the script and set the parameters. If you want the second object to be the same, copy it. But the problem arises. If there is a problem where all the n objects need to be modified, the operation should be performed n times. At this time, the pre-made items reflect the power. You can drag the objects in the scene to the resource browsing window below to create a new pre-made house. If n objects in a scenario are created through pre-made modifications, the objects in the scenario will be changed accordingly. Similarly, after modifying the attributes in the scenario, click the Apply button in the Properties window. The pre-made attributes will also be modified. The only exception is the Transform component, which is not pre-modified. Other components, including script configuration parameters, are modified.

It can be simply understood that prefabrication is a configuration file that records objects and related configuration parameters. Many times, I also use it for script configuration. For example, when a skill system is implemented, each skill is a premade one. I have bound the corresponding script to it and configured the skill parameters, then, when the game releases the skill, instantiate this pre-made one.

There was a problem that had plagued me all the time. I wanted to prefabricate and support such a function. Its parameters can be serialized into the scenario file, but will not be affected by the Apply button. In this way, I can easily drag a premade image to the scene, copy n copies, and edit the configuration for the scenario separately, and you don't have to worry that I accidentally click the Apply button to kill all the scenes. But now, this function can be implemented without prefabrication. simply extend the menu function to generate and assemble objects.

In short, it is a correct idea and direction to use presets to load models and other resources in Unity instead of directly using them. It is very convenient and easy to maintain.


IX. File System and AssetsBundle

Now that we have talked about Prefab, let's take a look at the Unity file system.

Prior to the rise of mobile games, Unity was specialized in web game development, which is a feature not available to unreal and other engines. The file Packaging System and AssetsBundle are important components.

A Unity game runs a scenario. By default, a scenario is loaded, and other scenarios can be loaded using Application. LoadLevel in the code. Only one scenario exists during game operation. When a new scenario is loaded, the old scenario resources will be released. Of course, Application. LoadLevelAdditive can add a scenario to the current scenario. Each script can use the DontDestroyOnLoad function to declare that the current object will not be destroyed when the scenario is loaded. It is often used in the singleton mode.

When game resources are packaged by Unity, they are filtered based on the resources in the current scenario. Unused resources are not packaged. When the image is packaged, the texture is compressed according to the configuration. This function is very convenient and powerful. With this function, you do not need to maintain a set of resources on each platform, and Unity will automatically help you deal with it. Of course, Unity caches the converted files, so these files will be compressed again when the platform is switched. Therefore, we recommend that you use a platform checkout and a project to avoid time consumption during multi-platform switching.

The exported resources of Unity are named level (N) and sharedassets (N ). assets. You cannot directly read a file. All files must exist in the scenario or be referenced by the script of an object in the scenario.

This mode is very convenient when making simple games or games, but it cannot be used when the game is large. Most of our actual game development experiences dynamic loading. For example, my current game has only one scenario, which contains only a few simple management classes. All required interfaces and scenarios are dynamically loaded at runtime. In this case, we need to put the Resources (more specifically, prefabricated, and Unity will automatically search and package the Resources based on the dependency) under the Resources folder. Resources in this folder will be exported no matter whether the scenario is used or not, and Resources. Load ("file path") can be used in the code to Load this object. After the object is loaded, it is instantiated through the Instantiate function, and then it can be used normally.

Finally, let's talk about AssetsBundle, which is also a small file system. To reduce the size of the installation package, or to automatically update resources, you must use AssetsBundle to package resources. We can use the extended editor to write an ExportResource function, package the specified file into AssetsBundle, and then place the package on the server. When the game is running, it can be dynamically downloaded and loaded. It can be said that the implementation of an mmo must use this function. It is an open interface for the relatively closed file system of Unity.


10. Configure object attributes and extend the editor

This is a brilliant feature. With the reflection mechanism, this feature is extremely convenient and powerful. You only need to write a script and add a public variable. Then you can set this value in the editor. Of course, Unity only recognizes built-in types. You must add [System. Serializable] to declare your own classes.

The editor Extension function of Unity is very powerful. You can easily overwrite the display Content and Display Mode in the attribute window, or add a button to implement a special function. The biggest purpose of Unity's built-in GUI is here.


11. Editor folder description

If you look at the vs project automatically generated by Unity, you may find that the number of projects may reach seven or eight. All the Editor-related script files of Unity must be placed under the Editor folder (whether it is the root directory or not, as long as the name is Editor). These files will generate a Unity-Editor project separately. All codes containing UnityEditor can only run in editor mode and cannot be published to windows or android.

(The third part is about Unity, and the fourth part is about the actual game solution)

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.