Unity Interface serialization-expose Interface field in Inspector

Source: Internet
Author: User

Unity has some quirks about their inspector, so as a preface they is listed here:

  • if you add A [serializable"  attribute to a class, Unity ' s Inspector'll attempt to show all Public fields inside that class.
  • any class Extending monobehaviour  automatically has the [serializable"  attribute
  • unity ' s inspector would attempt to display any private field with The [serializefield"  attribute.
  • unity s Inspector will not  attempt to display generic types or interfaces, with the exception of  LIST<T>
  • Unity's inspector won't attempt to display properties. A common workaround is to has a private backing field for your property with [SerializeField] attached. Setters won ' t is called on the value set in the inspector, but since that's only set pre-compilation, that's acceptable.
  • Unity has aPropertyDrawerClass can extend to control how a type is displayed in the inspector. ThePropertyDrawerFor a interface or generic type would be ignored.
When we want to Serialize the Interface,What are we can do?

Unity, by itself, does isn't expose fields that is of an interface type. It is possible to manually enable this functionality by implementing a custom inspector each time as Mike 3 have pointed OU T, but even then thereference would isn't be serialized ("remembered" between sessions and Entering/exiting Playmod e).

It is possible however to create a serializable container object, wraps around a Component field (which is SE rialized) and casts to the desired interface type through a generic property. And with the introduction's custom property drawers to Unity, you can effectively expose a serialized interface field I n Your scripts without have to write a custom inspector/property drawer each time.

Some Simple Demo Code:

Using Unityengine; [System.serializable]public class Interfacehelper  {public Component target;public T getinterface<t> () where T:class{return target as T;}}

And the Custom property drawer:

Using unityengine;using unityeditor; [Custompropertydrawer (typeof (Interfacehelper))]public class Editorinterfacehelper:propertydrawer {public override void Ongui (Rect pos, Serializedproperty prop, Guicontent label) {editorgui.beginproperty (POS, label, prop);p OS = Editorgui.prefixlabel (Pos,guiutility.getcontrolid (focustype.passive), label); Editorgui.propertyfield (Pos,prop. Findpropertyrelative ("target"), Guicontent.none); Editorgui.endproperty ();}}

Usage:

public interface idata{void GetData ();}
#definepublic Interfacehelper Datasrc;...//call the functiondatasrc.getinterface<idata> (). GetData ();

The interface field in Inspector are like this:


Of course, you can use an abstract class instead sometimes,but if you do, and you'll miss the benefit of mul-Inher it.

Reference:

Http://codereview.stackexchange.com/questions/65028/inspector-interface-serializer

Http://answers.unity3d.com/questions/46210/how-to-expose-a-field-of-type-interface-in-the-ins.html

Http://answers.unity3d.com/questions/783456/solution-how-to-serialize-interfaces-generics-auto.html

Unity Interface serialization-expose Interface field in Inspector

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.