unity c# SendMessage
SendMessage
GameObject.SendMessage("function name", parameters, SendMessageOptions);
parameter
Function name : The name of the function you are calling (you can't make a mistake).
Parameters : You can add parameters when sending messages. Because the called function has parameters, you must also pass them. As a person, if the calling function has no parameters, it can be omitted directly.
SendMessageOptions : As long as there are two or two points.
- The SendMessageOptions.RequireReceiver mode is the default, so when using the SendMessage function, there can be only one function name parameter. The function of SendMessageOptions.RequireReceiver is to report an error when the function you call cannot be found, reminding you that there is nothing to receive the sent message.
- SendMessageOptions.DontRequireReceiver , the function of this mode is to allow the existence of no received information and prevent the code from reporting errors.
effect
SendMessage is an api of a game object, which is used to send information to call the function you want to call. Mainly call a method in another script in another script. But first get the object with the method script.
example:
I have some problems with the scene I created. I created two 3dCubes in the 2d project. But there is no problem, as long as everyone understands it.
Next, add scripts for cube1 and cube2 respectively.
Cube1Send is added to Cube1, Cube2Get is added to Cube2;
The Cube1Send code is as follows:
public class cube1Send : MonoBehaviour
{
private GameObject receiveCube;
void Start()
{
receiveCube = GameObject.Find("Cube2");
}
void OnMouseDown()
{
if (this.gameObject)
{
string str = "Cube1被点击了";
receiveCube.SendMessage("GetMessage",str);
}
}
}
The Cube2Get code is as follows:
public class Cube2Get : MonoBehaviour
{
void GetMessage(string n)
{
Debug.Log("接收信息:" +n);
}
}
Run Unity, when you click Cube1, it will happen: In the
above code, only Cube2 that gets the calling function can be called. This is the first condition.
If you don't get it, can you directly use your own Cube1 to send a message and call GetMessage to issue it?
This is to use its own call to call the foot method of other objects:
this will report an error, and it is not possible to debug in this way.
What happens without parameters?
Send message parameter passing, but a function call, no arguments will happen?
This will also report an error, and there is no corresponding receiver, so the calling function does not pass parameters. What kind of parameters are passed should be consistent with the calling function parameters.
SendMessageOptions
SendMessageOptions.DontRequireReceiver
SendMessage defaults to SendMessageOptions.RequireReceiver, the above examples are all that way, so there is no need to elaborate on one or two, because when there is a problem, it will directly report an error to you.
When using SendMessageOptions.DontRequireReceiver:
Cube2Get's GetMessage still has no parameters.
When you click Cube1, you will find that it can still run without error, and you can call the GetMessage function.
Run when the GetMessage function parameter is of int type: the
parameters are inconsistent but an error will be reported, saying that the parameters do not match.
When the GetMessage function is overloaded: In
this case, the corresponding function method will be automatically matched, and the int GetMessage will not be called.
Called access level
I don't know if you have carefully explored the access rights of the GetMessage function I called.
The GetMessage I wrote is private, not public, so SendMessage can call function methods with arbitrary permissions ! !