public override void OnInspectorGUI() { EditorGUIUtility.LookLikeControls(80f); mButton = target as NGUIImageButton; mSprite = EditorGUILayout.ObjectField("Sprite", mButton.target, typeof(NGUISprite), true) as NGUISprite; if (mButton.target != mSprite) { NGUIEditorTools.RegisterUndo("Image Button Change", mButton); mButton.target = mSprite; if (mSprite != null) mSprite.spriteName = mButton.normalSprite; } if (mSprite != null) { ComponentSelector.Draw<NGUIAtlas>(mSprite.atlas, OnSelectAtlas); if (mSprite.atlas != null) { NGUIEditorTools.SpriteField("Normal", mSprite.atlas, mButton.normalSprite, OnNormal); NGUIEditorTools.SpriteField("Hover", mSprite.atlas, mButton.hoverSprite, OnHover); NGUIEditorTools.SpriteField("Pressed", mSprite.atlas, mButton.pressedSprite, OnPressed); NGUIEditorTools.SpriteField("Disabled", mSprite.atlas, mButton.disabledSprite, OnDisabled); } } }
void ValidatePivot(Transform fg, string name, NGUISlider.Direction dir) { if (fg != null) { NGUISprite sprite = fg.GetComponent <NGUISprite>(); if (sprite != null && sprite.type != NGUISprite.Type.Filled) { if (dir == NGUISlider.Direction.Horizontal) { if (sprite.pivot != NGUIWidget.Pivot.Left && sprite.pivot != NGUIWidget.Pivot.TopLeft && sprite.pivot != NGUIWidget.Pivot.BottomLeft) { GUI.color = new Color(1f, 0.7f, 0f); GUILayout.Label(name + " should use a Left pivot"); GUI.color = Color.white; } } else if (sprite.pivot != NGUIWidget.Pivot.BottomLeft && sprite.pivot != NGUIWidget.Pivot.Bottom && sprite.pivot != NGUIWidget.Pivot.BottomRight) { GUI.color = new Color(1f, 0.7f, 0f); GUILayout.Label(name + " should use a Bottom pivot"); GUI.color = Color.white; } } } }
/// <summary> /// Sprite creation function. /// </summary> void CreateSprite(GameObject go, string field) { if (NGUISettings.atlas != null) { NGUIEditorTools.SpriteField("Sprite", "Sprite that will be created", NGUISettings.atlas, field, OnSprite); if (!string.IsNullOrEmpty(field)) { GUILayout.BeginHorizontal(); NGUISettings.pivot = (NGUIWidget.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)) { NGUISprite sprite = NGUITools.AddWidget <NGUISprite>(go); sprite.name = sprite.name + " (" + field + ")"; sprite.atlas = NGUISettings.atlas; sprite.spriteName = field; sprite.pivot = NGUISettings.pivot; sprite.MakePixelPerfect(); Selection.activeGameObject = sprite.gameObject; } }
/// <summary> /// Show the selection wizard. /// </summary> public static void Show(NGUIAtlas atlas, NGUISprite selectedSprite) { SpriteSelector comp = ScriptableWizard.DisplayWizard<SpriteSelector>("Select a Sprite"); comp.mAtlas = atlas; comp.mSprite = selectedSprite; comp.mCallback = null; }
/// <summary> /// Initialize the cached values. /// </summary> void Init() { mInitDone = true; if (foreground != null) { mFGWidget = foreground.GetComponent <NGUIWidget>(); mFGFilled = (mFGWidget != null) ? mFGWidget as NGUISprite : null; mFGTrans = foreground.transform; if (mSize == Vector2.zero) { mSize = foreground.localScale; } if (mCenter == Vector2.zero) { mCenter = foreground.localPosition + foreground.localScale * 0.5f; } } else if (mCol != null) { if (mSize == Vector2.zero) { mSize = mCol.size; } if (mCenter == Vector2.zero) { mCenter = mCol.center; } } else { Debug.LogWarning("NGUISlider expected to find a foreground object or a box collider to work with", this); } }
void Start() { if (target == null) { target = GetComponentInChildren <NGUISprite>(); } }
public override void OnInspectorGUI() { EditorGUIUtility.LookLikeControls(80f); mButton = target as NGUIImageButton; mSprite = EditorGUILayout.ObjectField("Sprite", mButton.target, typeof(NGUISprite), true) as NGUISprite; if (mButton.target != mSprite) { NGUIEditorTools.RegisterUndo("Image Button Change", mButton); mButton.target = mSprite; if (mSprite != null) { mSprite.spriteName = mButton.normalSprite; } } if (mSprite != null) { ComponentSelector.Draw <NGUIAtlas>(mSprite.atlas, OnSelectAtlas); if (mSprite.atlas != null) { NGUIEditorTools.SpriteField("Normal", mSprite.atlas, mButton.normalSprite, OnNormal); NGUIEditorTools.SpriteField("Hover", mSprite.atlas, mButton.hoverSprite, OnHover); NGUIEditorTools.SpriteField("Pressed", mSprite.atlas, mButton.pressedSprite, OnPressed); NGUIEditorTools.SpriteField("Disabled", mSprite.atlas, mButton.disabledSprite, OnDisabled); } } }
/// <summary> /// Scroll bar template. /// </summary> void CreateScrollBar(GameObject go) { if (NGUISettings.atlas != null) { NGUIEditorTools.SpriteField("Background", "Sprite used for the background", NGUISettings.atlas, mScrollBG, OnScrollBG); NGUIEditorTools.SpriteField("Foreground", "Sprite used for the foreground (thumb)", NGUISettings.atlas, mScrollFG, OnScrollFG); GUILayout.BeginHorizontal(); NGUIScrollBar.Direction dir = (NGUIScrollBar.Direction)EditorGUILayout.EnumPopup("Direction", mScrollDir, GUILayout.Width(200f)); GUILayout.Space(20f); GUILayout.Label("Add colliders?", GUILayout.Width(90f)); bool draggable = EditorGUILayout.Toggle(mScrollCL); GUILayout.EndHorizontal(); if (mScrollCL != draggable || mScrollDir != dir) { mScrollCL = draggable; mScrollDir = dir; Save(); } } if (ShouldCreate(go, NGUISettings.atlas != null)) { int depth = NGUITools.CalculateNextDepth(go); go = NGUITools.AddChild(go); go.name = "Scroll Bar"; NGUISprite bg = NGUITools.AddWidget <NGUISprite>(go); bg.type = NGUISprite.Type.Sliced; bg.name = "Background"; bg.depth = depth; bg.atlas = NGUISettings.atlas; bg.spriteName = mScrollBG; bg.transform.localScale = new Vector3(400f + bg.border.x + bg.border.z, 14f + bg.border.y + bg.border.w, 1f); bg.MakePixelPerfect(); NGUISprite fg = NGUITools.AddWidget <NGUISprite>(go); fg.type = NGUISprite.Type.Sliced; fg.name = "Foreground"; fg.atlas = NGUISettings.atlas; fg.spriteName = mScrollFG; NGUIScrollBar sb = go.AddComponent <NGUIScrollBar>(); sb.background = bg; sb.foreground = fg; sb.direction = mScrollDir; sb.barSize = 0.3f; sb.scrollValue = 0.3f; sb.ForceUpdate(); if (mScrollCL) { NGUITools.AddWidgetCollider(bg.gameObject); NGUITools.AddWidgetCollider(fg.gameObject); } Selection.activeGameObject = go; } }
/// <summary> /// Show the selection wizard. /// </summary> public static void Show(NGUIAtlas atlas, NGUISprite selectedSprite) { SpriteSelector comp = ScriptableWizard.DisplayWizard <SpriteSelector>("Select a Sprite"); comp.mAtlas = atlas; comp.mSprite = selectedSprite; comp.mCallback = null; }
/// <summary> /// Add a sprite appropriate for the specified atlas sprite. /// It will be sliced if the sprite has an inner rect, and a regular sprite otherwise. /// </summary> static public NGUISprite AddSprite(GameObject go, NGUIAtlas atlas, string spriteName) { NGUIAtlas.Sprite sp = (atlas != null) ? atlas.GetSprite(spriteName) : null; NGUISprite sprite = AddWidget <NGUISprite>(go); sprite.type = (sp == null || sp.inner == sp.outer) ? NGUISprite.Type.Simple : NGUISprite.Type.Sliced; sprite.atlas = atlas; sprite.spriteName = spriteName; return(sprite); }
/// <summary> /// Draw the atlas and sprite selection fields. /// </summary> override protected bool DrawProperties() { mSprite = mWidget as NGUISprite; ComponentSelector.Draw <NGUIAtlas>(mSprite.atlas, OnSelectAtlas); if (mSprite.atlas == null) { return(false); } NGUIEditorTools.AdvancedSpriteField(mSprite.atlas, mSprite.spriteName, SelectSprite, false); return(true); }
/// <summary> /// Checkbox creation function. /// </summary> void CreateCheckbox(GameObject go) { if (NGUISettings.atlas != null) { NGUIEditorTools.SpriteField("Background", "Sprite used for the background", NGUISettings.atlas, mCheckBG, OnCheckBG); NGUIEditorTools.SpriteField("Checkmark", "Sprite used for the checkmark", NGUISettings.atlas, mCheck, OnCheck); } if (ShouldCreate(go, NGUISettings.atlas != null)) { int depth = NGUITools.CalculateNextDepth(go); go = NGUITools.AddChild(go); go.name = "Checkbox"; NGUISprite bg = NGUITools.AddWidget <NGUISprite>(go); bg.type = NGUISprite.Type.Sliced; bg.name = "Background"; bg.depth = depth; bg.atlas = NGUISettings.atlas; bg.spriteName = mCheckBG; bg.transform.localScale = new Vector3(26f, 26f, 1f); bg.MakePixelPerfect(); NGUISprite fg = NGUITools.AddWidget <NGUISprite>(go); fg.name = "Checkmark"; fg.atlas = NGUISettings.atlas; fg.spriteName = mCheck; fg.MakePixelPerfect(); if (NGUISettings.font != null) { NGUILabel lbl = NGUITools.AddWidget <NGUILabel>(go); lbl.font = NGUISettings.font; lbl.text = go.name; lbl.pivot = NGUIWidget.Pivot.Left; lbl.transform.localPosition = new Vector3(16f, 0f, 0f); lbl.MakePixelPerfect(); } // Add a collider NGUITools.AddWidgetCollider(go); // Add the scripts go.AddComponent <NGUICheckbox>().checkSprite = fg; go.AddComponent <NGUIButton>().tweenTarget = bg.gameObject; go.AddComponent <NGUIButtonScale>().tweenTarget = bg.transform; go.AddComponent <NGUIButtonSound>(); Selection.activeGameObject = go; } }
/// <summary> /// Button creation function. /// </summary> void CreateImageButton(GameObject go) { if (NGUISettings.atlas != null) { NGUIEditorTools.SpriteField("Normal", "Normal state sprite", NGUISettings.atlas, mImage0, OnImage0); NGUIEditorTools.SpriteField("Hover", "Hover state sprite", NGUISettings.atlas, mImage1, OnImage1); NGUIEditorTools.SpriteField("Pressed", "Pressed state sprite", NGUISettings.atlas, mImage2, OnImage2); NGUIEditorTools.SpriteField("Disabled", "Disabled state sprite", NGUISettings.atlas, mImage3, OnImage3); } if (ShouldCreate(go, NGUISettings.atlas != null)) { int depth = NGUITools.CalculateNextDepth(go); go = NGUITools.AddChild(go); go.name = "Image Button"; NGUIAtlas.Sprite sp = NGUISettings.atlas.GetSprite(mImage0); NGUISprite sprite = NGUITools.AddWidget <NGUISprite>(go); sprite.type = (sp.inner == sp.outer) ? NGUISprite.Type.Simple : NGUISprite.Type.Sliced; sprite.name = "Background"; sprite.depth = depth; sprite.atlas = NGUISettings.atlas; sprite.spriteName = mImage0; sprite.transform.localScale = new Vector3(150f, 40f, 1f); sprite.MakePixelPerfect(); if (NGUISettings.font != null) { NGUILabel lbl = NGUITools.AddWidget <NGUILabel>(go); lbl.font = NGUISettings.font; lbl.text = go.name; lbl.MakePixelPerfect(); } // Add a collider NGUITools.AddWidgetCollider(go); // Add the scripts NGUIImageButton ib = go.AddComponent <NGUIImageButton>(); ib.target = sprite; ib.normalSprite = mImage0; ib.hoverSprite = mImage1; ib.pressedSprite = mImage2; go.AddComponent <NGUIButtonSound>(); Selection.activeGameObject = go; } }
/// <summary> /// Input field creation function. /// </summary> void CreateInput(GameObject go) { if (NGUISettings.atlas != null) { NGUIEditorTools.SpriteField("Background", "Sliced Sprite for the background", NGUISettings.atlas, mInputBG, OnInputBG); } if (ShouldCreate(go, NGUISettings.atlas != null && NGUISettings.font != null)) { int depth = NGUITools.CalculateNextDepth(go); go = NGUITools.AddChild(go); go.name = "Input"; float padding = 3f; NGUISprite bg = NGUITools.AddWidget <NGUISprite>(go); bg.type = NGUISprite.Type.Sliced; bg.name = "Background"; bg.depth = depth; bg.atlas = NGUISettings.atlas; bg.spriteName = mInputBG; bg.pivot = NGUIWidget.Pivot.Left; bg.transform.localScale = new Vector3(400f, NGUISettings.font.size + padding * 2f, 1f); bg.transform.localPosition = Vector3.zero; bg.MakePixelPerfect(); NGUILabel lbl = NGUITools.AddWidget <NGUILabel>(go); lbl.font = NGUISettings.font; lbl.pivot = NGUIWidget.Pivot.Left; lbl.transform.localPosition = new Vector3(padding, 0f, 0f); lbl.multiLine = false; lbl.supportEncoding = false; lbl.lineWidth = Mathf.RoundToInt(400f - padding * 2f); lbl.text = "You can type here"; lbl.MakePixelPerfect(); // Add a collider to the background NGUITools.AddWidgetCollider(go); // Add an input script to the background and have it point to the label NGUIInput input = go.AddComponent <NGUIInput>(); input.label = lbl; // Update the selection Selection.activeGameObject = go; } }
/// <summary> /// Button creation function. /// </summary> void CreateButton(GameObject go) { if (NGUISettings.atlas != null) { NGUIEditorTools.SpriteField("Background", "Sliced Sprite for the background", NGUISettings.atlas, mButton, OnButton); } if (ShouldCreate(go, NGUISettings.atlas != null)) { int depth = NGUITools.CalculateNextDepth(go); go = NGUITools.AddChild(go); go.name = "Button"; NGUISprite bg = NGUITools.AddWidget <NGUISprite>(go); bg.type = NGUISprite.Type.Sliced; bg.name = "Background"; bg.depth = depth; bg.atlas = NGUISettings.atlas; bg.spriteName = mButton; bg.transform.localScale = new Vector3(150f, 40f, 1f); bg.MakePixelPerfect(); if (NGUISettings.font != null) { NGUILabel lbl = NGUITools.AddWidget <NGUILabel>(go); lbl.font = NGUISettings.font; lbl.text = go.name; lbl.MakePixelPerfect(); } // Add a collider NGUITools.AddWidgetCollider(go); // Add the scripts go.AddComponent <NGUIButton>().tweenTarget = bg.gameObject; go.AddComponent <NGUIButtonScale>(); go.AddComponent <NGUIButtonOffset>(); go.AddComponent <NGUIButtonSound>(); Selection.activeGameObject = go; } }
public override void OnInspectorGUI() { EditorGUIUtility.LookLikeControls(80f); NGUIScrollBar sb = target as NGUIScrollBar; NGUIEditorTools.DrawSeparator(); float val = EditorGUILayout.Slider("Value", sb.scrollValue, 0f, 1f); float size = EditorGUILayout.Slider("Size", sb.barSize, 0f, 1f); float alpha = EditorGUILayout.Slider("Alpha", sb.alpha, 0f, 1f); NGUIEditorTools.DrawSeparator(); NGUISprite bg = (NGUISprite)EditorGUILayout.ObjectField("Background", sb.background, typeof(NGUISprite), true); NGUISprite fg = (NGUISprite)EditorGUILayout.ObjectField("Foreground", sb.foreground, typeof(NGUISprite), true); NGUIScrollBar.Direction dir = (NGUIScrollBar.Direction)EditorGUILayout.EnumPopup("Direction", sb.direction); bool inv = EditorGUILayout.Toggle("Inverted", sb.inverted); if (sb.scrollValue != val || sb.barSize != size || sb.background != bg || sb.foreground != fg || sb.direction != dir || sb.inverted != inv || sb.alpha != alpha) { NGUIEditorTools.RegisterUndo("Scroll Bar Change", sb); sb.scrollValue = val; sb.barSize = size; sb.inverted = inv; sb.background = bg; sb.foreground = fg; sb.direction = dir; sb.alpha = alpha; UnityEditor.EditorUtility.SetDirty(sb); } }
static public void AddSprite() { GameObject go = NGUIEditorTools.SelectedRoot(true); if (go != null) { Undo.RegisterSceneUndo("Add a Sprite"); NGUISprite sprite = NGUITools.AddWidget <NGUISprite>(go); sprite.name = "Sprite"; sprite.atlas = NGUISettings.atlas; if (sprite.atlas != null) { string sn = EditorPrefs.GetString("NGUI Sprite", ""); NGUIAtlas.Sprite sp = sprite.atlas.GetSprite(sn); if (sp != null) { sprite.spriteName = sn; if (sp.inner != sp.outer) { sprite.type = NGUISprite.Type.Sliced; } } } sprite.pivot = NGUISettings.pivot; sprite.cachedTransform.localScale = new Vector3(100f, 100f, 1f); sprite.MakePixelPerfect(); Selection.activeGameObject = sprite.gameObject; } else { Debug.Log("You must select a game object first."); } }
/// <summary> /// Get rid of the popup dialog when the selection gets lost. /// </summary> void OnSelect(bool isSelected) { if (!isSelected && mChild != null) { mLabelList.Clear(); handleEvents = false; if (isAnimated) { NGUIWidget[] widgets = mChild.GetComponentsInChildren <NGUIWidget>(); for (int i = 0, imax = widgets.Length; i < imax; ++i) { NGUIWidget w = widgets[i]; Color c = w.color; c.a = 0f; TweenColor.Begin(w.gameObject, animSpeed, c).method = NGUITweener.Method.EaseOut; } Collider[] cols = mChild.GetComponentsInChildren <Collider>(); for (int i = 0, imax = cols.Length; i < imax; ++i) { cols[i].enabled = false; } UpdateManager.AddDestroy(mChild, animSpeed); } else { Destroy(mChild); } mBackground = null; mHighlight = null; mChild = null; } }
/// <summary> /// Force-localize the widget. /// </summary> public void Localize() { Localization loc = Localization.instance; NGUIWidget w = GetComponent <NGUIWidget>(); NGUILabel lbl = w as NGUILabel; NGUISprite sp = w as NGUISprite; // If no localization key has been specified, use the label's text as the key if (string.IsNullOrEmpty(mLanguage) && string.IsNullOrEmpty(key) && lbl != null) { key = lbl.text; } // If we still don't have a key, leave the value as blank string val = string.IsNullOrEmpty(key) ? "" : loc.Get(key); if (lbl != null) { // If this is a label used by input, we should localize its default value instead NGUIInput input = NGUITools.FindInParents <NGUIInput>(lbl.gameObject); if (input != null && input.label == lbl) { input.defaultText = val; } else { lbl.text = val; } } else if (sp != null) { sp.spriteName = val; sp.MakePixelPerfect(); } mLanguage = loc.currentLanguage; }
/// <summary> /// Rebuild the sprite list after changing the sprite name. /// </summary> void RebuildSpriteList() { if (mSprite == null) { mSprite = GetComponent <NGUISprite>(); } mSpriteNames.Clear(); if (mSprite != null && mSprite.atlas != null) { List <NGUIAtlas.Sprite> sprites = mSprite.atlas.spriteList; for (int i = 0, imax = sprites.Count; i < imax; ++i) { NGUIAtlas.Sprite sprite = sprites[i]; if (string.IsNullOrEmpty(mPrefix) || sprite.name.StartsWith(mPrefix)) { mSpriteNames.Add(sprite.name); } } mSpriteNames.Sort(); } }
/// <summary> /// Get rid of the popup dialog when the selection gets lost. /// </summary> void OnSelect(bool isSelected) { if (!isSelected && mChild != null) { mLabelList.Clear(); handleEvents = false; if (isAnimated) { NGUIWidget[] widgets = mChild.GetComponentsInChildren<NGUIWidget>(); for (int i = 0, imax = widgets.Length; i < imax; ++i) { NGUIWidget w = widgets[i]; Color c = w.color; c.a = 0f; TweenColor.Begin(w.gameObject, animSpeed, c).method = NGUITweener.Method.EaseOut; } Collider[] cols = mChild.GetComponentsInChildren<Collider>(); for (int i = 0, imax = cols.Length; i < imax; ++i) cols[i].enabled = false; UpdateManager.AddDestroy(mChild, animSpeed); } else { Destroy(mChild); } mBackground = null; mHighlight = null; mChild = null; } }
/// <summary> /// Create a popup list or a menu. /// </summary> void CreatePopup(GameObject go, bool isDropDown) { if (NGUISettings.atlas != null) { NGUIEditorTools.SpriteField("Foreground", "Foreground sprite (shown on the button)", NGUISettings.atlas, mListFG, OnListFG); NGUIEditorTools.SpriteField("Background", "Background sprite (envelops the options)", NGUISettings.atlas, mListBG, OnListBG); NGUIEditorTools.SpriteField("Highlight", "Sprite used to highlight the selected option", NGUISettings.atlas, mListHL, OnListHL); } if (ShouldCreate(go, NGUISettings.atlas != null && NGUISettings.font != null)) { int depth = NGUITools.CalculateNextDepth(go); go = NGUITools.AddChild(go); go.name = isDropDown ? "Popup List" : "Popup Menu"; NGUIAtlas.Sprite sphl = NGUISettings.atlas.GetSprite(mListHL); NGUIAtlas.Sprite spfg = NGUISettings.atlas.GetSprite(mListFG); Vector2 hlPadding = new Vector2( Mathf.Max(4f, sphl.inner.xMin - sphl.outer.xMin), Mathf.Max(4f, sphl.inner.yMin - sphl.outer.yMin)); Vector2 fgPadding = new Vector2( Mathf.Max(4f, spfg.inner.xMin - spfg.outer.xMin), Mathf.Max(4f, spfg.inner.yMin - spfg.outer.yMin)); // Background sprite NGUISprite sprite = NGUITools.AddSprite(go, NGUISettings.atlas, mListFG); sprite.depth = depth; sprite.atlas = NGUISettings.atlas; sprite.pivot = NGUIWidget.Pivot.Left; sprite.transform.localScale = new Vector3(150f + fgPadding.x * 2f, NGUISettings.font.size + fgPadding.y * 2f, 1f); sprite.transform.localPosition = Vector3.zero; sprite.MakePixelPerfect(); // Text label NGUILabel lbl = NGUITools.AddWidget <NGUILabel>(go); lbl.font = NGUISettings.font; lbl.text = go.name; lbl.pivot = NGUIWidget.Pivot.Left; lbl.cachedTransform.localPosition = new Vector3(fgPadding.x, 0f, 0f); lbl.MakePixelPerfect(); // Add a collider NGUITools.AddWidgetCollider(go); // Add the popup list NGUIPopupList list = go.AddComponent <NGUIPopupList>(); list.atlas = NGUISettings.atlas; list.font = NGUISettings.font; list.backgroundSprite = mListBG; list.highlightSprite = mListHL; list.padding = hlPadding; if (isDropDown) { list.textLabel = lbl; } for (int i = 0; i < 5; ++i) { list.items.Add(isDropDown ? ("List Option " + i) : ("Menu Option " + i)); } // Add the scripts go.AddComponent <NGUIButton>().tweenTarget = sprite.gameObject; go.AddComponent <NGUIButtonSound>(); Selection.activeGameObject = go; } }
/// <summary> /// Convenience function. /// </summary> public static Bounds CalculateRelativeInnerBounds(Transform root, NGUISprite sprite) { if (sprite.type == NGUISprite.Type.Sliced || sprite.type == NGUISprite.Type.MirrorSliced) { Matrix4x4 toLocal = root.worldToLocalMatrix; Vector2 size = sprite.relativeSize; Vector2 offset = sprite.pivotOffset; Transform toWorld = sprite.cachedTransform; float x = (offset.x + 0.5f) * size.x; float y = (offset.y - 0.5f) * size.y; size *= 0.5f; float sx = toWorld.localScale.x; float sy = toWorld.localScale.y; // Get the border in pixels Vector4 border = sprite.border; // Convert pixels to relative coordinates if (sx != 0f) { border.x /= sx; border.z /= sx; } if (sy != 0f) { border.y /= sy; border.w /= sy; } // Calculate the relative dimensions float left = x - size.x + border.x; float right = x + size.x - border.z; float top = y - size.y + border.y; float bottom = y + size.y - border.w; // Start with the corner of the widget Vector3 v = new Vector3(left, top, 0f); v = toWorld.TransformPoint(v); v = toLocal.MultiplyPoint3x4(v); Bounds b = new Bounds(v, Vector3.zero); // Repeat for the other 3 corners v = new Vector3(left, bottom, 0f); v = toWorld.TransformPoint(v); v = toLocal.MultiplyPoint3x4(v); b.Encapsulate(v); v = new Vector3(right, bottom, 0f); v = toWorld.TransformPoint(v); v = toLocal.MultiplyPoint3x4(v); b.Encapsulate(v); v = new Vector3(right, top, 0f); v = toWorld.TransformPoint(v); v = toLocal.MultiplyPoint3x4(v); b.Encapsulate(v); return b; } return CalculateRelativeWidgetBounds(root, sprite.cachedTransform); }
/// <summary> /// Progress bar creation function. /// </summary> void CreateSlider(GameObject go, bool slider) { if (NGUISettings.atlas != null) { NGUIEditorTools.SpriteField("Empty", "Sprite for the background (empty bar)", NGUISettings.atlas, mSliderBG, OnSliderBG); NGUIEditorTools.SpriteField("Full", "Sprite for the foreground (full bar)", NGUISettings.atlas, mSliderFG, OnSliderFG); if (slider) { NGUIEditorTools.SpriteField("Thumb", "Sprite for the thumb indicator", NGUISettings.atlas, mSliderTB, OnSliderTB); } } if (ShouldCreate(go, NGUISettings.atlas != null)) { int depth = NGUITools.CalculateNextDepth(go); go = NGUITools.AddChild(go); go.name = slider ? "Slider" : "Progress Bar"; // Background sprite NGUIAtlas.Sprite bgs = NGUISettings.atlas.GetSprite(mSliderBG); NGUISprite back = (NGUISprite)NGUITools.AddWidget <NGUISprite>(go); back.type = (bgs.inner == bgs.outer) ? NGUISprite.Type.Simple : NGUISprite.Type.Sliced; back.name = "Background"; back.depth = depth; back.pivot = NGUIWidget.Pivot.Left; back.atlas = NGUISettings.atlas; back.spriteName = mSliderBG; back.transform.localScale = new Vector3(200f, 30f, 1f); back.transform.localPosition = Vector3.zero; back.MakePixelPerfect(); // Foreground sprite NGUIAtlas.Sprite fgs = NGUISettings.atlas.GetSprite(mSliderFG); NGUISprite front = NGUITools.AddWidget <NGUISprite>(go); front.type = (fgs.inner == fgs.outer) ? NGUISprite.Type.Filled : NGUISprite.Type.Sliced; front.name = "Foreground"; front.pivot = NGUIWidget.Pivot.Left; front.atlas = NGUISettings.atlas; front.spriteName = mSliderFG; front.transform.localScale = new Vector3(200f, 30f, 1f); front.transform.localPosition = Vector3.zero; front.MakePixelPerfect(); // Add a collider if (slider) { NGUITools.AddWidgetCollider(go); } // Add the slider script NGUISlider uiSlider = go.AddComponent <NGUISlider>(); uiSlider.foreground = front.transform; // Thumb sprite if (slider) { NGUIAtlas.Sprite tbs = NGUISettings.atlas.GetSprite(mSliderTB); NGUISprite thb = NGUITools.AddWidget <NGUISprite>(go); thb.type = (tbs.inner == tbs.outer) ? NGUISprite.Type.Simple : NGUISprite.Type.Sliced; thb.name = "Thumb"; thb.atlas = NGUISettings.atlas; thb.spriteName = mSliderTB; thb.transform.localPosition = new Vector3(200f, 0f, 0f); thb.transform.localScale = new Vector3(20f, 40f, 1f); thb.MakePixelPerfect(); NGUITools.AddWidgetCollider(thb.gameObject); thb.gameObject.AddComponent <NGUIButtonColor>(); thb.gameObject.AddComponent <NGUIButtonScale>(); uiSlider.thumb = thb.transform; } uiSlider.sliderValue = 1f; // Select the slider Selection.activeGameObject = go; } }
/// <summary> /// Initialize the cached values. /// </summary> void Init() { mInitDone = true; if (foreground != null) { mFGWidget = foreground.GetComponent<NGUIWidget>(); mFGFilled = (mFGWidget != null) ? mFGWidget as NGUISprite : null; mFGTrans = foreground.transform; if (mSize == Vector2.zero) mSize = foreground.localScale; if (mCenter == Vector2.zero) mCenter = foreground.localPosition + foreground.localScale * 0.5f; } else if (mCol != null) { if (mSize == Vector2.zero) mSize = mCol.size; if (mCenter == Vector2.zero) mCenter = mCol.center; } else { Debug.LogWarning("NGUISlider expected to find a foreground object or a box collider to work with", this); } }
/// <summary> /// Rebuild the sprite list after changing the sprite name. /// </summary> void RebuildSpriteList() { if (mSprite == null) mSprite = GetComponent<NGUISprite>(); mSpriteNames.Clear(); if (mSprite != null && mSprite.atlas != null) { List<NGUIAtlas.Sprite> sprites = mSprite.atlas.spriteList; for (int i = 0, imax = sprites.Count; i < imax; ++i) { NGUIAtlas.Sprite sprite = sprites[i]; if (string.IsNullOrEmpty(mPrefix) || sprite.name.StartsWith(mPrefix)) { mSpriteNames.Add(sprite.name); } } mSpriteNames.Sort(); } }
void Start() { if (target == null) target = GetComponentInChildren<NGUISprite>(); }
/// <summary> /// Display the drop-down list when the game object gets clicked on. /// </summary> void OnClick() { if (mChild == null && atlas != null && font != null && items.Count > 0) { mLabelList.Clear(); // Disable the navigation script handleEvents = true; // Automatically locate the panel responsible for this object if (mPanel == null) { mPanel = NGUIPanel.Find(transform, true); } // Calculate the dimensions of the object triggering the popup list so we can position it below it Transform myTrans = transform; Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(myTrans.parent, myTrans); // Create the root object for the list mChild = new GameObject("Drop-down List"); mChild.layer = gameObject.layer; Transform t = mChild.transform; t.parent = myTrans.parent; t.localPosition = bounds.min; t.localRotation = Quaternion.identity; t.localScale = Vector3.one; // Add a sprite for the background mBackground = NGUITools.AddSprite(mChild, atlas, backgroundSprite); mBackground.pivot = NGUIWidget.Pivot.TopLeft; mBackground.depth = NGUITools.CalculateNextDepth(mPanel.gameObject); mBackground.color = backgroundColor; // We need to know the size of the background sprite for padding purposes Vector4 bgPadding = mBackground.border; mBgBorder = bgPadding.y; mBackground.cachedTransform.localPosition = new Vector3(0f, bgPadding.y, 0f); // Add a sprite used for the selection mHighlight = NGUITools.AddSprite(mChild, atlas, highlightSprite); mHighlight.pivot = NGUIWidget.Pivot.TopLeft; mHighlight.color = highlightColor; NGUIAtlas.Sprite hlsp = mHighlight.GetAtlasSprite(); if (hlsp == null) { return; } float hlspHeight = hlsp.inner.yMin - hlsp.outer.yMin; float fontScale = font.size * font.pixelSize * textScale; float x = 0f, y = -padding.y; List <NGUILabel> labels = new List <NGUILabel>(); // Run through all items and create labels for each one for (int i = 0, imax = items.Count; i < imax; ++i) { string s = items[i]; NGUILabel lbl = NGUITools.AddWidget <NGUILabel>(mChild); lbl.pivot = NGUIWidget.Pivot.TopLeft; lbl.font = font; lbl.text = (isLocalized && Localization.instance != null) ? Localization.instance.Get(s) : s; lbl.color = textColor; lbl.cachedTransform.localPosition = new Vector3(bgPadding.x + padding.x, y, -0.01f); lbl.MakePixelPerfect(); if (textScale != 1f) { Vector3 scale = lbl.cachedTransform.localScale; lbl.cachedTransform.localScale = scale * textScale; } labels.Add(lbl); y -= fontScale; y -= padding.y; x = Mathf.Max(x, lbl.relativeSize.x * fontScale); // Add an event listener NGUIEventListener listener = NGUIEventListener.Get(lbl.gameObject); listener.onHover = OnItemHover; listener.onPress = OnItemPress; listener.parameter = s; // Move the selection here if this is the right label if (mSelectedItem == s) { Highlight(lbl, true); } // Add this label to the list mLabelList.Add(lbl); } // The triggering widget's width should be the minimum allowed width x = Mathf.Max(x, bounds.size.x - (bgPadding.x + padding.x) * 2f); Vector3 bcCenter = new Vector3((x * 0.5f) / fontScale, -0.5f, 0f); Vector3 bcSize = new Vector3(x / fontScale, (fontScale + padding.y) / fontScale, 1f); // Run through all labels and add colliders for (int i = 0, imax = labels.Count; i < imax; ++i) { NGUILabel lbl = labels[i]; BoxCollider bc = NGUITools.AddWidgetCollider(lbl.gameObject); bcCenter.z = bc.center.z; bc.center = bcCenter; bc.size = bcSize; } x += (bgPadding.x + padding.x) * 2f; y -= bgPadding.y; // Scale the background sprite to envelop the entire set of items mBackground.cachedTransform.localScale = new Vector3(x, -y + bgPadding.y, 1f); // Scale the highlight sprite to envelop a single item mHighlight.cachedTransform.localScale = new Vector3( x - (bgPadding.x + padding.x) * 2f + (hlsp.inner.xMin - hlsp.outer.xMin) * 2f, fontScale + hlspHeight * 2f, 1f); bool placeAbove = (position == Position.Above); if (position == Position.Auto) { NGUICamera cam = NGUICamera.FindCameraForLayer(gameObject.layer); if (cam != null) { Vector3 viewPos = cam.cachedCamera.WorldToViewportPoint(myTrans.position); placeAbove = (viewPos.y < 0.5f); } } // If the list should be animated, let's animate it by expanding it if (isAnimated) { float bottom = y + fontScale; Animate(mHighlight, placeAbove, bottom); for (int i = 0, imax = labels.Count; i < imax; ++i) { Animate(labels[i], placeAbove, bottom); } AnimateColor(mBackground); AnimateScale(mBackground, placeAbove, bottom); } // If we need to place the popup list above the item, we need to reposition everything by the size of the list if (placeAbove) { t.localPosition = new Vector3(bounds.min.x, bounds.max.y - y - bgPadding.y, bounds.min.z); } } else { OnSelect(false); } }
/// <summary> /// Convenience function. /// </summary> static public Bounds CalculateRelativeInnerBounds(Transform root, NGUISprite sprite) { if (sprite.type == NGUISprite.Type.Sliced || sprite.type == NGUISprite.Type.MirrorSliced) { Matrix4x4 toLocal = root.worldToLocalMatrix; Vector2 size = sprite.relativeSize; Vector2 offset = sprite.pivotOffset; Transform toWorld = sprite.cachedTransform; float x = (offset.x + 0.5f) * size.x; float y = (offset.y - 0.5f) * size.y; size *= 0.5f; float sx = toWorld.localScale.x; float sy = toWorld.localScale.y; // Get the border in pixels Vector4 border = sprite.border; // Convert pixels to relative coordinates if (sx != 0f) { border.x /= sx; border.z /= sx; } if (sy != 0f) { border.y /= sy; border.w /= sy; } // Calculate the relative dimensions float left = x - size.x + border.x; float right = x + size.x - border.z; float top = y - size.y + border.y; float bottom = y + size.y - border.w; // Start with the corner of the widget Vector3 v = new Vector3(left, top, 0f); v = toWorld.TransformPoint(v); v = toLocal.MultiplyPoint3x4(v); Bounds b = new Bounds(v, Vector3.zero); // Repeat for the other 3 corners v = new Vector3(left, bottom, 0f); v = toWorld.TransformPoint(v); v = toLocal.MultiplyPoint3x4(v); b.Encapsulate(v); v = new Vector3(right, bottom, 0f); v = toWorld.TransformPoint(v); v = toLocal.MultiplyPoint3x4(v); b.Encapsulate(v); v = new Vector3(right, top, 0f); v = toWorld.TransformPoint(v); v = toLocal.MultiplyPoint3x4(v); b.Encapsulate(v); return(b); } return(CalculateRelativeWidgetBounds(root, sprite.cachedTransform)); }
/// <summary> /// Display the drop-down list when the game object gets clicked on. /// </summary> void OnClick() { if (mChild == null && atlas != null && font != null && items.Count > 0) { mLabelList.Clear(); // Disable the navigation script handleEvents = true; // Automatically locate the panel responsible for this object if (mPanel == null) mPanel = NGUIPanel.Find(transform, true); // Calculate the dimensions of the object triggering the popup list so we can position it below it Transform myTrans = transform; Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(myTrans.parent, myTrans); // Create the root object for the list mChild = new GameObject("Drop-down List"); mChild.layer = gameObject.layer; Transform t = mChild.transform; t.parent = myTrans.parent; t.localPosition = bounds.min; t.localRotation = Quaternion.identity; t.localScale = Vector3.one; // Add a sprite for the background mBackground = NGUITools.AddSprite(mChild, atlas, backgroundSprite); mBackground.pivot = NGUIWidget.Pivot.TopLeft; mBackground.depth = NGUITools.CalculateNextDepth(mPanel.gameObject); mBackground.color = backgroundColor; // We need to know the size of the background sprite for padding purposes Vector4 bgPadding = mBackground.border; mBgBorder = bgPadding.y; mBackground.cachedTransform.localPosition = new Vector3(0f, bgPadding.y, 0f); // Add a sprite used for the selection mHighlight = NGUITools.AddSprite(mChild, atlas, highlightSprite); mHighlight.pivot = NGUIWidget.Pivot.TopLeft; mHighlight.color = highlightColor; NGUIAtlas.Sprite hlsp = mHighlight.GetAtlasSprite(); if (hlsp == null) return; float hlspHeight = hlsp.inner.yMin - hlsp.outer.yMin; float fontScale = font.size * font.pixelSize * textScale; float x = 0f, y = -padding.y; List<NGUILabel> labels = new List<NGUILabel>(); // Run through all items and create labels for each one for (int i = 0, imax = items.Count; i < imax; ++i) { string s = items[i]; NGUILabel lbl = NGUITools.AddWidget<NGUILabel>(mChild); lbl.pivot = NGUIWidget.Pivot.TopLeft; lbl.font = font; lbl.text = (isLocalized && Localization.instance != null) ? Localization.instance.Get(s) : s; lbl.color = textColor; lbl.cachedTransform.localPosition = new Vector3(bgPadding.x + padding.x, y, -0.01f); lbl.MakePixelPerfect(); if (textScale != 1f) { Vector3 scale = lbl.cachedTransform.localScale; lbl.cachedTransform.localScale = scale * textScale; } labels.Add(lbl); y -= fontScale; y -= padding.y; x = Mathf.Max(x, lbl.relativeSize.x * fontScale); // Add an event listener NGUIEventListener listener = NGUIEventListener.Get(lbl.gameObject); listener.onHover = OnItemHover; listener.onPress = OnItemPress; listener.parameter = s; // Move the selection here if this is the right label if (mSelectedItem == s) Highlight(lbl, true); // Add this label to the list mLabelList.Add(lbl); } // The triggering widget's width should be the minimum allowed width x = Mathf.Max(x, bounds.size.x - (bgPadding.x + padding.x) * 2f); Vector3 bcCenter = new Vector3((x * 0.5f) / fontScale, -0.5f, 0f); Vector3 bcSize = new Vector3(x / fontScale, (fontScale + padding.y) / fontScale, 1f); // Run through all labels and add colliders for (int i = 0, imax = labels.Count; i < imax; ++i) { NGUILabel lbl = labels[i]; BoxCollider bc = NGUITools.AddWidgetCollider(lbl.gameObject); bcCenter.z = bc.center.z; bc.center = bcCenter; bc.size = bcSize; } x += (bgPadding.x + padding.x) * 2f; y -= bgPadding.y; // Scale the background sprite to envelop the entire set of items mBackground.cachedTransform.localScale = new Vector3(x, -y + bgPadding.y, 1f); // Scale the highlight sprite to envelop a single item mHighlight.cachedTransform.localScale = new Vector3( x - (bgPadding.x + padding.x) * 2f + (hlsp.inner.xMin - hlsp.outer.xMin) * 2f, fontScale + hlspHeight * 2f, 1f); bool placeAbove = (position == Position.Above); if (position == Position.Auto) { NGUICamera cam = NGUICamera.FindCameraForLayer(gameObject.layer); if (cam != null) { Vector3 viewPos = cam.cachedCamera.WorldToViewportPoint(myTrans.position); placeAbove = (viewPos.y < 0.5f); } } // If the list should be animated, let's animate it by expanding it if (isAnimated) { float bottom = y + fontScale; Animate(mHighlight, placeAbove, bottom); for (int i = 0, imax = labels.Count; i < imax; ++i) Animate(labels[i], placeAbove, bottom); AnimateColor(mBackground); AnimateScale(mBackground, placeAbove, bottom); } // If we need to place the popup list above the item, we need to reposition everything by the size of the list if (placeAbove) { t.localPosition = new Vector3(bounds.min.x, bounds.max.y - y - bgPadding.y, bounds.min.z); } } else OnSelect(false); }
public void createSymblos() { if (Application.isPlaying) { if (mSymblosVerts.Count <= 0) { return; } if (label == null) { return; } if (label.font.dynamicSymbolsFont == null) { return; } //float fontSize = label.font.size; float fontSize = label.transform.localScale.x; float relateScale = label.transform.localScale.x / label.font.size; float lineFillLengthPixel = label.LineWidthBase * fontSize; float xLeftSide = 0; float yTopSide = 0; //note:this is not "Top". center Y of Top Line... //count the left of label's Fill rect x. if (label.pivot == NGUIWidget.Pivot.Left || label.pivot == NGUIWidget.Pivot.TopLeft || label.pivot == NGUIWidget.Pivot.BottomLeft) { xLeftSide = label.transform.localPosition.x; } else if (label.pivot == NGUIWidget.Pivot.Right || label.pivot == NGUIWidget.Pivot.TopRight || label.pivot == NGUIWidget.Pivot.BottomRight) { //chiuan fixe When label has maxLength,and pivot == TopRight... float tLength = label.lineWidth > 0 ? label.lineWidth : lineFillLengthPixel; xLeftSide = label.transform.localPosition.x - tLength; //Debug.Log(label.transform.localPosition.x + " - " + tLength + " = " + xLeftSide); } else { float tLength = label.lineWidth > 0 ? label.lineWidth : lineFillLengthPixel; xLeftSide = label.transform.localPosition.x - tLength / 2f; } //start count yTopSide if (label.pivot == NGUIWidget.Pivot.TopLeft || label.pivot == NGUIWidget.Pivot.TopRight || label.pivot == NGUIWidget.Pivot.Top) { yTopSide = label.transform.localPosition.y - fontSize * 0.5f; } else if (label.pivot == NGUIWidget.Pivot.BottomLeft || label.pivot == NGUIWidget.Pivot.BottomRight || label.pivot == NGUIWidget.Pivot.Bottom) { yTopSide = label.transform.localPosition.y + label.relativeSize.y * fontSize - fontSize * 0.5f; } else { yTopSide = label.transform.localPosition.y + label.relativeSize.y * 0.5f * fontSize - fontSize * 0.5f; } //Debug.Log("xLeftSide = " + xLeftSide); //Debug.Log("yTopSide = " + yTopSide); //Debug.LogError("lfp: " + lineFillLengthPixel); //Debug.LogError("label x: " + label.transform.localPosition.x); float lineSeed = 0; if (label.lineWidth > 0 && label.pivot != NGUIWidget.Pivot.Right && label.pivot != NGUIWidget.Pivot.TopRight && label.pivot != NGUIWidget.Pivot.BottomRight) { lineSeed = label.lineWidth * 1.0f / fontSize; //Debug.LogWarning("line seed = " + lineSeed); } for (int i = 0; i < mSymblosVerts.Count; i++) { GameObject go = new GameObject("emoji" + i); go.transform.parent = label.transform.parent; go.layer = label.gameObject.layer; NGUISprite emoji = NGUITools.AddSprite(go, label.font.dynamicSymbolsFont.atlas, mSymblosNames[i]); //Debug.LogError("Adding emoji sprite @" + XUIActiveBase.logObjName(go) + " label text: " + label.text); go.transform.localScale = Vector3.one; //why is new parent? emoji.pivot = NGUIWidget.Pivot.Left; emoji.MakePixelPerfect(); emoji.transform.localPosition = Vector3.zero; //reset to zero of parent. float y = emoji.transform.localScale.y / emoji.transform.localScale.x; emoji.transform.localScale = new Vector3(fontSize, fontSize * y, 1f); // go.transform.localPosition = new Vector3( xLeftSide + fontSize * mSymblosVerts[i].x, yTopSide + (fontSize * mSymblosVerts[i].y + offsetOfY - 6.104168f - 2f), label.transform.localPosition.z - 1f); sprites.Add(go); } } }
/// <summary> /// Draw the atlas and sprite selection fields. /// </summary> protected override bool DrawProperties() { mSprite = mWidget as NGUISprite; ComponentSelector.Draw<NGUIAtlas>(mSprite.atlas, OnSelectAtlas); if (mSprite.atlas == null) return false; NGUIEditorTools.AdvancedSpriteField(mSprite.atlas, mSprite.spriteName, SelectSprite, false); return true; }
/// <summary> /// Mark all widgets associated with this atlas as having changed. /// </summary> public void MarkAsDirty() { #if UNITY_EDITOR UnityEditor.EditorUtility.SetDirty(gameObject); #endif if (mReplacement != null) { mReplacement.MarkAsDirty(); } NGUISprite[] list = NGUITools.FindActive <NGUISprite>(); for (int i = 0, imax = list.Length; i < imax; ++i) { NGUISprite sp = list[i]; if (CheckIfRelated(this, sp.atlas)) { NGUIAtlas atl = sp.atlas; sp.atlas = null; sp.atlas = atl; #if UNITY_EDITOR UnityEditor.EditorUtility.SetDirty(sp); #endif } } NGUIFont[] fonts = Resources.FindObjectsOfTypeAll(typeof(NGUIFont)) as NGUIFont[]; for (int i = 0, imax = fonts.Length; i < imax; ++i) { NGUIFont font = fonts[i]; if (CheckIfRelated(this, font.atlas)) { NGUIAtlas atl = font.atlas; font.atlas = null; font.atlas = atl; #if UNITY_EDITOR UnityEditor.EditorUtility.SetDirty(font); #endif } } NGUILabel[] labels = NGUITools.FindActive <NGUILabel>(); for (int i = 0, imax = labels.Length; i < imax; ++i) { NGUILabel lbl = labels[i]; if (lbl.font != null && CheckIfRelated(this, lbl.font.atlas)) { NGUIFont font = lbl.font; lbl.font = null; lbl.font = font; #if UNITY_EDITOR UnityEditor.EditorUtility.SetDirty(lbl); #endif } } }