Unity Interface Serialization-Expose Interface field In Inspector
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Unity Interface Serialization-Expose Interface field In Inspector,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3144字,纯文字阅读大概需要5分钟。
内容图文
![Unity Interface Serialization-Expose Interface field In Inspector](/upload/InfoBanner/zyjiaocheng/1072/7572ee0c6dbe45ad9bf030447335a89e.jpg)
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.
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:
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
原文:http://www.cnblogs.com/bhlsheji/p/5212658.html
内容总结
以上是互联网集市为您收集整理的Unity Interface Serialization-Expose Interface field In Inspector全部内容,希望文章能够帮你解决Unity Interface Serialization-Expose Interface field In Inspector所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。