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
Monobehaviourautomatically
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
PropertyDrawerclass
you can extend to control how a type is displayed in the inspector. ThePropertyDrawerfor
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 ...
随机推荐
- win7如何快速设置开机启动项?
添加开机启动项方法: 找到windows开始菜单->所有程序->启动,右键打开, 进入C:\Users\Ocean\AppData\Roaming\Microsoft\Windows\St ...
- 一个分组查询 每组前 10 的sql 语句
USE tmpgo CREATE TABLE Employee( ID int identity(1,1), EmpName varchar(20), EmpSalary varchar(10), E ...
- HTML5 总结-SVG-5
HTML5 内联 SVG HTML5 支持内联 SVG. 什么是SVG? SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG 用于定义用于网络的基于矢量的图形 SV ...
- 【转载】Google Analytics 使用图文全攻略
转载自:Google Analytics 使用图文全攻略 最近一段时间,因为工作的需要,小励使用GA(GA是Google Analytics的简称)比较频繁,所以花时间研究了一下,从不太了解到会使用( ...
- IE6不支持<a>标签以外元素的hover的解决方案
IE6以及更低版本的浏览器对“:hover”的支持不理想,对于类似的“p:hover”.“img:hover”.“#header:hover”...,今天给大家介绍一种新的方法,可以完美解决IE6不支 ...
- KbmMW资源汇总(更新中…)
KbmMW框架是收费的,不在此提供下载,如需购买,请自行联系作者Kim Madsen. 网址资源: 官网主页:http://www.components4programmers.com/product ...
- AseoZdpAseo.init(this, AseoZdpAseo.INSERT_TYPE);
让以后的人知道吧,这就是一个广告包,相当于广告插件.
- [置顶] PHP开发实战权威指南-读书总结
从今年开始,断断续续学习PHP已经有4个月了. 最初,认真学习PHP几天,就弄WordPress搭建了一个个人博客,这也符合技术人的实践理念. 最近,重温PHP开发实战权威指南,做点总结,整理下自己学 ...
- Java面试题之三
十一.谈谈final,finally,finalize的区别? 1.final:是修饰符,是一个关键字.修饰变量,如果是基本类型表示该变量的值不能修改:如果是引用类型表示该变量不能指向别的对象:修饰类 ...
- 手把手教你在Windows端搭建Redmine项目管理软件
1.Redmine介绍 Redmine是用Ruby开发的基于web的项目管理软件,是用ROR框架开发的一套跨平台项目管理系统,据说是源于Basecamp的ror版而来,支持多种数据库,有不少自己独特的 ...