/// <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.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; } }
/// <summary> /// Create a popup list or a menu. /// </summary> void CreatePopup(GameObject go, bool isDropDown) { if (NGUISettings.atlas != null) { NGUIEditorTools.DrawSpriteField("Foreground", "Foreground sprite (shown on the button)", NGUISettings.atlas, mListFG, OnListFG, GUILayout.Width(120f)); NGUIEditorTools.DrawSpriteField("Background", "Background sprite (envelops the options)", NGUISettings.atlas, mListBG, OnListBG, GUILayout.Width(120f)); NGUIEditorTools.DrawSpriteField("Highlight", "Sprite used to highlight the selected option", NGUISettings.atlas, mListHL, OnListHL, GUILayout.Width(120f)); } if (ShouldCreate(go, NGUISettings.atlas != null && NGUISettings.ambigiousFont != null)) { int depth = NGUITools.CalculateNextDepth(go); go = NGUITools.AddChild(go); go.name = isDropDown ? "Popup List" : "Popup Menu"; UISpriteData sphl = NGUISettings.GetSprite(mListHL); UISpriteData spfg = NGUISettings.GetSprite(mListFG); Vector2 hlPadding = new Vector2(Mathf.Max(4f, sphl.paddingLeft), Mathf.Max(4f, sphl.paddingTop)); Vector2 fgPadding = new Vector2(Mathf.Max(4f, spfg.paddingLeft), Mathf.Max(4f, spfg.paddingTop)); // Background sprite UISprite sprite = NGUITools.AddSprite(go, NGUISettings.atlas as INGUIAtlas, mListFG); sprite.depth = depth; sprite.atlas = NGUISettings.atlas; sprite.pivot = UIWidget.Pivot.Left; sprite.width = Mathf.RoundToInt(150f + fgPadding.x * 2f); sprite.height = Mathf.RoundToInt(NGUISettings.fontSize + fgPadding.y * 2f); sprite.transform.localPosition = Vector3.zero; sprite.MakePixelPerfect(); // Text label UILabel lbl = NGUITools.AddWidget <UILabel>(go); lbl.ambigiousFont = NGUISettings.ambigiousFont; lbl.fontSize = NGUISettings.fontSize; lbl.fontStyle = NGUISettings.fontStyle; lbl.text = go.name; lbl.pivot = UIWidget.Pivot.Left; lbl.cachedTransform.localPosition = new Vector3(fgPadding.x, 0f, 0f); lbl.AssumeNaturalSize(); // Add a collider NGUITools.AddWidgetCollider(go); // Add the popup list UIPopupList list = go.AddComponent <UIPopupList>(); list.atlas = NGUISettings.atlas as Object; list.ambigiousFont = NGUISettings.ambigiousFont; list.fontSize = NGUISettings.fontSize; list.fontStyle = NGUISettings.fontStyle; list.backgroundSprite = mListBG; list.highlightSprite = mListHL; list.padding = hlPadding; if (isDropDown) { EventDelegate.Add(list.onChange, lbl.SetCurrentSelection); } for (int i = 0; i < 5; ++i) { list.items.Add(isDropDown ? ("List Option " + i) : ("Menu Option " + i)); } // Add the scripts go.AddComponent <UIButton>().tweenTarget = sprite.gameObject; go.AddComponent <UIPlaySound>(); Selection.activeGameObject = go; } }
/// <summary> /// Progress bar creation function. /// </summary> void CreateSlider(GameObject go, bool slider) { if (NGUISettings.atlas != null) { NGUIEditorTools.DrawSpriteField("Empty", "Sprite for the background (empty bar)", NGUISettings.atlas, mSliderBG, OnSliderBG, GUILayout.Width(120f)); NGUIEditorTools.DrawSpriteField("Full", "Sprite for the foreground (full bar)", NGUISettings.atlas, mSliderFG, OnSliderFG, GUILayout.Width(120f)); if (slider) { NGUIEditorTools.DrawSpriteField("Thumb", "Sprite for the thumb indicator", NGUISettings.atlas, mSliderTB, OnSliderTB, GUILayout.Width(120f)); } } if (ShouldCreate(go, NGUISettings.atlas != null)) { int depth = NGUITools.CalculateNextDepth(go); go = NGUITools.AddChild(go); go.name = slider ? "Slider" : "Progress Bar"; // Background sprite var bgs = NGUISettings.GetSprite(mSliderBG); var back = NGUITools.AddWidget <UISprite>(go); back.type = bgs.hasBorder ? UISprite.Type.Sliced : UISprite.Type.Simple; back.name = "Background"; back.depth = depth; back.pivot = UIWidget.Pivot.Left; back.atlas = NGUISettings.atlas; back.spriteName = mSliderBG; back.width = 200; back.height = 30; back.transform.localPosition = Vector3.zero; back.MakePixelPerfect(); // Foreground sprite UISpriteData fgs = NGUISettings.GetSprite(mSliderFG); UISprite front = NGUITools.AddWidget <UISprite>(go); front.type = fgs.hasBorder ? UISprite.Type.Sliced : UISprite.Type.Simple; front.name = "Foreground"; front.pivot = UIWidget.Pivot.Left; front.atlas = NGUISettings.atlas; front.spriteName = mSliderFG; front.width = 200; front.height = 30; front.transform.localPosition = Vector3.zero; front.MakePixelPerfect(); // Add a collider if (slider) { NGUITools.AddWidgetCollider(go); } // Add the slider script UISlider uiSlider = go.AddComponent <UISlider>(); uiSlider.foregroundWidget = front; // Thumb sprite if (slider) { UISpriteData tbs = NGUISettings.GetSprite(mSliderTB); UISprite thb = NGUITools.AddWidget <UISprite>(go); thb.type = tbs.hasBorder ? UISprite.Type.Sliced : UISprite.Type.Simple; thb.name = "Thumb"; thb.atlas = NGUISettings.atlas; thb.spriteName = mSliderTB; thb.width = 20; thb.height = 40; thb.transform.localPosition = new Vector3(200f, 0f, 0f); thb.MakePixelPerfect(); NGUITools.AddWidgetCollider(thb.gameObject); thb.gameObject.AddComponent <UIButtonColor>(); thb.gameObject.AddComponent <UIButtonScale>(); uiSlider.thumb = thb.transform; } uiSlider.value = 1f; // Select the slider Selection.activeGameObject = go; } }