Use Cases of Unity3D game development I: Creating type ing and unity3d Game Development

Source: Internet
Author: User

Use Cases of Unity3D game development I: Creating type ing and unity3d Game Development


In general, the main application scenarios of Unity are as follows:

  • Create type ing
  • Used in singleton Mode
  • Used for dependency Injection

Next, we will introduce each application scenario one by one.


When creating an object, there is no doubt that type ing is an unavoidable problem, and it is also the fundamental starting point of a series of factory models. Type ing is the fundamental principle of object-oriented design-"interface-oriented programming rather than implementation programming" and "dependency abstraction rather than specific classes"-implementation in applications, provide strong support.

We know that Unity provides the object container. How does this container index? That is to say, how are the units in the container identified? In Unity, there are two main methods to identify. One is to directly use an interface (or a base class) as the ID key, and the other is to use an interface (or a base class) the combination with the name as the identification key. The value corresponding to the key is a specific class.


  • Use the interface type as the identification key

In fact, the previous example is an application scenario where the interface type is used as the ID key.

It should be noted that Unity provides both generic and non-generic functions to ensure that Unity does not support generic environments. We can use any method (generic and non-generic) in the Code, or use it together as needed. For example, you can use the generic form to register the ing, and then use the non-generic form to obtain the object instance.

The following code demonstrates non-generic usage:



Ps: The following examples and subsequent examples will not demonstrate non-generic usage.

  • Use a base class as the identification key

Using a base class as the identification key is essentially the same as using an interface type as the identification key. It should be noted that the base class does not necessarily refer to the abstract class. The following is an example of using a base class as the ID key:




Interestingly, if container. RegisterType (); is used, Resolve can be used. However, if container. RegisterType (); is used, an exception is reported during runtime.

 

  • Use the combination of interfaces (or base classes) and names as the identification key

To register multiple mappings using the same interface (or base class), you can specify a name to differentiate each ing. When you need Resolve, you can use the combination of the specified interface (or base class) and name as the identification key to obtain the appropriate type of objects. The following example uses the combination of interfaces and names as an example. The combination of the base class and name is the same as the ID key:



It should be noted that the registration name is a string and can contain spaces if needed. They are case sensitive. For example, the names "Mymapping" and "MyMapping" refer to two different registration mappings.


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.