Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by superpig · Jan 14, 2011 at 12:39 PM · interfacedesigndelegatepattern

Using the Delegate Object design pattern in Unity

In native iOS development, one common pattern is the Delegate Object pattern.

The idea is that you've got some common UI component - say, a grid view - but, as it's a common component, it doesn't have any logic about how many rows or columns it should have, or what it should put in each cell, or whether cells are selectable, and so on. So, what it does is to declare an interface that describes all these undefined things, and then ask for an object that implements that interface - a Delegate Object. (It's similar to C#'s delegate functions, but grouping a bunch of functions together into an interface).

I've got a collection of behaviours and objects set up that represent the game's main menu, but I want to keep all the logic for actually starting a new game, loading a game, saving a game, etc, out of there. I figured that the Delegate Object pattern would be good for this; I could declare interface IMainMenuDelegate { void StartNewGame(); void LoadGame(string savePath); } and so on, have the main menu code hold a reference to an IMainMenuDelegate, and have it call it when interesting things happen.

Where I run into problems is wiring up the delegate. I can write a MonoBehaviour that implements IMainMenuDelegate quite happily, but because IMainMenuDelegate itself doesn't derive from UnityEngine.Object, I can't have it show up as a field in the inspector, or FindObjectOfType(), or even GetComponentOfType().

The only option that remains is to wire it up in code. I could do that, but I'd quite like to avoid it if possible - it's not very elegant.

The only other solution I've seen has been to implement IMainMenuDelegate not as a C# interface, but as a MonoBehaviour that contains a bunch of C# delegates. This is kinda clever in that you can call the resulting component as if it were an interface, but it requires quite a lot of funky wireup, and in theory makes it possible for some of the methods to fail to be implemented, if you don't set the delegates. Obviously with an interface you could fail to wire up the delegate object entirely, but at least that way it's all-or-nothing.

Anyone have any ideas?

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 StephanK · Jan 14, 2011 at 01:38 PM

Have you tried creating an Abstract class that implements the IGameMenu interface and inherits from MonoBehaviour?

Comment
Add comment · Show 3 · 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 superpig ♦♦ · Jan 14, 2011 at 04:02 PM 0
Share

I haven't. If I'm doing that, though, then wouldn't that render the interface obsolete? Also my delegate object would need to inherit from the abstract class, so I wouldn't be able to have it inherit from anything else...

avatar image StephanK · Jan 16, 2011 at 11:57 AM 0
Share

It won't make the interface obsolete neccessarily, imagine you may want to have a special type of Game$$anonymous$$enu, that uses the IGame$$anonymous$$enu interface + some other interface. Then you could have an abstract class that uses the IGame$$anonymous$$enu interface and another one that uses IGame$$anonymous$$enu + IGameController (or whatever you come up with). So if you use an abstract class implementing the interface you could still create an abstract class C that inherits from A but also implements interface B.

avatar image Ippokratis · Jan 16, 2011 at 01:30 PM 0
Share

Angry Ant implemented a solution that demonstrates the use of delegates in the creation of a menu system. The code is [here][1]. He also includes a Unity project based on this code. I cannot fully understand what you want, but I hope that this code might help you :) [1]: http://eej.dk/angryant/general/tipsandtricks/building-a-menu-of-delegates-and-enums/

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

No one has followed this question yet.

Related Questions

Reliable / intelligent user input handling 0 Answers

General and OOD advice,General and OOD advice based 0 Answers

Design Patterns (Singleton Issue) 1 Answer

How to interact game objects? 1 Answer

Need to inherit from EventArgs AND ScriptableObject 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