Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
0
Question by Yarden-Raveh · Feb 07, 2014 at 12:42 PM · editorinterfacepublicextension-methodsexpose

Exposing interface implementation

So i have many objects of different types implementing the interface IMatrixNode. I now have a object Matrix. I tried declaring a variable like so:

 Public IMatrixNode[] column;

It is my understanding now that you can't expose interfaces in such a way. If i declare it as a GameObject array however it will also be able to recieve objects that do not implement the interface. Is there a way to limit a variable so it will only be able to recieve GameObjects that implement IMatrixNode in the editor?

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

3 Replies

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

Answer by Jamora · Feb 07, 2014 at 03:05 PM

You have two options (that I know of) to have the Editor accept only your interface types in the general case:

1) Do as IUnified, and have a generic MonoBehaviour class for which you create a property drawer

2) Have a collection that is visible in the Editor (e.g. Monobehaviour, GameObject...). Then provide an implementation for the OnValidate method for the class that has your interface-collection. An example implementation could be one where you check that each element in the collection implements your interface. If one doesn't, it is removed.

If it's just for that one class, then a possibilty is to create a custom editor.

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 Yarden-Raveh · Feb 08, 2014 at 11:02 AM 0
Share

Thnk you OnValidate worked great. Here is an example of what i did if anyone else is interested: void OnValidate() { if (light_node != null) { I$$anonymous$$atrixNode test = (I$$anonymous$$atrixNode)light_node.GetComponent (typeof(I$$anonymous$$atrixNode)); if (test == null) { light_node = null; } } }

avatar image
0

Answer by nesis · Feb 07, 2014 at 02:03 PM

I'm thinking I did what you're attempting with this (Unity 4.3):

MyInterface.cs:

 using UnityEngine;
 using System.Collections;

 public interface MyInterface {

     string GetSomeClassId();
     void DoSomeMaths(float a, float b);
     bool IsSomething();
 }

bar.cs:

 using UnityEngine;
 using System.Collections;

 public class bar : MyInterface {

     public string GetSomeClassId() {
         return "bar is cool";
     }

     public void DoSomeMaths(float a, float b) {
         Debug.Log (a+" * "+b+" = "+(a*b));
     }

     public bool IsSomething() {
         return true;
     }
 }

foo.cs:

 using UnityEngine;
 using System.Collections;

 public class foo : MyInterface {
     
     public string GetSomeClassId() {
         return "foo is cooler";
     }
     
     public void DoSomeMaths(float a, float b) {
         Debug.Log (a+" + "+b+" = "+(a+b));
     }
     
     public bool IsSomething() {
         return false;
     }
 }

TestOutput.cs:

 using UnityEngine;
 using System.Collections;

 public class TestOutput : MonoBehaviour {

     public MyInterface[] tests = {new bar(), new foo(), new bar()};

     public void Start () {
         foreach (MyInterface t in tests) {
             t.DoSomeMaths(2,8);
             Debug.Log("GetSomeClassId(): "+t.GetSomeClassId());
             Debug.Log("IsSomething(): "+t.IsSomething());
             Debug.Log("===");
         }
     }
 }
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 Yarden-Raveh · Feb 07, 2014 at 02:35 PM 0
Share

Thanks for the reply but this is not what I meant. In your example I need to hard code every object that is implementing the interface. I want to be able to add an object from the editor like any other public array however I want to limit that array so it will only be able to recieve objects that implemented the interface as input.

avatar image nesis · Feb 07, 2014 at 03:01 PM 0
Share

If exposing an interface to the Inspector is what you're wanting, you'll have to do a bit of work, as suggested in this similar question. Essentially, you'll need to make a custom editor script for it, or use an existing workaround others have made (one of which is posted in that link).

avatar image
0

Answer by vexe · Jan 04, 2015 at 12:19 AM

A 3rd solution to add to @Jamora's list, is to implement a custom serialization system that properly handles edge cases that Unity doesn't do a good job at.

I did just that in VFW, a custom serialization and drawing system that provides polymorphic serialization and proper exposure to interfaces/abstract types. An added benefit to having a better serialization system is that you could handle pretty much everything else that Unity can't (properties, dictionaries, delegates etc)

It's free, Check it out!

Comment
Add comment · 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

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

21 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

Related Questions

Animator dialog doesn't pop up when sprites are dragged 0 Answers

Web browser tab editor extension? 0 Answers

How to expose UnityEvent in editor? 1 Answer

Change colour of background in editor's object previews? 0 Answers

expose public property (not variable) in inspector 9 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