Unity uses the [ClientRpc] attribute to change the behavior of a function. How can I do something similar?
Looking at the Unity manual entry for remote actions, it says you can assign the [ClientRpc] attribute to a function. Then, when you call that function, it somehow knows to run it on the client machines. From the example code they give, it looks like they just simply call the function with the attribute in the normal way, and it somehow works.
I am interested in knowing how to duplicate functionality such as this in my own code. I am not interested in duplicating the ClientRpc logic, but rather simply figuring out how to add special logic to a function call using just an attribute.
For instance, how could I make it print out "Hello World" whenever a function with the attribute [PrintHelloWorld] is invoked?
public class MyTestClass
{
[PrintHelloWorld]
public void MyTestFunction ()
{
// funciton doesn't do anything, but "Hello World" will be printed when called because of Attribute.
}
private void Start ()
{
MyTestFunction();
}
}
Answer by UnityCoach · Dec 05, 2016 at 11:33 PM
You can declare your own attributes
https://msdn.microsoft.com/en-us/library/aa288454(v=vs.71).aspx
Thanks! Do you happen to know how to implement special functionality that runs when a function with a specific attribute is invoked? For instance, in the example above, if I created a custom attribute called [PrintHelloWorld] and attached it to $$anonymous$$yTestFunction(), is there a way to define code that runs each time any function with the attribute [PrintHelloWorld] is called?
I didn't originally think it was possible with attributes until I saw that Unity somehow did it with their [ClientRpc] attribute.
Hey, so, I played a little bit with it. To me, it seems this more something you use when you build a Framework, or Editor tools. It allows you to quickly parse the attributes given to classes and members, to do something in the editor, like the Range and Header attributes.
Here's my test:
using UnityEngine;
using System.Collections;
using System;
[AttributeUsage(AttributeTargets.All)]
public class HelpAttribute : System.Attribute
{
public readonly string Url;
private string topic;
public string Topic
{
get { return topic; }
set { topic = value; }
}
public HelpAttribute(string url)
{
this.Url = url;
}
}
[HelpAttribute ("http://audc.jikkou.ca")]
public class Test
{
string testStr;
}
public class AttributeTestClass : $$anonymous$$onoBehaviour
{
public Test test;
private void Start()
{
System.Reflection.$$anonymous$$emberInfo info = typeof(Test);
object [] attribute = info.GetCustomAttributes(typeof (HelpAttribute), true);
Debug.Log( ((HelpAttribute) attribute[0]).Url );
}
}
Thanks for the example code! That usage makes a lot of sense. Unity must have done some wacky behind-the-scenes stuff with their [ClientRpc] attribute. I am implementing my own multiplayer system built on top of S$$anonymous$$m's API. When I saw that Unity was using attributes to define a function to automatically be sent to the associated objects on client computers, I liked how clean and easy it was. I wanted to implement something similar for remote method invocations (R$$anonymous$$Is) to where when I called a function with the [R$$anonymous$$I] attribute, it would automatically notify all clients so they could duplicate the function call. I ended up just defining a special R$$anonymous$$I function that takes in the name of the function that I wish to invoke on clients. Not as elegant as using an attribute like Unity did, but it works just fine.