Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ThePilgrim · Dec 05, 2016 at 10:44 PM · c#attribute

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();
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThePilgrim · Dec 06, 2016 at 01:54 PM 0
Share

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.

avatar image UnityCoach ThePilgrim · Dec 07, 2016 at 03:01 AM 0
Share

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 );
     }
 }
avatar image ThePilgrim UnityCoach · Dec 07, 2016 at 03:26 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

269 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set SyncVarAttribute.hook? 1 Answer

Can I set one enum value equal to another enum value? 2 Answers

With a custom attribute & drawer, how do I allow multiple attributes? 0 Answers

How to make Input Field with middle text align? 2 Answers

Object cant be found in new scene 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges