C # Unity 3D notes compiled by programmers (13th): Unity 3D is based on the component concept,

Source: Internet
Author: User

C # Unity 3D notes compiled by programmers (13th): Unity 3D is based on the component concept,

If you have been familiar with design patterns and software architecture programming ideas, you will understand the outstanding design principles:"Combination is better than inherited".

This sentence is very short, but when I started to learn OOP, I really did not understand it very well (I personally studied it as an example ).

 

Inheritance of OOP

When designing the Player, In order to reuse the functions of A, B, and C, I began to write A, B, and C in accordance with inheritance, some delimiters such as Virutal \ Override \ Protected are added, so there is no problem with the function, that is, some awkwardness. For example, the Start and Update methods can only be processed using the template method in A. If the Start and Update methods are directly used in B, C, and Player, this will lead to strange problems; at the same time, some burdens are invisible in the inherited base classes. For players, they have to use A, B, and C functions and variables (non-private ).

The entire Link is changed:

  • Player is a
  • Player is a B
  • Player is a C

I think it is a bit awkward.

 

Combined idea of OOP

In the past, many combination ideas were used to build tree and leaf models, such as the Network Element Model in China Telecom. This idea belongs to the core idea of Unity 3D-component. The Start and Update functions can be freely used in Player, A, B, and C (do not consider the execution sequence. The sequence of script components can be adjusted externally, but it is of little significance ), most importantly, the relationship is streamlined-the protagonist becomes more active and active.

  • Player have a
  • Player have a B
  • Player have a C
    In Unity 3D, almost all reusable components are encapsulated as components, such as transform, rigibody, render, camera, and ,***. cs script. To facilitate the use of non-built-in components, you can use gameObject. addComponent <T> (), gameObject. getComponent <T> () to add and obtain components (generally custom scripts ).

 

Here is a practical example. In the second version of "Space Shooting Game" in "Unity 3D mobile game development", there is a demand, you need to add the automatic destruction function for reusable gameobjects in the game (by using a time timer or trigger). The code is very simple. There are less than 100 lines of gameobjects to be added: 5 or 6, although the workload is not large, you cannot copy the code once.

At first, I did it according to OOP inheritance,

 

After reading it for a few days, I was very upset. Later I reconstructed it as follows:

In this way, the function of the automatically destroyed component is more flexible and flexible, that is, it does not have to stick to the static inheritance idea.

From This refactoring process, I learned how the Unity 3D component thought is shining ......

Appendix: complete automatic destruction component code:

Public class AutoDestoryComponent: MonoBehaviour
{
# Region ICanCache
Public Participant system [] m_pss = null;
Public int m_life = 1; // 3
Public float m_AutoDeadTime = 3; // 3 S Automatic destruction

Private int m_life_Base = 3; // 3 Records [used for recovery]
Private float m_AutoDeadTime_Base = 3; // 3 S Automatic destruction [for restoration] [-1: indicates that automatic destruction is not performed, such as Enemy]

Void Update ()
{
// Automatic destruction is required
If (m_AutoDeadTime_Base> = 0)
{
M_AutoDeadTime-= Time. deltaTime;

If (m_AutoDeadTime <= 0)
{
InnerDead ();
Return;
}
}

If (m_life <= 0)
{
InnerDead ();
}
}

/// <Summary>
/// Set automatic data destruction
/// </Summary>
/// <Param name = "life_base"> default lifecycle </param>
/// <Param name = "autoDeadTime_base">-1 indicates that the data is not automatically destroyed. Other data indicates the destruction time (unit: s). </param>
Public void SetBasePara (int life_base = 1, float autoDeadTime_base =-1)
{
M_AutoDeadTime = m_AutoDeadTime_Base = autoDeadTime_base;
M_life = m_life_Base = life_base;
}

// Enable or not
Public bool IsUse {get; set ;}
// Post location
Public Vector3 DeathPosition
{
Get
{
Return new Vector3 (2000,200 0, 2000 );
}
}

// Resurrection
Public void Init (Vector3 position, Quaternion rotation)
{
Transform. gameObject. SetActive (true );
Transform. position = position;
Transform. rotation = rotation;
IsUse = true;
Foreach (participant system item in m_pss)
{
Item. Play (true );
}

// Some Loops
M_life = m_life_Base;
M_AutoDeadTime = m_AutoDeadTime_Base;
}

Private void InnerDead ()
{
IsUse = false;
Transform. position = DeathPosition;
Foreach (participant system item in m_pss)
{
Item. Stop (true );
}

This. gameObject. SetActive (false );
}
# Endregion
}

 

 

Including the built-in Audio, Transform, Camera, Image, and Button. A GameObject is a container without an Image. You only need to create an empty GameObject and add an Image Component to an extremely Image GameObject object.

In Unity3D, the concept of GameObject. ID is rarely used. Instead, different gameobjects are distinguished by GameObject. Tag and GameObject. name, And the Tag and name are not unique.

 

Conclusion:In Unity3D, everything is Component.

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.