官术网_书友最值得收藏!

Instantiating scripts

Each script file in Unity defines one main class that is like a blueprint or design that can be instantiated. It is a collection of related variables, functions, and events (as we'll see soon). By default, a script file is like any other kind of Unity asset, such as meshes and audio files. Specifically, it remains dormant in the Project folder and does nothing until it's added to a specific scene (by being added to an object as a component), where it comes alive at runtime. Now, scripts, being logical and mathematical in nature, are not added to the scene as tangible, independent objects as meshes are. You cannot see or hear them directly, because they have no visible or audible presence. Instead, they're added onto existing game objects as components, where they define the behavior of those objects. This process of bringing scripts to life as a specific component on a specific object is known as instantiation. Of course, a single script file can be instantiated on multiple objects to replicate the behavior for them all, saving us from making multiple script files for each object, such as when multiple enemy characters must use the same artificial intelligence. The point of the script file, ideally, is to define an abstract formula or behavior pattern for an object that can be reused successfully across many similar objects in all possible scenarios. To add a script file onto an object, simply drag-and-drop the script from the Project panel onto the destination object in the scene. The script will be instantiated as a component, and its public variables will be visible in the Object Inspector whenever the object is selected, as shown in the following screenshot:

Instantiating scripts

Attaching scripts onto game objects as components

Variables are considered in more depth in the next section.

Tip

More information on creating and using scripts in Unity can be found online at http://docs.unity3d.com/412/Documentation/Manual/Scripting.html.

主站蜘蛛池模板: 本溪市| 甘谷县| 长兴县| 沭阳县| 西畴县| 通河县| 麻栗坡县| 静安区| 蓬溪县| 安西县| 教育| 探索| 繁昌县| 宜章县| 昌吉市| 正宁县| 正安县| 永德县| 东乌| 巩留县| 葵青区| 花莲市| 墨脱县| 泾阳县| 阳东县| 阳朔县| 扎兰屯市| 阳东县| 榆树市| 北京市| 自治县| 云阳县| 九江市| 思茅市| 伊通| 万全县| 田东县| 布尔津县| 满洲里市| 绍兴市| 确山县|