【问题标题】:Extending Unity UI components with custom Inspector使用自定义 Inspector 扩展 Unity UI 组件
【发布时间】:2015-05-17 02:42:21
【问题描述】:

是否可以扩展新的统一 ui 组件,例如转换组件?因为当我尝试扩展按钮而不是转换组件时没有任何反应

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

【问题讨论】:

  • 这不是扩展,它只是一个特定组件的编辑器,它派生自MonoBehaviour
  • 所以只能扩展从 MonoBehavior 派生的组件?
  • 在 C# 中扩展意味着,创建一个从另一个类派生的新类,也就是继承。上面的代码 sn-p 与继承无关,它与编辑器扩展有关。 public class Player : MonoBehavior { } 正在扩展 MonoBehavior 并添加新功能。并且 UI 组件是可以扩展的,你只需要继承它们,而不是继承自 Editor。编辑器用于 Unity Inspector 中的简化 UI
  • 如果您需要重新编译自己版本的 Unity UI(即在某些情况下可能导致继承可能不起作用),您可以从bitbucket.org/Unity-Technologies/ui 下载 Unity UI 源并自行编译。查看该页面上的自述文件,了解如何用新编译的 UI 程序集替换 Unity 标准 UI 程序集
  • 对不起,我再次使用了扩展。我说的是扩展,因为我只想在编辑器中添加一些 GUI 控件,然后使用 DrawDefaultInspector() 所以我不是在谈论继承。编辑:没有阅读您的最后评论。应该在发布之前刷新

标签: c# unity3d unity3d-gui


【解决方案1】:

是的,您可以扩展 UI 组件并为它们编写自己的自定义检查器。 您只需要记住使用正确的命名空间并从正确的 Inspector 类继承。

你当然也可以覆盖!

这里的例子是一个 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);
}

}

以及它的编辑器类:

附: ButtonEditor 与 UnityEditor.UI.ButtonEditor 不同,因为第一个来自 UnityEngine.ButtonEditor。要从 UnityEditor 访问 .UI,您需要将 Editor 脚本放在 Editor 文件夹下

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);

}
}

这里还有一个直接指向 Unity UI 源代码的有用链接

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

还有一点证明它有效:

【讨论】:

  • 我无法从编辑器类中的 UnityEditor.UI.ButtonEditor 扩展。该类型不存在。但是我查看了 bitbucket 上的源代码,并且该类确实存在并且是公开的。我只能从 GraphicEditor 和 TextEditor 扩展
  • 确保您使用的是上述命名空间。工作 100%。我现在在现场项目中使用它
  • 谢谢。现在它起作用了。只是忘记将脚本移动到编辑器文件夹中;)但是有没有办法像使用转换组件一样直接扩展组件,以便我可以使用默认按钮?
  • 太棒了,请在回答中打勾
  • 我会,但我还是想知道是否真的可以直接扩展一个ui组件。
猜你喜欢
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多