[Unity3D Basics] Unity tutorials for beginners (2): basic classes of all script Components

Source: Internet
Author: User

Author: Wang Xuan Yi, source: http://www.cnblogs.com/neverdie/Reprinted, Please keep this statement. If you like this article, click [recommendation ]. Thank you!

Introduction

The last Time we talked about GameObject, Compoent, Time, Input, Physics, where Time, Input, and Physics are global variables in Unity. GameObject is a basic object in the game. GameObject is composed of Component. The GameObject itself must have a Transform Component, which also deepens our understanding of GameObject, that is, GameObject actually exists in the game scene, an object with a location.

But how can we manipulate this GameObject? This requires the introduction of the script component, that is, MonoBehaviour described today.

Life Cycle of MonoBehaviour

MonoBehaviour is the base class of all scripts in Unity. If you use JS, the script automatically inherits MonoBehaviour. If you use C #, You need to explicitly inherit MonoBehaviour.

When we use MonoBehaviour, we need to pay special attention to which rewrite functions are available, which will be called when some events occur in the game. The following are some of the most common rewritable functions in Unity:

  • Awake: Awake is called when a script instance is loaded. Most of us initialize the member variables in this class.
  • Start: it is called only before the first call of the Update function. Because it is called after Awake, we can put some variables dependent on Awake into Start for initialization. At the same time, we mostly execute StartCoroutine in this class to trigger some coroutine. Note that when writing a script in C #, you must use StartCoroutine to start a coroutine. However, if JavaScript is used, you do not need to do this.
  • Update: When MonoBehaviour is enabled, its Update is called at each frame.
  • FixedUpdate: When MonoBehaviour is enabled, its FixedUpdateFixed FrameCalled.
  • OnEnable: This function is called when the object becomes available or activated.
  • OnDisable: This function is called when the object becomes unavailable or inactive.
  • OnDestroy: This function is called when MonoBehaviour is destroyed.

The following figure shows how these classes are called in the life cycle of MonoBehaviour:

What is hard to understand in the above example is what exactly is Coroutine? Don't worry. We will write a blog to talk about it later.

 

Those pitfalls of MonoBehaviour
  • Private and protected variables can only be displayed in EXPERT mode. properties are not serialized or displayed in the view panel.
  • Do not use namespace)
  • Remember to useCache component search,That is to say, in the long-term method of MonoBehaviour, it is best to store components that are frequently accessed as private member variables.
  • In the game, we often encounter problems that need to detect the distance between the enemy and us. In this case, if we want to find all the enemies, it will obviously consume too much computing, therefore, the best way is to use Collider to indicate the attack scope, and then set the isTrigger of Collider to True. At last, OnTriggerEnter is used for distance detection within the attack range, which greatly improves program performance.
Summary

What is the most difficult to understand in MonoBehaviour is how Coroutine is implemented. But if you write Coroutine in a blog, it seems too long. I will write Coroutine content in the next blog.

Related Article

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.