Unity3d Practice Series 07, the Enable or disable switch of the component, the visible or invisible switch of the object, and the corresponding event

Source: Internet
Author: User
Tags visibility

Create a Unity project.

In the Project window, in Asserts, add the _myscene folder.

Click "Save Scene" in "File" to name the scene and save it to the "_myscene" folder.

Create a gameobject of type "Plane" and resize it.

Create a gameobject of type "Cube" and resize it appropriately.

Using scripts to control the enable/disable of object components

Select "Cube" under the "Hierarchy" window and if you remove the "Meshe Render" in its corresponding "Inspector" window, you will see a hollow Cube in the Scene window. That is, the component can be declared as enable or disable in an explicit manner.

You can also control the visibility of a component in a script way.

In the Project window, in Asserts, add the _scripts folder.

Create a script named "Enablecomponent" under the "_scripts" folder, double-click, open and Edit in Visual Studio.

Using Unityengine;
Using System.Collections;
public class Enablecomponent:monobehaviour
{
    Public Meshrenderer Mmesh;
    Use this for initialization
    void Start () {
    
    }
    
    Update is called once per frame
    void Update () {
    
    }
}

Drag the "enablecomponent" script onto "Cube" in the "Hierarchy" window, where there is an "M Mesh" attribute in the Script component in its corresponding Inpector window.

Drag the cube from the Hierarchy window into the M Mesh property value box, and unity automatically recognizes the Meshrenderer on the Cube as a property value for M mesh.

Now, we want to make Cube hollow by pressing the SPACEBAR on the keyboard, setting the value of the "M Mesh" property of the cube Script component. Modify the "enablecomponent" script as follows:

Using Unityengine;
Using System.Collections;
public class Enablecomponent:monobehaviour
{
    Public Meshrenderer Mmesh;
    Use this for initialization
    void Start () {
    
    }
    
    Update is called once per frame
    void Update () {
        if (Input.getkeydown (keycode.space))
        {
            mmesh.enabled = false;
        }
    }
}

Save, and run the game.

When you press the SPACEBAR in the keyboard, the cube in the Game window disappears automatically.

If you want to toggle the visible/invisible state of the cube by using the SPACEBAR on the keyboard, modify the "enablecomponent" script as follows:


Save, and run the game.

When you press the SPACEBAR in the keyboard each time, the cube in the Game window is either visible or invisible.

Using scripts to control the active/inactive of objects

Create a sphere of type "Sphere" and adjust the position and size appropriately.

Create a script named "SetActive" under the "_scripts" folder, double-click, open and Edit in Visual Studio.

Using Unityengine;
Using System.Collections;
public class Setactive:monobehaviour {
    Use this for initialization
    void Start () {
        Gameobject indicates that the current object
        Gameobject.setactive (FALSE);
    }
    
    Update is called once per frame
    void Update () {
    
    }
}

Drag the "SetActive" script to "Sphere" in the "Hierarchy" window.

Save, run the game, the sphere does not appear.

Of course, the most straightforward way to object visibility is to set it explicitly in the Inspector window.

Onenable, Ondisable and Ondestry

This is a 3 response event function.

Onenable: Called when the object becomes active or active.
Ondisable: Called when an object becomes unavailable or inactive.
OnDestroy: Called when Monobehaviour will be destroyed. Note that the script is called when it is destroyed, not when the object is destroyed.

Create a script called "Disabledestroy", which is written as follows:

Using Unityengine;
Using System.Collections;
public class Disabledestroy:monobehaviour {
    void Onenable ()
    {
        Debug.Log ("script was Enabled");
    }
    void Ondisable ()
    {
        Debug.Log ("script was disable");
    }
    void OnDestroy ()
    {
        Debug.Log ("script was destroyed");
    }
    Use this for initialization
    void Start () {
    
    }
    
    Update is called once per frame
    void Update () {
    
    }
}

Select "Cube" in the "Hierarchy" window, delete the original script component, and drag the "Disabledestroy" script onto it.

Save, run the game, in the process of running, the "Disable Destroy (Script)" Tick off, stop running, the console display as follows:


Reference: Geek College Unity3d Course

Unity3d Practice Series 07, the Enable or disable switch of the component, the visible or invisible switch of the object, and the corresponding event

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.