Unity Learning note 13--Code dynamically loading prefab presets

Source: Internet
Author: User

When doing some functional development, we often make reusable objects into . Prefab Preset objects, then store the presets in the Resources directory , and then dynamically load them into the scene and instantiate them when used. For example, bullets, special effects, and even audio can be created as presets.


First, the preset dynamic loading to the scene:

A preset to be able to display through code control in the scene, requires three steps, here we take the dynamic loading Monster blood bar as an example to analyze a common misunderstanding:

1. Pre-set resource loading:

        Load the default body resource        gameobject Hp_bar = (gameobject) resources.load ("Prefabs/hp_bar");

       

through the above operation, to download from the resource directory into the Hp_bar.prefab preset, with a Gameobject object to store, at this time the preset object is not really loaded into the scene, because the operation has not yet materialized.


2. Instantiation of the preset body:

In the first step we have loaded the pre-set resources, but the instantiated resources do not appear in the scene, so the second no we need to instantiate the resources, Instantiation is done using the Monobehaviour.instantiate function, which essentially clones an object from a preset resource that has exactly the same properties as the preset and is loaded into the current scene:

        Instantiation of the preset body        instantiate (Hp_bar);

After the above code is completed, an instantiated object appears in the current scene and can be found in the hierarchy column:


Because we do not have any property settings for this instantiated object, its properties are consistent with the original properties of the preset, and its parent node defaults to the scene's external layer.


3. Instantiate object property settings (optional):

after completing the above steps, we can already see the instantiated object in the scene, but usually we like the hierarchy between our objects, and it also makes it easier for us to manage objects uniformly, rather than hierarchy see a bunch of scattered objects, so we need to set the name of the object is the parent node and other properties.

( common error: properties are set on uninitialized Hp_bar, and properties after the setting are not valid after instantiation. This is because the last thing we show in the scene is not the resource object before the instantiation, but rather a cloned object, so if the property you want to set is in effect in the last object shown, we need to set the object after the instantiation. )

The correct setup code is as follows, and you can see that the instantiated object has been successfully hung onto the parent node of the canvas:

        Gameobject Hp_bar = (gameobject) resources.load ("Prefabs/hp_bar");        Gameobject Muicanvas = Gameobject.find ("Canvas");        Hp_bar = Instantiate (Hp_bar);        Hp_bar.transform.parent = Muicanvas.transform;


In fact, the first two steps can be completed together, the following simplified wording:
        Gameobject Hp_bar = (gameobject) Instantiate (Resources.load ("Prefabs/hp_bar"));        Gameobject Muicanvas = Gameobject.find ("Canvas");        Hp_bar.transform.parent = Muicanvas.transform;


Unity Learning note 13--Code dynamically loading prefab presets

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.