位置: 编程技术 - 正文

Custom List in inspector, displaying data your way[Unity]

编辑:rootadmin

推荐整理分享Custom List in inspector, displaying data your way[Unity],希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

原文地址: this Unity C# tutorial you will create a custom visualization for arrays and lists in the editor.You will learn tocreate a custom editoruse SerializedObjectmanipulate a SerializedProperty that represents an array or listuse an enumeration for option flagsuse GUI buttons

This tutorial comes after the Custom Data tutorial.

This tutorial is for Unity version 4.3 and above. The older version can still be found here.

inShare1Customized lists.Creating Test DataUnity's default way to show lists is serviceable, but it's possible to find yourself wanting for an alternative. The specifics can vary from case to case. So it would be useful if we could use a mix of different visualizations. It is possible to do this by adding attributes to variables that specify how the editor should show them.

We start with the finished Custom Data tutorial project, or by creating a new empty project and importing custom-data.unitypackage.

Then we create a new test script named ListTester with some test arrays, and make a new prefab and prefab instance with it, so we can see it all works as expected.

New test object, with wide inspector.Creating a Custom InspectorOur first step to customizing our lists is to create a custom inspector for our test component. Create a new C# script named ListTesterInspector in the Editor folder, make it extendUnityEditor.Editor, and apply the UnityEditor.CustomEditor attribute to tell Unity that we want it to do the drawing for our component.Custom inspector script.To actually change the inspector we need to override the OnInspectorGUI method of the Editorclass. Leaving the method empty will result in an empty inspector as well.Empty inspector.There are three important differences between a property drawer and an editor. Firstly, in the editor we work with an entire SerializedObject instead of a single SerializedProperty. Secondly, an instance of the editor exists as long as the object stays selected, keeping a reference to its data instead of getting it via a method parameter. Finally, we can useEditorGUILayout, which takes care of positioning for us.Custom List in inspector, displaying data your way[Unity]

We can get to the serialized object via the serializedObject property. To prepare it for editing, we must first synchronize it with the component it represents, by calling its Update method. Then we can show the properties. And after we are done, we have to commit any changes via itsApplyModifiedProperties method. This also takes care of Unity's undo history. In between these two is where we'll draw our properties.

Inspector with empty properties.The fields are visible again, but they're empty. This is because PropertyField doesn't show any children – like array elements – unless we tell it to do so.Inspector with children.Creating an Editor ListTo use a customized list in our inspector, we'll create an alternative for the PropertyFieldmethod. We will name this method Show and put it in its own static utility class, so we can use it wherever we want. We'll name this class EditorList and place it in the Editor folder.EditorList script.This method doesn't do anything yet, but we can already use it in our custom editor, resulting once again in an empty inspector.Showing a list consists of three parts, its foldout, its size, and its elements. We can show the foldout by using EditorGUILayout.PropertyField without having it show the children of the list. Then we can show the list elements ourselves with help of the arraySize property and theGetArrayElementAtIndex method of SerializedProperty. We'll leave the size for later.Lists without indented elements.Properly IndentingWe have our lists again, but their elements aren't indented. We can solve this by increasing the indent level before showing the elements, and decreasing it again afterwards.Messed up indenting.Now indentation works again, except for our color point list, which goes wrong after the first element. This is because we set the indent level to zero in our custom property drawer. There are two ways to fix this. Either our custom list should set the indent level to the correct value again after each element, or the property drawer should make sure it leaves the indent level unchanged. Let's make sure that ColorPointDrawer behaves well.Correct indenting, but no collapsing.Collapsing ListsNow that indenting works, we can take care of the next problem. Toggling the foldouts should collapse and expand the lists, but it currently doesn't work. This is easy to fix by checking theisExpanded property of our list.Correctly collapsing.Showing the SizeTo show the list's size, we can use the special relative property named Array.size. We'll simply show it in between the foldout and the elements.Why not use arraySize here?Complete lists.Customizing the ListNow that we have replicated the default list, it's time to add customizations. An easy start is to make the size that we just added optional. We add a boolean parameter to control this and turn it on by default.How does showListSize work?Using this new option, we can switch of the size for some of our lists.Hiding some of the list sizes.As a second option, let's make the list's label optional as well. If we don't show the label, we won't indent either and we will always show the elements, regardless whether the list is expanded.Now we can remove the label from some of our lists.Hiding some of the list labels.Using FlagsWhile we can keep adding options this way, our method calls will become increasingly obscure. We could add wrapper methods with more descriptive names, but that would bloat our script and isn't flexible. An alternative is the use of option flags.

The first thing we need to do is create an enumeration of all our options. We name itEditorListOption and give it the System.Flags attribute. We place it in its own script file or in the same script as EditorList, but outside of the class.

Is the Flags attribute required?Now we add entries for the two options that we already have. We also add an option for when we want nothing and for when we want the default. The default is to show both the list's label and its size. We specify this by combining both options with the bitwise OR operator |.How do bitwise flags work?The boolean parameters of the Show method can now be replaced with a single options parameter. Then we'll extract the individual options with the help of the bitwise AND operator &and store them in local variables to keep things clear.Then we can modify our custom inspector to use the new options approach.Hiding the Element LabelsAnother useful feature is the ability to hide the labels of the elements. So let's add an option for element labels and include it in the default. We can also add a convenient entry for the default without the element labels.Now all we have to do in our Show method is extract this option and perform a simple check. Let's also move the element loop to its own private method, for clarity. Hiding some of the element labels, wide and narrow.Our element labels can now be hidden, but something is wrong when we use a narrower inspector. It turns out that our color point drawer still decides to use an additional line, but without a label this is no longer useful. The solution is simply to make sure ColorPointDrawer does not claim an extra line when it does not receive a label.No longer needlessly claiming extra lines.Adding ButtonsInstead of removing parts, we can also add thing to our list. We could add a set of buttons for manipulating list elements. They can provide an alternative way to delete and duplicate elements, and we can add a means to reorder the elements as well.

First we'll add an option for buttons, and also a convenient option to activate everything.

For the button labels we'll use a simple "&#;" for duplicate, a "-" for delete, and a "↴" (rightwards arrow with corner downwards) for move. You can directly insert the arrow unicode character, but I use its escape code just to be sure everyone can copy it correctly.

We predefine static GUIContent for these buttons and include handy tooltips as well. We also add a separate method for showing the buttons and call it after each element, if desired.

Why static instead of const?Let's add these buttons to the lists for our color points and objects.Quite huge buttons.These buttons are way too large, because each claims an entire line. We want everything to stay together on a single line instead. We can instruct the automatic layout to do this by putting our content in between calls to EditorGUILayout.BeginHorizontal andEditorGUILayout.EndHorizontal.Pretty large buttons.That is a lot better, but the buttons are still too large. We'll change two things to make them behave. First, we'll apply some mini button styles to them. Second, we'll give them a fixed width of pixels each.Mini buttons.Now the buttons look fine, but they don't do anything yet.

Fortunately, adding functionality to the buttons is very simple, as we can directly use the methods for array manipulation provided by SerializedProperty. We need the list and the current element index for this to work, so we add them as parameters to our ShowButtons method and pass them along inside the loop of ShowElements.

How does Button work?What happens when we move the bottom element?What are the contents of a new item?Our buttons now work as expected. Or do they? Try deleting an element from the objects list. If it references no component, it works. But if it does reference something, it will clear the reference, but not remove the element from the list.

While this is how Unity handles deletion in this case, it is weird. Instead, we want the element to always be removed, not sometimes cleared. We can enforce this by checking whether the list's size has remained the same after deleting the element. If so, it has only been cleared and we should delete it again, for real this time.

An additional button we could include is one to add an element when the list is empty. This is useful when we're not showing the list size, because in that case you could neither duplicate an element nor manipulate the list size directly. So let's add such a button.A big add button.Only Allowing ListsWhat would happen if we would try to use our editor list with something that isn't a list at all? Let's find out by adding something to ListTester that is not a list.Now also try to show it in ListTestInspector.Not a list shown.It actually works for our integer. But it's not guaranteed to work for any arbitrary type and it will also result in errors for some options. It is a better idea to flatly refuse to show anything that isn't a list. So let's check whether the property is an array. If it isn't, we display a nicely formatted warning and nothing else.Only lists allowed.Multi-object EditingAnother interesting point is multi-object editing. You can test this by duplicating our list tester object, make them a little different, and selecting both at the same time.No multi-object editing yet.By default custom editors do not support multi-object editing, though that is easily fixed by adding the CanEditMultipleObjects attribute to our ListTesterInspector.Multi-object editing.While this can be useful, it gets weird when you're editing multiple objects that have lists of different sizes. In general it is not very useful and might even be drawn wrong. So let's not show the list's elements when we have multiple different sizes.Divergent lists will not be shown.We now have a generic list drawer with four customization options that you can use instead of the default list representation. Quite handy!

Unity NGUI添加事件监听(转摘) usingUnityEngine;usingSystem.Collections;usingUnityEngine.UI;usingUnityEngine.EventSystems;usingUnityEngine.Events;publicclassUIMain:MonoBehaviour{Buttonbutton;Imageimage;voidStart(){button=transform.

Unity光照烘焙后,在PC上曝光,发布移动平台正常的解决方法 Unity在Windows下默认使用的是DX,使用OpenGL就能解决过曝的问题,添加Unity的启动参数force-opengl,例如下面:在unity.exe的快捷方式中写上exe路径-force-opengl安

CustomPropertyDrawer-Change the height of Property field Thelastblogpostdidn'tcoveracertainniftymethodinPropertyDrawer,whichistheGetPropertyHeight.GetPropertyHeight:Determinestheheightinpixelofthepropertyfield.OverridingGetPropertyHeightthereforeallowsyouto

标签: Custom List in inspector, displaying data your way[Unity]

本文链接地址:https://www.jiuchutong.com/biancheng/377322.html 转载请保留说明!

上一篇:numix-cicle圆形图标(圆形图表分析怎么做)

下一篇:Unity NGUI添加事件监听(转摘)

  • 农民收购农产品发票
  • 经营范围变更银行不变更有影响吗
  • 发票上的不含税单价怎么算的
  • 取消待摊费用科目
  • 出口货物退货需要退税吗
  • 小企业会计准则调整以前年度费用分录
  • 临时人员劳务费有哪些?
  • 融资租赁租入固定资产折旧可以税前扣除吗
  • 应计入资产负债表应付利息项目的有
  • 去年的成本没有入账
  • 上年度的印花税怎么计算
  • 劳动合同的履行包括什么原则
  • 旅行社代订机票怎么做账
  • 专票住宿费认证怎么操作
  • 提示涉税风险该怎么弄
  • 酒店工作车工作间标准
  • 广告传媒公司的名字
  • 关于个人所得税下列说法正确的是
  • 开发票回款是什么意思
  • 利润表中利润归还投资
  • 跨年度的房租发票怎么做账
  • 股权转让受让方要交个人所得税吗
  • linux连接windows的服务redis
  • 行政划拨无偿取得的土地使用权属于什么资产
  • 工程竣工结算资料存档几年
  • 增值税不允许抵扣计入什么科目
  • php7安装教程详解
  • 银行承兑汇票背书什么意思
  • php三大特性
  • js如何实现异步编程
  • vue实现返回顶部
  • 流动资产金额
  • 销售退换货的账务处理
  • python photoshop
  • 会计调整以前年度遗留问题查不出来说明怎么写
  • 红字发票信息表怎么打印出来
  • 当月已付款, 没收到发票怎么做账
  • 使用时间
  • 人工智能自动对焦又叫什么
  • 年底计提坏账收回一部分怎么处理
  • 递延所得税资产和负债账务处理
  • 对公账户收到钱都要开票吗
  • 经营活动的现金流量占全部现金流量的比重越大
  • 中小企业所得税优惠
  • 拍卖行业收取手续费多少
  • 注册资本没有全额投入,公司银行贷款贷款利息怎么入账
  • 附加税多交了怎么抵税
  • 增值税专用发票有几联?
  • 项目评审结果
  • 工会经费购买的固定资产处置
  • 支付上月采购货款
  • 抵账的车买了什么后果
  • 购买的商品属于什么会计科目
  • 固定资产净额是什么意思
  • c# mysql实例
  • mysql8.0远程连接
  • Gene6 FTP在windows 2008上面破解后无法启动解决方法
  • freebsd操作命令
  • vsftpd 550错误
  • xp系统内存不能为read的解决方法
  • hpp是什么文件
  • win8怎么安装win10
  • win7 64位纯净版图标变成了一样该怎么办?win7旗舰版图标变成一样的解决方法
  • windows7中彻底删除文件的操作
  • 内存一次性读多少字节
  • unity寻路系统
  • shell脚本实例精讲
  • 支持向量机
  • php操作dom
  • unity如何成一组
  • document对象常用方法
  • 教你怎样用气球做可爱小兔子气球君带你做气球手工
  • 在jquery中使用什么方法获取和设置属性
  • java语言基于对象
  • jquery的实现原理
  • Listview的onItemClickListener无法响应的解决方法
  • 广州酒家月饼抽奖公告最新
  • 福建省农业厅副厅长梁
  • 取得土地使用权后两年未开发
  • 河南地税申报表怎么填
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设