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. PHP 导入excel

    db.php为数据库操作类, $config为数据库配置,PHPExcel版本为PHPExcel_1.8.0,  PHP代码: header("Content-type:text/html; ...

  2. Python网络编程——主机字节序和网络字节序之间的相互转换

    If you ever need to write a low-level network application, it may be necessary to handle the low-lev ...

  3. MacOS + Linux + Nginx

    Asp.Net Core 发布和部署( MacOS + Linux + Nginx ) 前言 在上篇文章中,主要介绍了 Dotnet Core Run 命令,这篇文章主要是讲解如何在Linux中,对 ...

  4. QPushButton跑进度条(使用QSS的不同修饰来实现,其实是伪进度条)

    主要用到qlineargradient,写以下CSS样式即可实现: background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, sto ...

  5. java 抽象类与接口的区别 整理

    抽象类与接口的区别 抽象类 包含抽象方法的类就是抽象类,声明的语句:abstract class 必须是public protected 接口 对行为的抽象,声明语句:interface 抽象方法的修 ...

  6. [WPF疑难]避免窗口最大化时遮盖任务栏

    原文 [WPF疑难]避免窗口最大化时遮盖任务栏 [WPF疑难]避免窗口最大化时遮盖任务栏 周银辉 WPF窗口最大化时有个很不好的现象是:如果窗口的WindowStyle被直接或间接地设置为None后( ...

  7. Gartner 认可 Microsoft 为应用程序平台即服务的领导者

    对于 Windows Azure 而言,2013 年是了不起的一年.客户使用量每月都创新高:4 月份 Windows Azure 基础结构服务一经正式发布即受到前所未有的青睐,成为重要的里程碑.Gar ...

  8. 在web page中使鼠标右击失效的几种方法

    这里主要介绍两种方法,一种是使用js来处理,还有一种是在html属性中设置. 方法一:js 1: <script language="javascript"> docu ...

  9. 使用CAShapeLayer来实现圆形图片加载动画[译]

    原文链接 : How To Implement A Circular Image Loader Animation with CAShapeLayer 原文作者 : Rounak Jain 译文出自 ...

  10. JavaScript 高级程序设计(第3版)笔记——chapter3:基本概念(函数部分)

    3.7函数 3.7.1 理解参数 ECMAScript 函数不介意传递进来多个参数,也不在乎传递进来的参数是什么数据类型.因为在 ECMAScript 中的参数在内部是用一个数组来表示的.在函数体内可 ...