赞
踩
项目用到了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;
- }
这个右键,应该还有别的更好的方式,不过这个也能凑合用
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。