Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Kurochi · Sep 03, 2013 at 07:23 AM · classdelegateparameter

Delegates and class constructors

I seem to not be able to figure delegates out. I need to use a delegate as parameter for a class constructor, to be able to access the constructor anytime directly from the class.

What I have:

 public class Cheat{
     public string cheat;
     
     public Cheat(string cht){
         cheat = cht;
     }
 }

What I want to do with this:

 Cheat cheat = new Cheat("cheat", FUNCTION/DELEGATE);

Then I want to be able to use the thing like this:

 cheat.assignedDelegate();
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
0
Best Answer

Answer by robhuhn · Sep 03, 2013 at 07:37 AM

This site explains how delegates work: http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx

So basically you create a delegate

 public delegate void CheatDelegate();

The delegate here is of type CheatDelegate

 private CheatDelegate myDelegate;

This type can be treated like any other type e.g. to pass it to a constructor

 public Cheat (CheatDelegate d)
 {
     this.myDelegate = d;
     myDelegate();
 }

And finally create the object and pass an anonymous delegate

 new Cheat( delegate()
 {
     Debug.Log("my delegate");
 });

Or a named method

 new Cheat(new CheatDelegate(MyMethod));
 ...
 void MyMethod()
 {
     Debug.Log("delegate from a named method");
 }

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 Jamora · Sep 03, 2013 at 08:03 AM 0
Share

When you get fed up with having to explicitly declare your delegates (i.e. type public delegate void funcName(); and then make one) for just another private delegate, you can start using System.Action or System.Func. You can have up to 16 parameters and the return value for Func for those delegates.

Still, sometimes it's just a good idea for clarity and readability reasons to declare your own delegates.

To illustrate: the first two lines of code can be compressed into

 private System.Action myDelegate;

And the third line would be

 public Cheat (System.Action d)

avatar image robhuhn · Sep 03, 2013 at 08:24 AM 0
Share

I thought Func is .net 3.5 and unity supports a .net 2.0 subset?

avatar image Kurochi · Sep 03, 2013 at 08:40 AM 0
Share

Thanks, it worked.

avatar image robhuhn · Sep 03, 2013 at 08:48 AM 0
Share

Good. $$anonymous$$ay be you want to mark it as an answer.

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

18 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

Related Questions

Using a static class & pass in gameObject or just add same script to multiple objects 1 Answer

Parameter Name Need "Some Generator Here" is a duplicate 2 Answers

Creating new instances of a child class within a function's for loop after it's passed as a parameter of the parent class type 1 Answer

GUI set max amount of characters for Label 1 Answer

How to pass classes of a class to a function's parameter? 1 Answer


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