NGUI 拓展Poplist 插件

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PopupList : UIWidgetContainer
{
    public class PopValue
    {
        public object value;
        public object frag;
        public PopValue(object value,object frag)
        {
            this.value=value;
            this.frag=frag;
        }
    }


    /// <summary>
    /// List of all the active toggles currently in the scene.
    /// </summary>
    static public List<PopupList> list = new List<PopupList>();

    PopupList currentPopList=null;

    /// <summary>
    /// 拉框点击按钮
    /// </summary>
    GameObject ClickBtn;
    /// <summary>
    /// 选中输入数据
    /// </summary>
   public UILabel InputShowLab;
    /// <summary>
    /// 父物件
    /// </summary>
    GameObject DragList;
    /// <summary>
    /// 克隆元素
    /// </summary>
    GameObject Item1;
    /// <summary>
    /// 下拉背景
    /// </summary>
    GameObject Back;

    /// <summary>
    /// 滚动条
    /// </summary>
    SUIWrapContent mSUIWrapContent;

    Vector3 ActivePos = new Vector3(-47.9f, -95.52f, 0);
    Vector3 ClosePos = new Vector3(10000,10000,0);

    /// <summary>
    /// 选中值改变事件
    /// </summary>
    System.Action<string> OninputvalueChange;
    /// <summary>
    /// 值的List
    /// </summary>
    List<PopValue> VaList;
    CreateGridTools<PopValue> mCreateGridTools;

    /// <summary>
    /// 默认值
    /// </summary>
    string startValue = "";
    public void AddValue(List<PopValue> VaList,string str="",System.Action<string> action=null)
    {
        if (action != null)
            this.OninputvalueChange = action;

        this.VaList = VaList;

        if (str != "")
            InputShowLab.text = UITools.GetColorStr(str, UITools.cWhite);
        else
            InputShowLab.text =UITools.GetColorStr((string) VaList[0].value,UITools.cWhite);

        mCreateGridTools = new CreateGridTools<PopValue>();
        mCreateGridTools.InitData(Item1.transform.parent.gameObject, Item1, VaList, InitItem);

        DragList.SetActive(false);
    }

    public void AwakeData()
    {
        list.Add(this);

        VaList = new List<PopValue>();
        Back = WindowUtil.Control("Back", gameObject);
      
        ClickBtn = WindowUtil.Control("ClickBtn", gameObject);
        InputShowLab = WindowUtil.Control("InputShowLab", gameObject).GetComponent<UILabel>();
        DragList = WindowUtil.Control("DragList", gameObject);

        spr = ClickBtn.GetComponent<UISprite>();
        Item1 = WindowUtil.Control("Item1", gameObject);
        UIEventListener.Get(ClickBtn).onClick = ListBtnClick;       
    }

    UISprite spr;
    /// <summary>
    /// 下拉点击按钮
    /// </summary>
    /// <param name="go"></param>
    void ListBtnClick(GameObject go)
    {
        UISprite spr = go.GetComponent<UISprite>();
        if (spr.spriteName.Equals("Common_PopDown")) //可以下拉
        {
            currentPopList = this;
            Show();
            spr.spriteName = "Common_PopUp";
            //打开一个下拉条 关闭其他下拉条
            CloseOtherList(currentPopList);
        }
        else if(spr.spriteName.Equals("Common_PopUp"))//可以上拉
        {
            Close();
            spr.spriteName = "Common_PopDown";
        }
    }


    void CloseOtherList(PopupList current)
    {
        for(int i=0,length=list.Count;i<length;i++)
        {
            if(list[i]!=current)
            {
                list[i].Close();
            }
        }
    }
    

    #region 打开关闭拉框
    /// <summary>
    /// 展开下拉框
    /// </summary>
    void  Show()
    {
        Back.SetActive(true);
        DragList.SetActive(true);
    }

    /// <summary>
    /// 打开下拉框
    /// </summary>
   public void Close()
    {
        spr.spriteName = "Common_PopDown";
        Back.SetActive(false);
        DragList.SetActive(false);
    }
    #endregion 

   public UILabel ValueLab;
    void InitItem(GameObject go,PopValue Popvalue,int index)
    {
        go.name = index.ToString();
        UISprite ItemSprite=go.GetComponent<UISprite>();
        UILabel ValueLabs = WindowUtil.Control("ValueLab", go).GetComponent<UILabel>();

        ValueLabs.text = UITools.GetColorStr((string)Popvalue.value, UITools.cWhite);
        UIEventListener.Get(go).onClick=OnItemClick;
        UIEventListener.Get(go).onHover=OnMouseHaver;
    }

    GameObject lastSelect;
    /// <summary>
    /// 元素点击处理事件
    /// </summary>
    /// <param name="obj"></param>
    void OnItemClick(GameObject obj)
    {
       int index = int.Parse(obj.name);
       PopValue value = obj.GetComponent<GameObjectData>().Data as PopValue;
       InputShowLab.text = UITools.GetColorStr( (string)value.value,UITools.cWhite);

       if (OninputvalueChange!=null)
       {
           OninputvalueChange(InputShowLab.text);
       }

       WindowUtil.Control("Sprite", obj).SetActive(true);
       if (lastSelect != obj)
       {
           if (lastSelect != null)
           {
               WindowUtil.Control("Sprite", lastSelect).SetActive(false);
           }
           lastSelect = obj;
       }
    }

    void OnMouseHaver(GameObject go,bool isHover)
    {
        UISprite haver = WindowUtil.Control("Sprite", gameObject).GetComponent<UISprite>();
        if (isHover == true)
            haver.gameObject.SetActive(true);
        else
            haver.gameObject.SetActive(false);
    }
}

//TODO
编辑器重写
//----------------------------------------------
//            NGUI: Next-Gen UI kit
// Copyright © 2011-2014 Tasharen Entertainment
//----------------------------------------------

#if !UNITY_3_5 && !UNITY_FLASH
#define DYNAMIC_FONT
#endif

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

/// <summary>
/// Inspector class used to edit UIPopupLists.
/// </summary>

[CustomEditor(typeof(UIPopupList))]
public class UIPopupListInspector : UIWidgetContainerEditor
{
	enum FontType
	{
		Bitmap,
		Dynamic,
	}

	UIPopupList mList;
	FontType mType;

	void OnEnable ()
	{
		SerializedProperty bit = serializedObject.FindProperty("bitmapFont");
		mType = (bit.objectReferenceValue != null) ? FontType.Bitmap : FontType.Dynamic;
		mList = target as UIPopupList;

		if (mList.ambigiousFont == null)
		{
			mList.ambigiousFont = NGUISettings.ambigiousFont;
			mList.fontSize = NGUISettings.fontSize;
			mList.fontStyle = NGUISettings.fontStyle;
			EditorUtility.SetDirty(mList);
		}

		if (mList.atlas == null)
		{
			mList.atlas = NGUISettings.atlas;
			mList.backgroundSprite = NGUISettings.selectedSprite;
			mList.highlightSprite = NGUISettings.selectedSprite;
			EditorUtility.SetDirty(mList);
		}
	}

	void RegisterUndo ()
	{
		NGUIEditorTools.RegisterUndo("Popup List Change", mList);
	}

	void OnSelectAtlas (Object obj)
	{
		RegisterUndo();
		mList.atlas = obj as UIAtlas;
		NGUISettings.atlas = mList.atlas;
	}

	void OnBackground (string spriteName)
	{
		RegisterUndo();
		mList.backgroundSprite = spriteName;
		Repaint();
	}

	void OnHighlight (string spriteName)
	{
		RegisterUndo();
		mList.highlightSprite = spriteName;
		Repaint();
	}

	void OnBitmapFont (Object obj)
	{
		serializedObject.Update();
		SerializedProperty sp = serializedObject.FindProperty("bitmapFont");
		sp.objectReferenceValue = obj;
		serializedObject.ApplyModifiedProperties();
		NGUISettings.ambigiousFont = obj;
	}

	void OnDynamicFont (Object obj)
	{
		serializedObject.Update();
		SerializedProperty sp = serializedObject.FindProperty("trueTypeFont");
		sp.objectReferenceValue = obj;
		serializedObject.ApplyModifiedProperties();
		NGUISettings.ambigiousFont = obj;
	}

	public override void OnInspectorGUI ()
	{
		serializedObject.Update();
		NGUIEditorTools.SetLabelWidth(80f);

		GUILayout.BeginHorizontal();
		GUILayout.Space(6f);
		GUILayout.Label("Options");
		GUILayout.EndHorizontal();

		string text = "";
		foreach (string s in mList.items) text += s + "
";

		GUILayout.Space(-14f);
		GUILayout.BeginHorizontal();
		GUILayout.Space(84f);
		string modified = EditorGUILayout.TextArea(text, GUILayout.Height(100f));
		GUILayout.EndHorizontal();

		if (modified != text)
		{
			RegisterUndo();
			string[] split = modified.Split(new char[] { '
' }, System.StringSplitOptions.RemoveEmptyEntries);
			mList.items.Clear();
			foreach (string s in split) mList.items.Add(s);

			if (string.IsNullOrEmpty(mList.value) || !mList.items.Contains(mList.value))
			{
				mList.value = mList.items.Count > 0 ? mList.items[0] : "";
			}
		}

		GUI.changed = false;
		string sel = NGUIEditorTools.DrawList("Default", mList.items.ToArray(), mList.value);
		if (GUI.changed) serializedObject.FindProperty("mSelectedItem").stringValue = sel;

		NGUIEditorTools.DrawProperty("Position", serializedObject, "position");
		NGUIEditorTools.DrawProperty("Alignment", serializedObject, "alignment");
		NGUIEditorTools.DrawProperty("Open on", serializedObject, "openOn");
		NGUIEditorTools.DrawProperty("Localized", serializedObject, "isLocalized");

		DrawAtlas();
		DrawFont();

		NGUIEditorTools.DrawEvents("On Value Change", mList, mList.onChange);

		serializedObject.ApplyModifiedProperties();
	}

	void DrawAtlas()
	{
		if (NGUIEditorTools.DrawHeader("Atlas"))
		{
			NGUIEditorTools.BeginContents();

			GUILayout.BeginHorizontal();
			{
				if (NGUIEditorTools.DrawPrefixButton("Atlas"))
					ComponentSelector.Show<UIAtlas>(OnSelectAtlas);
				NGUIEditorTools.DrawProperty("", serializedObject, "atlas");
			}
			GUILayout.EndHorizontal();

			NGUIEditorTools.DrawPaddedSpriteField("Background", mList.atlas, mList.backgroundSprite, OnBackground);
			NGUIEditorTools.DrawPaddedSpriteField("Highlight", mList.atlas, mList.highlightSprite, OnHighlight);

			EditorGUILayout.Space();

			NGUIEditorTools.DrawProperty("Background", serializedObject, "backgroundColor");
			NGUIEditorTools.DrawProperty("Highlight", serializedObject, "highlightColor");
			NGUIEditorTools.DrawProperty("Animated", serializedObject, "isAnimated");
			NGUIEditorTools.EndContents();
		}
	}

	void DrawFont ()
	{
		if (NGUIEditorTools.DrawHeader("Font"))
		{
			NGUIEditorTools.BeginContents();

			SerializedProperty ttf = null;

			GUILayout.BeginHorizontal();
			{
				if (NGUIEditorTools.DrawPrefixButton("Font"))
				{
					if (mType == FontType.Bitmap)
					{
						ComponentSelector.Show<UIFont>(OnBitmapFont);
					}
					else
					{
						ComponentSelector.Show<Font>(OnDynamicFont, new string[] { ".ttf", ".otf"});
					}
				}

#if DYNAMIC_FONT
				GUI.changed = false;
				mType = (FontType)EditorGUILayout.EnumPopup(mType, GUILayout.Width(62f));

				if (GUI.changed)
				{
					GUI.changed = false;

					if (mType == FontType.Bitmap)
					{
						serializedObject.FindProperty("trueTypeFont").objectReferenceValue = null;
					}
					else
					{
						serializedObject.FindProperty("bitmapFont").objectReferenceValue = null;
					}
				}
#else
				mType = FontType.Bitmap;
#endif

				if (mType == FontType.Bitmap)
				{
					NGUIEditorTools.DrawProperty("", serializedObject, "bitmapFont", GUILayout.MinWidth(40f));
				}
				else
				{
					ttf = NGUIEditorTools.DrawProperty("", serializedObject, "trueTypeFont", GUILayout.MinWidth(40f));
				}
			}
			GUILayout.EndHorizontal();

			if (ttf != null && ttf.objectReferenceValue != null)
			{
				GUILayout.BeginHorizontal();
				{
					EditorGUI.BeginDisabledGroup(ttf.hasMultipleDifferentValues);
					NGUIEditorTools.DrawProperty("Font Size", serializedObject, "fontSize", GUILayout.Width(142f));
					NGUIEditorTools.DrawProperty("", serializedObject, "fontStyle", GUILayout.MinWidth(40f));
					NGUIEditorTools.DrawPadding();
					EditorGUI.EndDisabledGroup();
				}
				GUILayout.EndHorizontal();
			}
			else NGUIEditorTools.DrawProperty("Font Size", serializedObject, "fontSize", GUILayout.Width(142f));

			NGUIEditorTools.DrawProperty("Text Color", serializedObject, "textColor");

			GUILayout.BeginHorizontal();
			NGUIEditorTools.SetLabelWidth(66f);
			EditorGUILayout.PrefixLabel("Padding");
			NGUIEditorTools.SetLabelWidth(14f);
			NGUIEditorTools.DrawProperty("X", serializedObject, "padding.x", GUILayout.MinWidth(30f));
			NGUIEditorTools.DrawProperty("Y", serializedObject, "padding.y", GUILayout.MinWidth(30f));
			NGUIEditorTools.DrawPadding();
			NGUIEditorTools.SetLabelWidth(80f);
			GUILayout.EndHorizontal();

			NGUIEditorTools.EndContents();
		}
	}
}

  

原文地址:https://www.cnblogs.com/bambomtan/p/5602720.html