/// <summary> /// We want to receive drag events from the thumb. /// </summary> void Start() { if (foreground != null) { mWidget = foreground.GetComponent <UIWidget>(); mSprite = (mWidget != null) ? mWidget as UIFilledSprite : null; mForeTrans = foreground.transform; if (fullSize == Vector2.zero) { fullSize = foreground.localScale; } } else if (mCol != null) { if (fullSize == Vector2.zero) { fullSize = mCol.size; } } else { Debug.LogWarning("UISlider expected to find a foreground object or a box collider to work with", this); } if (Application.isPlaying && thumb != null && thumb.collider != null) { UIEventListener listener = UIEventListener.Add(thumb.gameObject); listener.onPress += OnPressThumb; listener.onDrag += OnDragThumb; } Set(rawValue); }
/// <summary> /// We want to receive drag events from the thumb. /// </summary> /// /// void Start() { if (Application.isPlaying && thumb != null && thumb.GetComponent <Collider>() != null) { UIEventListener listener = UIEventListener.Add(thumb.gameObject); listener.onPress += OnPressThumb; listener.onDrag += OnDragThumb; } Set(rawValue); }
/// <summary> /// Load and set the state of the checkboxes. /// </summary> void OnEnable() { string s = PlayerPrefs.GetString("NGUI State: " + name); if (!string.IsNullOrEmpty(s)) { UICheckbox[] checkboxes = GetComponentsInChildren <UICheckbox>(); foreach (UICheckbox ch in checkboxes) { UIEventListener.Add(ch.gameObject).onClick -= Save; ch.isChecked = (ch.name == s); UIEventListener.Add(ch.gameObject).onClick += Save; } } }
/// <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 > 1) { mLabelList.Clear(); // Disable the navigation script handleEvents = true; // Automatically locate the panel responsible for this object if (mPanel == null) { mPanel = UIPanel.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 UISprite background = NGUITools.AddSprite(mChild, atlas, backgroundSprite); background.pivot = UIWidget.Pivot.TopLeft; background.depth = NGUITools.CalculateNextDepth(mPanel.gameObject); background.color = backgroundColor; // Add a sprite used for the selection mHighlight = NGUITools.AddSprite(mChild, atlas, highlightSprite); mHighlight.pivot = UIWidget.Pivot.TopLeft; mHighlight.color = highlightColor; float fontScale = textLabel.FontSize * textScale; float x = 0f, y = -padding.y; List <UILabel> labels = new List <UILabel>(); // Run through all items and create labels for each one foreach (string s in items) { UILabel lbl = NGUITools.AddWidget <UILabel>(mChild); lbl.pivot = UIWidget.Pivot.TopLeft; lbl.font = font; lbl.text = (isLocalized && Localization.instance != null) ? Localization.instance.Get(s) : s; lbl.color = textColor; lbl.cachedTransform.localPosition = new Vector3(padding.x, y, 0f); lbl.MakePixelPerfect(); if (textScale != 1f) { Vector3 scale = lbl.cachedTransform.localScale; lbl.cachedTransform.localScale = scale * textScale; } labels.Add(lbl); y -= fontScale; x = Mathf.Max(x, lbl.relativeSize.x * fontScale); // Add an event listener UIEventListener listener = UIEventListener.Add(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 - padding.x * 2f); // Run through all labels and add colliders foreach (UILabel lbl in labels) { BoxCollider bc = NGUITools.AddWidgetCollider(lbl.gameObject); bc.center = new Vector3((x * 0.5f) / fontScale, -0.5f, bc.center.z); bc.size = new Vector3(x / fontScale, 1f, 1f); } x += padding.x * 2f; y -= padding.y; // Scale the background sprite to envelop the entire set of items background.cachedTransform.localScale = new Vector3(x, -y, 1f); // Scale the highlight sprite to envelop a single item mHighlight.cachedTransform.localScale = new Vector3(x, fontScale + padding.y * 2f, 1f); bool placeAbove = (position == Position.Above); if (position == Position.Auto) { UICamera cam = UICamera.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); foreach (UILabel lbl in labels) { Animate(lbl, placeAbove, bottom); } AnimateColor(background); AnimateScale(background, 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, bounds.min.z); } } else { OnSelect(false); } }