/// <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; } }
void Awake() { mSlider = GetComponent <NGUISlider>(); mSlider.sliderValue = NGUITools.soundVolume; mSlider.eventReceiver = gameObject; }
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; } } } }
void Start() { mSlider = GetComponent <NGUISlider>(); Update(); }
/// <summary> /// Update the visible slider. /// </summary> void Set(float input, bool force) { if (!mInitDone) Init(); // Clamp the input float val = Mathf.Clamp01(input); if (val < 0.001f) val = 0f; float prevStep = sliderValue; // Save the raw value rawValue = val; // Take steps into account float stepValue = sliderValue; // If the stepped value doesn't match the last one, it's time to update if (force || prevStep != stepValue) { Vector3 scale = mSize; #if UNITY_EDITOR if (Application.isPlaying) { if (direction == Direction.Horizontal) scale.x *= stepValue; else scale.y *= stepValue; } #else if (direction == Direction.Horizontal) scale.x *= stepValue; else scale.y *= stepValue; #endif #if UNITY_EDITOR if (Application.isPlaying) #endif { if (mFGFilled != null && mFGFilled.type == NGUISprite.Type.Filled) { mFGFilled.fillAmount = stepValue; } else if (foreground != null) { mFGTrans.localScale = scale; if (mFGWidget != null) { if (stepValue > 0.001f) { mFGWidget.enabled = true; mFGWidget.MarkAsChanged(); } else { mFGWidget.enabled = false; } } } } if (thumb != null) { Vector3 pos = thumb.localPosition; if (mFGFilled != null && mFGFilled.type == NGUISprite.Type.Filled) { if (mFGFilled.fillDirection == NGUISprite.FillDirection.Horizontal) { pos.x = mFGFilled.invert ? mSize.x - scale.x : scale.x; } else if (mFGFilled.fillDirection == NGUISprite.FillDirection.Vertical) { pos.y = mFGFilled.invert ? mSize.y - scale.y : scale.y; } else { Debug.LogWarning("Slider thumb is only supported with Horizontal or Vertical fill direction", this); } } else if (direction == Direction.Horizontal) { pos.x = scale.x; } else { pos.y = scale.y; } thumb.localPosition = pos; } current = this; if (eventReceiver != null && !string.IsNullOrEmpty(functionName) && Application.isPlaying) { eventReceiver.SendMessage(functionName, stepValue, SendMessageOptions.DontRequireReceiver); } if (onValueChange != null) onValueChange(stepValue); current = null; } }
void Awake() { mSlider = GetComponent<NGUISlider>(); mSlider.sliderValue = NGUITools.soundVolume; mSlider.eventReceiver = gameObject; }
public override void OnInspectorGUI() { EditorGUIUtility.LookLikeControls(80f); NGUISlider slider = target as NGUISlider; NGUIEditorTools.DrawSeparator(); float sliderValue = EditorGUILayout.Slider("Value", slider.sliderValue, 0f, 1f); if (slider.sliderValue != sliderValue) { NGUIEditorTools.RegisterUndo("Slider Change", slider); slider.sliderValue = sliderValue; UnityEditor.EditorUtility.SetDirty(slider); } int steps = EditorGUILayout.IntSlider("Steps", slider.numberOfSteps, 0, 11); if (slider.numberOfSteps != steps) { NGUIEditorTools.RegisterUndo("Slider Change", slider); slider.numberOfSteps = steps; slider.ForceUpdate(); UnityEditor.EditorUtility.SetDirty(slider); } NGUIEditorTools.DrawSeparator(); Transform fg = EditorGUILayout.ObjectField("Foreground", slider.foreground, typeof(Transform), true) as Transform; Transform tb = EditorGUILayout.ObjectField("Thumb", slider.thumb, typeof(Transform), true) as Transform; NGUISlider.Direction dir = (NGUISlider.Direction)EditorGUILayout.EnumPopup("Direction", slider.direction); // If we're using a sprite for the foreground, ensure it's using a proper pivot. ValidatePivot(fg, "Foreground sprite", dir); NGUIEditorTools.DrawSeparator(); GameObject er = EditorGUILayout.ObjectField("Event Recv.", slider.eventReceiver, typeof(GameObject), true) as GameObject; GUILayout.BeginHorizontal(); string fn = EditorGUILayout.TextField("Function", slider.functionName); GUILayout.Space(18f); GUILayout.EndHorizontal(); if (slider.foreground != fg || slider.thumb != tb || slider.direction != dir || slider.eventReceiver != er || slider.functionName != fn) { NGUIEditorTools.RegisterUndo("Slider Change", slider); slider.foreground = fg; slider.thumb = tb; slider.direction = dir; slider.eventReceiver = er; slider.functionName = fn; if (slider.thumb != null) { slider.thumb.localPosition = Vector3.zero; slider.sliderValue = -1f; slider.sliderValue = sliderValue; } else { slider.ForceUpdate(); } UnityEditor.EditorUtility.SetDirty(slider); } }
/// <summary> /// Update the visible slider. /// </summary> void Set(float input, bool force) { if (!mInitDone) { Init(); } // Clamp the input float val = Mathf.Clamp01(input); if (val < 0.001f) { val = 0f; } float prevStep = sliderValue; // Save the raw value rawValue = val; // Take steps into account float stepValue = sliderValue; // If the stepped value doesn't match the last one, it's time to update if (force || prevStep != stepValue) { Vector3 scale = mSize; #if UNITY_EDITOR if (Application.isPlaying) { if (direction == Direction.Horizontal) { scale.x *= stepValue; } else { scale.y *= stepValue; } } #else if (direction == Direction.Horizontal) { scale.x *= stepValue; } else { scale.y *= stepValue; } #endif #if UNITY_EDITOR if (Application.isPlaying) #endif { if (mFGFilled != null && mFGFilled.type == NGUISprite.Type.Filled) { mFGFilled.fillAmount = stepValue; } else if (foreground != null) { mFGTrans.localScale = scale; if (mFGWidget != null) { if (stepValue > 0.001f) { mFGWidget.enabled = true; mFGWidget.MarkAsChanged(); } else { mFGWidget.enabled = false; } } } } if (thumb != null) { Vector3 pos = thumb.localPosition; if (mFGFilled != null && mFGFilled.type == NGUISprite.Type.Filled) { if (mFGFilled.fillDirection == NGUISprite.FillDirection.Horizontal) { pos.x = mFGFilled.invert ? mSize.x - scale.x : scale.x; } else if (mFGFilled.fillDirection == NGUISprite.FillDirection.Vertical) { pos.y = mFGFilled.invert ? mSize.y - scale.y : scale.y; } else { Debug.LogWarning("Slider thumb is only supported with Horizontal or Vertical fill direction", this); } } else if (direction == Direction.Horizontal) { pos.x = scale.x; } else { pos.y = scale.y; } thumb.localPosition = pos; } current = this; if (eventReceiver != null && !string.IsNullOrEmpty(functionName) && Application.isPlaying) { eventReceiver.SendMessage(functionName, stepValue, SendMessageOptions.DontRequireReceiver); } if (onValueChange != null) { onValueChange(stepValue); } current = null; } }