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 ...
随机推荐
- wdcp centos6.5多网站部署
一.wdcp介绍 wdCP是WDlinux Control Panel的简称,是一套通过WEB控制和管理服务器的Linux服务器管理系统以及虚拟主机管理系统,旨在易于使用Linux系统做为我们的网站服 ...
- php 自己写的好看的分页类
自己写的一个分页类 ,不是很完整,个别没有做验证,但可以使用,分页效果见文章底部.除了链接数据库的代码没有粘贴上来,其他的都粘贴了.供学习使用- <?php /** * Created by P ...
- poj 1715 Hexadecimal Numbers 排列组合
/** 大意: 给定16进制数的16个字母,,求第k大的数,,要求数的长度最大为8.,并且每个数互不相同. 思路: 从高到低挨个枚举,每一位能组成的排列数 ,拿最高位来说,能做成的排列数为15*A(1 ...
- XML 反序列化为Model
什么也不多说,直接贴代码 需要反序列的XML <?xml version='1.0' encoding='utf-8' ?> <GetCitiesListResponse> & ...
- 亲测VS2010纯静态编译QT4.8.0,实现VS2010编译调试Qt程序,QtCreator静态发布程序(图文并茂,非常详细)
下载源代码,注意一定是源码压缩包如qt-everywhere-opensource-src-4.8.0.zip,不是Qt发布的已编译的不同版本的标准库如qt-win-opensource-4.8.0- ...
- perl学习(1) 入门
Perl 被设计成90%擅长处理文本,10%处理其余的问题.因此Perl 有强大的文本处理能力,包括正则表达式. 第一个程序 hello world #! /usr/bin/perl -w use s ...
- Hadoop Hive sql语法详解
Hadoop Hive sql语法详解 Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构 化的数据文件 ...
- prim模板
]; int n; ][]; ]; int prim(){ int i,j,mi,v; ;i<n;i++){ d[i]=map[][i]; vis[i]=; } ;i<=n;i++){ m ...
- 如何用 Swift 语言构建一个自定控件
(via:破船之家,原文:How To Make a Custom Control in Swift) 用户界面控件是所有应用程序重要的组成部分之一.它们以图形组件的方式呈现给用户,用户可以通过它 ...
- TCP/IP之三次握手、四次挥手
参照:http://www.cnblogs.com/hnrainll/archive/2011/10/14/2212415.html 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建 ...