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 Julien-Lynge · Aug 02, 2012 at 08:20 PM · editorvariableinspectorbehaviourassignment

Defining a public variable with 'where'

I would like to define a public, user-assignable variable that is a Behaviour. The Behaviour will be a target script to call when certain conditions are true. This is the easy part

I also want to define a specific interface to implement, so I have some known public functions to call. However, if I just ask for the interface like so:

 public EventReceiver EventController;

The user can't assign a MonoBehaviour from the editor - the only way to be able to assign is to use the following:

 public Behaviour EventController;

What I'm wondering is, is there any way in C# to do something like this:

 public Behaviour EventController where EventController : EventReceiver;

?

I know that I can check the type at runtime and deliver a message to the user if they've assigned an incorrect target - this is what I've already done. I'm just curious if there's a better way.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ScroodgeM · Aug 02, 2012 at 08:25 PM

you can check it in editor on assigning action instead of runtime, making incorrect assigning impossible.

http://docs.unity3d.com/Documentation/Components/gui-ExtendingEditor.html

'Making a Custom Editor' chapter

Comment
Add comment · Show 2 · 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 Julien-Lynge · Aug 02, 2012 at 08:38 PM 0
Share

I figured someone would bring up a custom editor :). I know that's an option too, but it's a lot of work for such a $$anonymous$$or thing I'm trying to do. Still, if nobody has any better idea about how to do it directly in C#, I'll definitely give your answer the check mark.

avatar image ScroodgeM · Aug 02, 2012 at 08:46 PM 0
Share

ok. it's will be interesting for me too if there's another way. i didn't found it and currently using custom editors. with 'drawdefaulteditor' it's just a few lines more to achieve what you want.

avatar image
0

Answer by whydoidoit · Aug 02, 2012 at 10:03 PM

Well you could make it a base class derived from MonoBehaviour - perhaps an abstract one, that would allow editor assignment - but not the query for an interface.

Comment
Add comment · Show 1 · 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 ScroodgeM · Aug 03, 2012 at 07:17 AM 0
Share

it can be a soulution in very simple case, but if you need more then one derive (what is usually so).... or you derived your class already from some abstract....

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

9 People are following this question.

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

Related Questions

Is there a Function Callback for when a vairable is altered in the Inspector? 1 Answer

Public Scene Variables not appearing in the inspector 2 Answers

Hide public field on base class but show it on derived class 1 Answer

Unity Editor - Class variable value contrain 4 Answers

How do you clear a variable in the inspector? 4 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