context: #if !UNITY_3_5 && !UNITY_FLASH
  #define DYNAMIC_FONT
  #endif
  
  using UnityEditor;
  using UnityEngine;
  using System.Collections.Generic;
  
  /// <summary>
  /// UI Widget Creation Wizard
  /// </summary>
  
  public class UICreateWidgetWizard : EditorWindow
  {
  	public enum WidgetType
  	{
  		Label,
  		Sprite,
  		Texture,
  		Button,
  		ImageButton,
  		Toggle,
  		ProgressBar,
  		Slider,
  		Input,
  		PopupList,
  		PopupMenu,
  		ScrollBar,
  	}
  
  	static WidgetType mWidgetType = WidgetType.Button;
  	static string mButton = "";
  	static string mImage0 = "";
  	static string mImage1 = "";
  	static string mImage2 = "";
  	static string mImage3 = "";
  	static string mSliderBG = "";
  	static string mSliderFG = "";
  	static string mSliderTB = "";
  	static string mCheckBG = "";
  	static string mCheck = "";
  	static string mInputBG = "";
  	static string mListFG = "";
  	static string mListBG = "";
  	static string mListHL = "";
  	static string mScrollBG = "";
  	static string mScrollFG = "";
  	static Color mColor = Color.white;
  	static bool mLoaded = false;
  	static bool mScrollCL = true;
  	static UIScrollBar.FillDirection mFillDir = UIScrollBar.FillDirection.LeftToRight;
  
  	/// <summary>
  	/// Save the specified string into player prefs.
  	/// </summary>
  
  	static void SaveString (string field, string val)
  	{
  		if (string.IsNullOrEmpty(val))
  		{
  			EditorPrefs.DeleteKey(field);
  		}
  		else
  		{
  			EditorPrefs.SetString(field, val);
  		}
  	}
  
  	/// <summary>
  	/// Load the specified string from player prefs.
  	/// </summary>
  
  	static string LoadString (string field) { string s = EditorPrefs.GetString(field); return (string.IsNullOrEmpty(s)) ? "" : s; }
  
  	/// <summary>
  	/// Save all serialized values in editor prefs.
  	/// This is necessary because static values get wiped out as soon as scripts get recompiled.
  	/// </summary>
  
  	static void Save ()
  	{
  		EditorPrefs.SetInt("NGUI Widget Type", (int)mWidgetType);
  		EditorPrefs.SetInt("NGUI Color", NGUIMath.ColorToInt(mColor));
  		EditorPrefs.SetBool("NGUI ScrollCL", mScrollCL);
  		EditorPrefs.SetInt("NGUI Fill Dir", (int)mFillDir);
  
  		SaveString("NGUI Button", mButton);
  		SaveString("NGUI Image 0", mImage0);
  		SaveString("NGUI Image 1", mImage1);
  		SaveString("NGUI Image 2", mImage2);
  		SaveString("NGUI Image 3", mImage3);
  		SaveString("NGUI CheckBG", mCheckBG);
  		SaveString("NGUI Check", mCheck);
  		SaveString("NGUI SliderBG", mSliderBG);
  		SaveString("NGUI SliderFG", mSliderFG);
  		SaveString("NGUI SliderTB", mSliderTB);
  		SaveString("NGUI InputBG", mInputBG);
  		SaveString("NGUI ListFG", mListFG);
  		SaveString("NGUI ListBG", mListBG);
  		SaveString("NGUI ListHL", mListHL);
  		SaveString("NGUI ScrollBG", mScrollBG);
  		SaveString("NGUI ScrollFG", mScrollFG);
  	}
  
  	/// <summary>
  	/// Load all serialized values from editor prefs.
  	/// This is necessary because static values get wiped out as soon as scripts get recompiled.
  	/// </summary>
  
  	static void Load ()
  	{
  		mWidgetType = (WidgetType)EditorPrefs.GetInt("NGUI Widget Type", 0);
  		mFillDir = (UIScrollBar.FillDirection)EditorPrefs.GetInt("NGUI Fill Dir", 0);
  
  		int color = EditorPrefs.GetInt("NGUI Color", -1);
  		if (color != -1) mColor = NGUIMath.IntToColor(color);
  
  		mButton		= LoadString("NGUI Button");
  		mImage0		= LoadString("NGUI Image 0");
  		mImage1		= LoadString("NGUI Image 1");
  		mImage2		= LoadString("NGUI Image 2");
  		mImage3		= LoadString("NGUI Image 3");
  		mCheckBG	= LoadString("NGUI CheckBG");
  		mCheck		= LoadString("NGUI Check");
  		mSliderBG	= LoadString("NGUI SliderBG");
  		mSliderFG	= LoadString("NGUI SliderFG");
  		mSliderTB	= LoadString("NGUI SliderTB");
  		mInputBG	= LoadString("NGUI InputBG");
  		mListFG		= LoadString("NGUI ListFG");
  		mListBG		= LoadString("NGUI ListBG");
  		mListHL		= LoadString("NGUI ListHL");
  		mScrollBG	= LoadString("NGUI ScrollBG");
  		mScrollFG	= LoadString("NGUI ScrollFG");
  		mScrollCL	= EditorPrefs.GetBool("NGUI ScrollCL", true);
  	}
  
  	/// <summary>
  	/// Atlas selection function.
  	/// </summary>
  
  	void OnSelectAtlas (Object obj)
  	{
  		if (NGUISettings.atlas != obj)
  		{
  			NGUISettings.atlas = obj as UIAtlas;
  			Repaint();
  		}
  	}
  
  	/// <summary>
  	/// Font selection function.
  	/// </summary>
  
  	void OnSelectFont (Object obj)
  	{
  		Object fnt = obj as UIFont;
  
  		if (NGUISettings.ambigiousFont != fnt)
  		{
  			NGUISettings.ambigiousFont = fnt;
  			Repaint();
  		}
  	}
  
  	/// <summary>
  	/// Convenience function -- creates the "Add To" button and the parent object field to the right of it.
  	/// </summary>
  
  	static public bool ShouldCreate (GameObject go, bool isValid)
  	{
  		GUI.color = isValid ? Color.green : Color.grey;
  
  		GUILayout.BeginHorizontal();
  		bool retVal = GUILayout.Button("Add To", GUILayout.Width(76f));
  		GUI.color = Color.white;
  		GameObject sel = EditorGUILayout.ObjectField(go, typeof(GameObject), true, GUILayout.Width(140f)) as GameObject;
  		GUILayout.Label("Select the parent in the Hierarchy View", GUILayout.MinWidth(10000f));
  		GUILayout.EndHorizontal();
  
  		if (sel != go) Selection.activeGameObject = sel;
  
  		if (retVal && isValid)
  		{
  			NGUIEditorTools.RegisterUndo("Add a Widget");
  			return true;
  		}
  		return false;
  	}
  
  	/// <summary>
  	/// Label creation function.
  	/// </summary>
  
  	void CreateLabel (GameObject go)
  	{
  		GUILayout.BeginHorizontal();
  		Color c = EditorGUILayout.ColorField("Color", mColor, GUILayout.Width(220f));
  		GUILayout.Label("Color tint the label will start with");
  		GUILayout.EndHorizontal();
  
  		if (mColor != c)
  		{
  			mColor = c;
  			Save();
  		}
  
  		if (ShouldCreate(go, NGUISettings.ambigiousFont != null))
  		{
  			Selection.activeGameObject = NGUISettings.AddLabel(go).gameObject;
  		}
  	}
  
  	/// <summary>
  	/// Sprite creation function.
  	/// </summary>
  
  	void CreateSprite (GameObject go)
  	{
  		if (NGUISettings.atlas != null)
  		{
  			NGUIEditorTools.DrawSpriteField("Sprite", "Sprite that will be created", NGUISettings.atlas, NGUISettings.selectedSprite, OnSprite, GUILayout.Width(120f));
  
  			if (!string.IsNullOrEmpty(NGUISettings.selectedSprite))
  			{
  				GUILayout.BeginHorizontal();
  				NGUISettings.pivot = (UIWidget.Pivot)EditorGUILayout.EnumPopup("Pivot", NGUISettings.pivot, GUILayout.Width(200f));
  				GUILayout.Space(20f);
  				GUILayout.Label("Initial pivot point used by the sprite");
  				GUILayout.EndHorizontal();
  			}
  		}
  
  		if (ShouldCreate(go, NGUISettings.atlas != null))
  		{
  			Selection.activeGameObject = NGUISettings.AddSprite(go).gameObject;
  		}
  	}
  
  	void OnSprite (string val)
  	{
  		if (NGUISettings.selectedSprite != val)
  		{
  			NGUISettings.selectedSprite = val;
  			Repaint();
  		}
  	}
  
  	/// <summary>
  	/// UI Texture doesn't do anything other than creating the widget.
  	/// </summary>
  
  	void CreateSimpleTexture (GameObject go)
  	{
  		if (ShouldCreate(go, true))
  		{
  			Selection.activeGameObject = NGUISettings.AddTexture(go).gameObject;
  		}
  	}
  
  	/// <summary>
  	/// Button creation function.
  	/// </summary>
  
  	void CreateButton (GameObject go)
  	{
  		if (NGUISettings.atlas != null)
  		{
  			NGUIEditorTools.DrawSpriteField("Background", "Sliced Sprite for the background", NGUISettings.atlas, mButton, OnButton, GUILayout.Width(120f));
  		}
  
  		if (ShouldCreate(go, NGUISettings.atlas != null))
  		{
  			int depth = NGUITools.CalculateNextDepth(go);
  			go = NGUITools.AddChild(go);
  			go.name = "Button";
  
  			UISprite bg = NGUITools.AddWidget<UISprite>(go);
  			bg.type = UISprite.Type.Sliced;
  			bg.name = "Background";
  			bg.depth = depth;
  			bg.atlas = NGUISettings.atlas;
  			bg.spriteName = mButton;
  			bg.width = 200;
  			bg.height = 50;
  			bg.MakePixelPerfect();
  
  			if (NGUISettings.ambigiousFont != null)
  			{
  				UILabel lbl = NGUITools.AddWidget<UILabel>(go);
  				lbl.ambigiousFont = NGUISettings.ambigiousFont;
  				lbl.text = go.name;
  				lbl.AssumeNaturalSize();
  			}
  
  			// Add a collider
  			NGUITools.AddWidgetCollider(go);
  
  			// Add the scripts
  			go.AddComponent<UIButton>().tweenTarget = bg.gameObject;
  			go.AddComponent<UIPlaySound>();
  
  			Selection.activeGameObject = go;
  		}
  	}
  
  	void OnButton (string val) { mButton = val; Save(); Repaint(); }
  
  	/// <summary>
  	/// Button creation function.
  	/// </summary>
  
  	void CreateImageButton (GameObject go)
  	{
  		if (NGUISettings.atlas != null)
  		{
  			NGUIEditorTools.DrawSpriteField("Normal", "Normal state sprite", NGUISettings.atlas, mImage0, OnImage0, GUILayout.Width(120f));
  			NGUIEditorTools.DrawSpriteField("Hover", "Hover state sprite", NGUISettings.atlas, mImage1, OnImage1, GUILayout.Width(120f));
  			NGUIEditorTools.DrawSpriteField("Pressed", "Pressed state sprite", NGUISettings.atlas, mImage2, OnImage2, GUILayout.Width(120f));
  			NGUIEditorTools.DrawSpriteField("Disabled", "Disabled state sprite", NGUISettings.atlas, mImage3, OnImage3, GUILayout.Width(120f));
  		}
  
  		if (ShouldCreate(go, NGUISettings.atlas != null))
  		{
  			int depth = NGUITools.CalculateNextDepth(go);
  			go = NGUITools.AddChild(go);
  			go.name = "Image Button";
  
  			UISpriteData sp = NGUISettings.atlas.GetSprite(mImage0);
  			UISprite sprite = NGUITools.AddWidget<UISprite>(go);
  			sprite.type = sp.hasBorder ? UISprite.Type.Sliced : UISprite.Type.Simple;
  			sprite.name = "Background";
  			sprite.depth = depth;
  			sprite.atlas = NGUISettings.atlas;
  			sprite.spriteName = mImage0;
  			sprite.width = 150;
  			sprite.height = 40;
  			sprite.MakePixelPerfect();
  
  			if (NGUISettings.ambigiousFont != null)
  			{
  				UILabel lbl = NGUITools.AddWidget<UILabel>(go);
  				lbl.ambigiousFont = NGUISettings.ambigiousFont;
  				lbl.text = go.name;
  				lbl.AssumeNaturalSize();
  			}
  
  			// Add a collider
  			NGUITools.AddWidgetCollider(go);
  
  			// Add the scripts
  			UIImageButton ib	= go.AddComponent<UIImageButton>();
  			ib.target			= sprite;
  			ib.normalSprite		= mImage0;
  			ib.hoverSprite		= mImage1;
  			ib.pressedSprite	= mImage2;
  			ib.disabledSprite	= mImage3;
  			go.AddComponent<UIPlaySound>();
  
  			Selection.activeGameObject = go;
  		}
  	}
  
  	void OnImage0 (string val) { mImage0 = val; Save(); Repaint(); }
  	void OnImage1 (string val) { mImage1 = val; Save(); Repaint(); }
  	void OnImage2 (string val) { mImage2 = val; Save(); Repaint(); }
  	void OnImage3 (string val) { mImage3 = val; Save(); Repaint(); }
  
  	/// <summary>
  	/// Toggle creation function.
  	/// </summary>
  
  	void CreateToggle (GameObject go)
  	{
  		if (NGUISettings.atlas != null)
  		{
  			NGUIEditorTools.DrawSpriteField("Background", "Sprite used for the background", NGUISettings.atlas, mCheckBG, OnCheckBG, GUILayout.Width(120f));
  			NGUIEditorTools.DrawSpriteField("Checkmark", "Sprite used for the checkmark", NGUISettings.atlas, mCheck, OnCheck, GUILayout.Width(120f));
  		}
  
  
gt: 		if (ShouldCreate(go, NGUISettings.atlas != null))