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

  • If you add a [Serializable] attribute
    to a class, Unity's Inspector will attempt to show all public fields inside that class.
  • Any class extending Monobehaviour automatically
    has the [Serializable] attribute
  • Unity's inspector will 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>,
    which is hard-coded.
  • Unity's inspector will not attempt to display properties. A common workaround is to have a private backing field for your property with [SerializeField] attached.
    Setters won't be called on the value set in the inspector, but since that's only set pre-compilation, that's acceptable.
  • Unity has a PropertyDrawer class
    you can extend to control how a type is displayed in the inspector. The PropertyDrawer for
    an interface or generic type will be ignored.

When we want to Serialize the Interface,What we can do?

Unity, by itself, does not expose fields that are of an interface type. It is possible to manually enable this functionality by implementing a custom inspector each time as Mike 3 has pointed out,but
even then the reference would not be serialized ("remembered" between sessions and entering/exiting playmode).

It is possible however to create a serializable container object that wraps around a Component field (which is serialized) and casts to the desired interface type through a generic property. And with
the introduction of custom property drawers into Unity, you can effectively expose a serialized interface field in your scripts without having 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);
pos = EditorGUI.PrefixLabel(pos,GUIUtility.GetControlID(FocusType.Passive),label);
EditorGUI.PropertyField(pos,prop.FindPropertyRelative("target"),GUIContent.none);
EditorGUI.EndProperty();
}
}

Usage:

public interface IData
{
void getData();
}
#define
public InterfaceHelper dataSrc;
...
//call the function
dataSrc.getInterface<IData>().getData();

The interface field in inspector is like this:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3ViZXNreQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

Of course, You can use abstract class instead sometimes,but if you do that, you will miss the benefit of mul-inherit.

參考:

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的更多相关文章

  1. 【Go入门教程6】interface(interface类型、interface值、空interface{}、嵌入interface、反射)

    interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...

  2. 【Go入门教程8】interface(interface类型、interface值、空interface{}、嵌入interface、反射)

    interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...

  3. Unity Low-level Native Plugin Interface

    https://docs.unity3d.com/Manual/NativePluginInterface.html 拿unity底层graphics device

  4. 【Java关键字-Interface】为什么Interface中的变量只能是 public static final

    三个关键字在接口中的存在原因:public:接口可以被其他接口继承,也可以被类实现,类与接口.接口与接口可能会形成多层级关系,采用public可以满足变量的访问范围: static:如果变量不是sta ...

  5. 【Unity】2.7 检视器(Inspector)

    分类:Unity.C#.VS2015 创建日期:2016-03-31 一.简介 Unity中的游戏是以包含网格.脚本.声音或灯光 (Lights) 等其他图形元素的多个游戏对象 (GameObject ...

  6. Unity 控制public/private 是否暴露给Inspector面板

    默认情况下Public是暴露给Unity,protect/private是不暴露给Unity的,但有时候想让外部引用,又不想暴露给Unity,怎么办? 对Unity隐藏,使用[HideInInspec ...

  7. java实际项目中interface和abstract interface 区别

    参考:https://zhidao.baidu.com/question/424485344260391052.html 这2种有什么区别,根据实际项目经验 帮我解答下 谢谢啊~~~~~~~~~问题补 ...

  8. Unity引擎GUI之Input Field

    InputField 文本输入组件,本文练习InputField的属性及事件 一.属性 1 Interactable: 是否禁用 Transition:过渡方式 Normal Color 正常的未有任 ...

  9. [转]Extending the User Interface in Outlook 2010

    本文转自:https://msdn.microsoft.com/en-us/library/office/ee692172%28v=office.14%29.aspx#OfficeOLExtendin ...

随机推荐

  1. MyEclipse下JDBC-MySQL配置总结

    原创文章,转载请注明:MyEclipse下JDBC-MySQL配置总结  By Lucio.Yang 新手,初期配置未成功,后将网上的方法几乎全部尝试才弄好,下面的方法全而不简练,希望高手指正. 1. ...

  2. AutoCAD 2014简体中文版官方正式版x86 x64下载,带注册机,永久免费使用

    注册机使用说明:会有部分杀毒软件报病毒,请无视.操作步骤:1.安装Autodesk AutoCAD 20142.使用以下序列号666-69696969安装.3.产品密码为001F14.安装完成后,启动 ...

  3. Qt布局与分割器QSplitter

    Qt的布局方式主要有四种:   QGridLayout         栅格布局 QFormLayout       表格布局 QHBoxLayout       水平布局 QVBoxLayout   ...

  4. QT动画介绍(有例子可以下载)

    所谓动画就是在一个时间段内的不同时间点有不同的状态,只要定义好这样状态,实现动画就是水到渠成的事情.当然做这件事情,最好用的就是状态机,点击这里查看Qt使用状态机实现动画效果实例. 不过,实现动画也有 ...

  5. 在Windows环境下使用MinGW编译Qt 4.8.6

    1.修改环境变量工具推荐:Rapid Environment Editor.官网:http://www.rapidee.com/ 修改前请先备份当前的环境变量.然后: (1)检查系统变量path,删除 ...

  6. nginx 配置日志

    http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $r ...

  7. C# 计算器 运算符和数字键的keys对照

    keys. private void Computer_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.NumPad0) ...

  8. java学习之总结

    学的时候忘记写博客,现在java SE学完了一口气把写的代码发了上来没有做什么补充,其中有很多知识漏掉了,学的有点不扎实,接下来写写项目来稳好基础

  9. Shiro入门(1)

    =============基本概念=================== 什么是Apache Shiro? Apache Shiro(发音为“shee-roh”,日语“堡垒(Castle)”的意思)是 ...

  10. iOS开发中NSDate时间戳的转换--

    NSTimeInterval time =(NSTimeInterval )[model.day floatValue]; NSDate *date = [NSDate dateWithTimeInt ...