扩展统一UI组件定制督察 [英] Extending Unity UI components with custom Inspector

查看:183
本文介绍了扩展统一UI组件定制督察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在新的统一UI组件延长例如像变换分量?因为没有任何反应,当我尝试,而不是使用UnityEditor变换组件

 延长按钮; 
使用UnityEngine;
[CustomEditor(typeof运算(变换))]
公共类CustomTransform:编辑
{
公共覆盖无效OnInspectorGUI()
{
}
}


解决方案

是的,你可以扩展UI组件并把它们写自己定制的检查。
你只需要记住使用权的命名空间,也从右边督察类继承。



您当然可以重写呢!



例子这里是一个UISegmentedControlButton

 使用UnityEngine; 
使用UnityEngine.UI;使用UnityEngine.EventSystems
; System.Collections中使用
;

公共类UISegmentedControlButton:按钮{

公共雪碧offSprite;
公共雪碧onSprite;
众彩offTextColor = Color.white;
众彩onTextColor = Color.white;

私人布尔isSelected;
公共BOOL IsSelected {
获得{
返回isSelected;
}
集合{
isSelected =价值;

文本文本= this.transform.GetComponentInChildren<文本>();

如果(价值){
this.GetComponent<图像方式>()=精灵onSprite;
text.color = onTextColor;
}其他{
this.GetComponent<图像方式>()=精灵offSprite;
text.color = offTextColor;
}
}
}




公共覆盖无效OnPointerClick(PointerEventData EVENTDATA){

this.transform.parent.GetComponent< UISegmentedControl方式>()SelectSegment(本);
base.OnPointerClick(EVENTDATA);
}

}

和编辑类的:

 使用UnityEngine; 
使用UnityEditor;
使用UnityEngine.UI; System.Collections中使用
;

[CustomEditor(typeof运算(UISegmentedControlButton))]
公共类UISegmentedControlButtonEditor:UnityEditor.UI.ButtonEditor {


公共覆盖无效OnInspectorGUI(){

UISegmentedControlButton成分=(UISegmentedControlButton)的目标;

base.OnInspectorGUI();

component.onSprite =(雪碧)EditorGUILayout.ObjectField(关于雪碧,component.onSprite的typeof(雪碧),TRUE);
component.onTextColor = EditorGUILayout.ColorField(文本颜色,component.onTextColor);
component.offSprite =(雪碧)EditorGUILayout.ObjectField(关雪碧,component.offSprite的typeof(雪碧),TRUE);
component.offTextColor = EditorGUILayout.ColorField(关文本颜色,component.offTextColor);

}
}



另外这里是直接有用的链接到Unity用户界面的源



https://bitbucket.org /单位技术/ UI / src目录



和一点点的证明,它的作品:




Is it possible to extend the new unity ui components like for example the transform component? Because nothing happens when i try to extend the button, instead of the transform component

using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Transform))]
public class CustomTransform : Editor
{
    public override void OnInspectorGUI()
    {            
    }
}

解决方案

Yes you can extend UI components and write them their own custom inspector. You just need to remember to use right namespaces and also inherit from the right Inspector class.

You can of course override too!.

Example here is a UISegmentedControlButton

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class UISegmentedControlButton : Button {

public Sprite offSprite;
public Sprite onSprite;
public Color offTextColor = Color.white;
public Color onTextColor = Color.white;

private bool isSelected;
public bool IsSelected {
    get {
        return isSelected;
    }
    set {
        isSelected = value;

        Text text = this.transform.GetComponentInChildren<Text>();

        if (value) {
            this.GetComponent<Image>().sprite = onSprite;
            text.color = onTextColor;
        } else {
            this.GetComponent<Image>().sprite = offSprite;
            text.color = offTextColor;
        }
    }
}




public override void OnPointerClick(PointerEventData eventData) {

    this.transform.parent.GetComponent<UISegmentedControl>().SelectSegment(this);
    base.OnPointerClick(eventData);
}

}

And Editor class for it:

using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System.Collections;

[CustomEditor(typeof(UISegmentedControlButton))]
public class UISegmentedControlButtonEditor : UnityEditor.UI.ButtonEditor {


public override void OnInspectorGUI() {

    UISegmentedControlButton component = (UISegmentedControlButton)target;

    base.OnInspectorGUI();

    component.onSprite = (Sprite)EditorGUILayout.ObjectField("On Sprite", component.onSprite, typeof(Sprite), true);
    component.onTextColor = EditorGUILayout.ColorField("On text colour", component.onTextColor);
    component.offSprite = (Sprite)EditorGUILayout.ObjectField("Off Sprite", component.offSprite, typeof(Sprite), true);
    component.offTextColor = EditorGUILayout.ColorField("Off text colour", component.offTextColor);

}
}

Also here is a useful link directly to the source of Unity UI

https://bitbucket.org/Unity-Technologies/ui/src

And a little proof that it works:

这篇关于扩展统一UI组件定制督察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆