当前位置:   article > 正文

继承UGUI组件类,自定义属性显示_unity继承的ugui组件怎么显示新增属性

unity继承的ugui组件怎么显示新增属性

项目用到了UGUI,但是Image,与Text组件不能直接控制Alpha,所以,自己继承原组件类搞了下

直接上代码与效果图

Texts类,主要实现Alpha控制,继承了Text

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. [AddComponentMenu("Text", 10)]
  4. public class Texts : Text
  5. {
  6. [HideInInspector]
  7. public float Alpha = 1;
  8. Color c;
  9. public float alpha
  10. {
  11. get
  12. {
  13. c = GetComponent<Texts>().color;
  14. float a = c.a;
  15. return a;
  16. }
  17. set
  18. {
  19. Alpha = value;
  20. c = GetComponent<Texts>().color;
  21. GetComponent<Texts>().color = new Color(c.r, c.g, c.b, Alpha);
  22. }
  23. }
  24. }

TextEditorLis类,继承TextEditor编辑类,主要用于面板显示自定义Alpha属性

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace UnityEditor.UI
  7. {
  8. [CustomEditor(typeof(Texts), true)]
  9. [CanEditMultipleObjects]
  10. public class TextEditorLis : UnityEditor.UI.TextEditor
  11. {
  12. Texts mText;
  13. protected override void OnEnable()
  14. {
  15. base.OnEnable();
  16. }
  17. public override void OnInspectorGUI()
  18. {
  19. base.OnInspectorGUI();
  20. EditorGUILayout.Space();
  21. serializedObject.Update();
  22. mText = (Texts)target;
  23. mText.alpha = EditorGUILayout.Slider("Alpha", mText.alpha, 0, 1);
  24. serializedObject.ApplyModifiedProperties();
  25. if (GUI.changed) {
  26. EditorUtility.SetDirty(target);
  27. }
  28. }
  29. }
  30. }

效果图

最后一步:右键创建时,直接创建为自己的Texts

  1. //---------------扩展UGUI右键Text/Image
  2. [MenuItem("GameObject/UI/Text")]
  3. static void TextLabel()
  4. {
  5. GameObject canvas = CreatCanvas();
  6. //-------创建TextLabel
  7. GameObject go = new GameObject();
  8. go.name = "Text";
  9. Texts t = go.AddComponent(typeof(Texts)) as Texts;
  10. if (GetIsGameObj() != null) {
  11. go.transform.SetParent(GetIsGameObj().transform);
  12. }
  13. else {
  14. go.transform.SetParent(canvas.transform);
  15. }
  16. t.text = "New TextLabel";
  17. t.GetComponent<RectTransform>().localPosition = Vector3.zero;
  18. t.transform.localScale = Vector3.one;
  19. Selection.activeObject = go;
  20. }
这个右键,应该还有别的更好的方式,不过这个也能凑合用
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号