Unity Interface Serialization-Expose Interface field In Inspector
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. ThePropertyDrawer
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的更多相关文章
- 【Go入门教程6】interface(interface类型、interface值、空interface{}、嵌入interface、反射)
interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...
- 【Go入门教程8】interface(interface类型、interface值、空interface{}、嵌入interface、反射)
interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...
- Unity Low-level Native Plugin Interface
https://docs.unity3d.com/Manual/NativePluginInterface.html 拿unity底层graphics device
- 【Java关键字-Interface】为什么Interface中的变量只能是 public static final
三个关键字在接口中的存在原因:public:接口可以被其他接口继承,也可以被类实现,类与接口.接口与接口可能会形成多层级关系,采用public可以满足变量的访问范围: static:如果变量不是sta ...
- 【Unity】2.7 检视器(Inspector)
分类:Unity.C#.VS2015 创建日期:2016-03-31 一.简介 Unity中的游戏是以包含网格.脚本.声音或灯光 (Lights) 等其他图形元素的多个游戏对象 (GameObject ...
- Unity 控制public/private 是否暴露给Inspector面板
默认情况下Public是暴露给Unity,protect/private是不暴露给Unity的,但有时候想让外部引用,又不想暴露给Unity,怎么办? 对Unity隐藏,使用[HideInInspec ...
- java实际项目中interface和abstract interface 区别
参考:https://zhidao.baidu.com/question/424485344260391052.html 这2种有什么区别,根据实际项目经验 帮我解答下 谢谢啊~~~~~~~~~问题补 ...
- Unity引擎GUI之Input Field
InputField 文本输入组件,本文练习InputField的属性及事件 一.属性 1 Interactable: 是否禁用 Transition:过渡方式 Normal Color 正常的未有任 ...
- [转]Extending the User Interface in Outlook 2010
本文转自:https://msdn.microsoft.com/en-us/library/office/ee692172%28v=office.14%29.aspx#OfficeOLExtendin ...
随机推荐
- 「Foundation」结构体
一.基本知识 Foundation—基础框架.框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础. 如果要想使用foundation框架中的数据类型,那么包含它的主头文 ...
- 在一个数组中是否存在两个数A、B的和为M
#include <iostream>#include <algorithm>//#include <vector>using namespace std; int ...
- 条款21: 必须返回对象时,不要强行返回对象的reference
总结: 绝不要返回一个local栈对象的指针或引用:绝不要返回一个被分配的堆对象的引用:绝不要返回一个静态局部对象(为了它,有可能同时需要多个这样的对象的指针或引用). 条款4中给出了“在单线程环境中 ...
- JS拖动DIV布局
方法一: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- 【c语言】求最大值
一.我个人觉得求最大值比较简单的一种方法(当然同时求最大值和最小值时稍微改改也能行) #include <stdio.h> int main(void) { int f, i, max; ...
- 蝕刻技術(Etching Technology)
1. 前言 蚀刻是将材料使用化学反应或物理撞击作用而移除的技术. 蚀刻技术可以分为『湿蚀刻』(wet etching)及『干蚀刻』(dry etching)两类.在湿蚀刻中是使用化学溶液,经由化学反应 ...
- Poj 2255 Tree Recovery(二叉搜索树)
题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...
- 聊聊高并发(二十五)解析java.util.concurrent各个组件(七) 理解Semaphore
前几篇分析了一下AQS的原理和实现.这篇拿Semaphore信号量做样例看看AQS实际是怎样使用的. Semaphore表示了一种能够同一时候有多个线程进入临界区的同步器,它维护了一个状态表示可用的票 ...
- ZOJ3640-Help Me Escape
Help Me Escape Time Limit: 2 Seconds Memory Limit: 32768 KB Background If thou doest well, ...
- Automatic Preferred Max Layout Width is not available on iOS versions prior to 8.0
今天在真机调试低版本系统的时候出现如题类似Layout Max Width在ios 8 之前不适用的问题, 初步估计是autolayout 所导致的 查找资料解决方法如下: 将label下Preffe ...