继承UGUI组件类,自定义属性显示

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

直接上代码与效果图

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

using UnityEngine;
using UnityEngine.UI;

[AddComponentMenu("Text", 10)]
public class Texts : Text
{
    [HideInInspector]
    public float Alpha = 1;
    Color c;
    public float alpha
    {

        get
        {
            c = GetComponent<Texts>().color;
            float a = c.a;
            return a;
        }
        set
        {
            Alpha = value;
            c = GetComponent<Texts>().color;
            GetComponent<Texts>().color = new Color(c.r, c.g, c.b, Alpha);
        }
    }
}

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

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

namespace UnityEditor.UI
{

    [CustomEditor(typeof(Texts), true)]
    [CanEditMultipleObjects]
    public class TextEditorLis : UnityEditor.UI.TextEditor
    {
        Texts mText;
        protected override void OnEnable()
        {
            base.OnEnable();
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            EditorGUILayout.Space();
            serializedObject.Update();
            mText = (Texts)target;
            mText.alpha = EditorGUILayout.Slider("Alpha", mText.alpha, 0, 1);
            serializedObject.ApplyModifiedProperties();
            if (GUI.changed) {
                EditorUtility.SetDirty(target);
            }
        }
    }
}

效果图

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

   //---------------扩展UGUI右键Text/Image
    [MenuItem("GameObject/UI/Text")]
    static void TextLabel()
    {
        GameObject canvas = CreatCanvas();

        //-------创建TextLabel
        GameObject go = new GameObject();
        go.name = "Text";
        Texts t = go.AddComponent(typeof(Texts)) as Texts;
        if (GetIsGameObj() != null) {
            go.transform.SetParent(GetIsGameObj().transform);
        }
        else {
            go.transform.SetParent(canvas.transform);
        }
        t.text = "New TextLabel";
        t.GetComponent<RectTransform>().localPosition = Vector3.zero;
        t.transform.localScale = Vector3.one;
        Selection.activeObject = go;
    }
这个右键,应该还有别的更好的方式,不过这个也能凑合用
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值