制作自己的UILabel控件

作者:追风剑情 发布于:2014-5-15 20:30 分类:Unity3d

一、新建两个脚本

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [ExecuteInEditMode]
  5. public class UILabel : MonoBehaviour {
  6.  
  7. [HideInInspector]
  8. [SerializeField]
  9. public string text;
  10.  
  11. void OnGUI()
  12. {
  13. GUI.Label(new Rect(0, 0, 200, 200), text);
  14. }
  15. }

 

 

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. [CustomEditor(typeof(UILabel))]
  6. [CanEditMultipleObjects]
  7. public class UILabelInspector : Editor
  8. {
  9. protected UILabel mLabel;
  10.  
  11. protected virtual void OnEnable()
  12. {
  13. mLabel = target as UILabel;
  14. }
  15.  
  16. public override void OnInspectorGUI()
  17. {
  18. string text = EditorGUILayout.TextArea(mLabel.text, GUI.skin.textArea, GUILayout.Height(100f));
  19. if (!text.Equals(mLabel.text)) { mLabel.text = text; }
  20. }
  21. }

 

1.把UILabel脚本挂在一个GameObject上

2.UILabelInspector脚本必须放在Editor目录下

 

工程结构

制作UILabel工程结构.png

 

编辑效果

制作UILabel运行效果.png

 

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号