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. 【转】从底层了解ASP.NET体系结构

    从底层了解ASP.NET体系结构 原文:http://blog.csdn.net/zhoufoxcn/article/details/1890158 Java体系架构的书多如牛毛,比如SSH架构什么的 ...

  2. sql差异

    类别 MS SQL Server My SQL PG SQL Oracle Access  自增  identity(1,1) auto_increment ALTER TABLE 'tableNam ...

  3. Qt Installer Framework的学习

    Qt Installer Framework是Qt默认包的发布框架.它很方便,使用静态编译Qt制作而成.并且使用了压缩率很高的7z对组件进行压缩.之所以有这些好处,我才觉得值得花一点儿精力研究一下这个 ...

  4. c++ ptrdiff_t 类型

    ptrdiff_t是C/C++标准库中定义的一个与机器相关的数据类型.ptrdiff_t类型变量通常用来保存两个指针减法操作的结果.ptrdiff_t定义在stddef.h(cstddef)这个文件内 ...

  5. BZOJ 3240([Noi2013]矩阵游戏-费马小定理【矩阵推论】-%*s-快速读入)

    3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec   Memory Limit: 256 MB Submit: 123   Solved: 73 [ Submit][ St ...

  6. Java怎样高速构造JSON字符串

    目标:依据key/value高速构造一个JSON字符串作为參数提交到web REST API服务上. 分别測试里阿里巴巴的FastJson和Google Gson,终于我採用了Google Gson来 ...

  7. B. Friends and Presents(Codeforces Round #275(div2)

    B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input stand ...

  8. java读取远程url图片,得到宽高

    链接地址:http://blog.sina.com.cn/s/blog_407a68fc0100nrb6.html import java.io.IOException;import java.awt ...

  9. Visual Studio 2012中编写C程序

    换了win7系统后,突然发现VC++6.0不兼容了,我听说有的同学的行,反正我是不行. 那就用VS2012呗.... 我们来看看怎么用: 打开文件->新建->项目,新建一个项目 选择win ...

  10. 提高你开发效率的十五个Visual Studio 2010使用技巧

    提高你开发效率的十五个Visual Studio 2010使用技巧 相信做开发的没有不重视效率的.开发C#,VB的都知道,我们很依赖VS,或者说,我们很感谢VS.能够对一个IDE产生依赖,说明这个ID ...