[Unity3d] C # delegated application in unity

Source: Internet
Author: User

In C #, there is a very practical type, namely the delegate type, which is also a type that beginners can hardly understand. We often talk about the delegate and event together, in fact, there are some differences between the two. The delegate is a type, and the Event is an instance (one in the delegate ). Next I will explain the C # Delegate application in unity by referring to an example on the Internet.

Create a new script named TestDelegate. The main function is to click the Chinese button to output the Chinese name and click the English button to output the English name. Here, two functions are implemented respectively. If you do not need to delegate, you can use if else and switch to implement it. Compared with new users, the code is usually less frequently, so we need to study our code carefully. Let's take a look at the code below:

Using UnityEngine; using System. Collections; public class TestDelegate: MonoBehaviour {// defines a delegate (the format is not very similar), which is used to point to a function. (Pointer function in c ++) // The param parameter is named private delegate void DebugString (string param );////// Output the Chinese name ///Public void DebugNameOfChina (string str) {Debug. Log ("Chinese name:" + str );}////// Output the English name ///Public void DebugNameOfEnglish (string str) {Debug. log ("English Name:" + str) ;}// defines a delegate variable event private DebugString handlerDebugString; void OnGUI () {if (GUILayout. button ("output Chinese name") {// if I want to output a Chinese name, assign the value to handlerDebugString to the function DebugNameOfChina handlerDebugString = DebugNameOfChina; handlerDebugString ("ding Xiaowei");} else if (GUILayout. button ("Debug English Name") {// if I want to output an English Name, assign the value to handlerDebugString to the function DebugNameOfEnglish handlerDebugString = DebugNameOfEnglish that outputs the Chinese Name; handlerDebugString ("DingXiaowei ");}}}

I have referenced some online tutorials here. @ is a good tutorial for conflict with reality, but I also want to add my own understanding, I personally understand that this delegate is a bit similar to the pointer function. Defining a delegate variable is similar to the function pointer in C/C ++. If it points to a function, it calls the function to execute the function.

Run the following command to check the effect:



The Class C/C ++ is easier to understand than the function pointer. In fact, this is the case. If you want to use a function, you can point to it, then call the method of this function. In fact, the delegate also has the + = and-=, that is, the multi-channel broadcast mechanism. The following is an example:

Using UnityEngine; using System. Collections; public class TestDelegate: MonoBehaviour {// defines a delegate (the format is not very similar), which is used to point to a function. (Pointer function in c ++) // The param parameter is named private delegate void DebugString (string param );////// Output the Chinese name ///Public void DebugNameOfChina (string str) {Debug. Log ("Chinese name:" + str );}////// Output the English name ///Public void DebugNameOfEnglish (string str) {Debug. log ("English Name:" + str) ;}// define a delegate variable event private DebugString handlerDebugString; void Start () {// assign handlerDebugString = DebugNameOfChina to the delegate object; handlerDebugString ("ding Xiaowei"); Debug. log ("................................ "); // Add an event to the delegate object, called multi-channel broadcast handlerDebugString + = DebugNameOfEnglish; handlerDebugString (" DingXiaowei "); Debug. log ("................................ "); // output Chinese characters and try handlerDebugString (" ding Xiaowei "); Debug. log ("................................ "); // subtract an event handlerDebugString-= DebugNameOfChina; handlerDebugString (" DingXiaowei ") from the delegate object ");}}

Let's guess what the result is. Is it the same as what you expected?


The second and third, that is, the red part, may be prone to errors. They executed the two events respectively. Then we added an event, so execute it all over again, and then the parameter is the parameter in the delegate method. delegate is the basic knowledge of C #. It has nothing to do with unity. The unity knowledge is only a UI display interface, C # is supported in the background. Therefore, when learning unity development, C #/. NET basics are still necessary!


================================== Publisher Ding Xiaowei CSDN blog column ================ =====

MyBlog: http://blog.csdn.net/dingxiaowei2013 MyQQ: 1213250243

Unity QQ group: 858550 cocos2dx QQ group: 280818155

==================================== Mutual learning, common progress ==============================

Reprinted please indicate the source: http://blog.csdn.net/dingxiaowei2013/article/details/18428727

Welcome to my weibo: http://weibo.com/u/2590571922


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.