public void Reposition() { if (!this.mStarted) { this.repositionNow = true; return; } Transform transform = base.transform; int num = 0; int num2 = 0; if (this.sorted) { List <Transform> list = new List <Transform>(); for (int i = 0; i < transform.childCount; i++) { Transform child = transform.GetChild(i); if (child && (!this.hideInactive || NGUITools.GetActive(child.gameObject))) { list.Add(child); } } list.Sort(new Comparison <Transform>(UIGrid.SortByName)); int j = 0; int count = list.Count; while (j < count) { Transform transform2 = list[j]; if (NGUITools.GetActive(transform2.gameObject) || !this.hideInactive) { float z = transform2.localPosition.z; transform2.localPosition = ((this.arrangement != UIGrid.Arrangement.Horizontal) ? new Vector3(this.cellWidth * (float)num2, -this.cellHeight * (float)num, z) : new Vector3(this.cellWidth * (float)num, -this.cellHeight * (float)num2, z)); if (++num >= this.maxPerLine && this.maxPerLine > 0) { num = 0; num2++; } } j++; } } else { for (int k = 0; k < transform.childCount; k++) { Transform child2 = transform.GetChild(k); if (NGUITools.GetActive(child2.gameObject) || !this.hideInactive) { float z2 = child2.localPosition.z; child2.localPosition = ((this.arrangement != UIGrid.Arrangement.Horizontal) ? new Vector3(this.cellWidth * (float)num2, -this.cellHeight * (float)num, z2) : new Vector3(this.cellWidth * (float)num, -this.cellHeight * (float)num2, z2)); if (++num >= this.maxPerLine && this.maxPerLine > 0) { num = 0; num2++; } } } } UIDraggablePanel uidraggablePanel = NGUITools.FindInParents <UIDraggablePanel>(base.gameObject); if (uidraggablePanel != null) { uidraggablePanel.UpdateScrollbars(true); } }
/// <summary> /// Add the specified widget to the managed list. /// </summary> public void AddWidget(UIWidget w) { if (w != null) { #if UNITY_EDITOR if (w.cachedTransform.parent != null) { UIWidget parentWidget = NGUITools.FindInParents <UIWidget>(w.cachedTransform.parent.gameObject); if (parentWidget != null) { w.cachedTransform.parent = parentWidget.cachedTransform.parent; Debug.LogError("You should never nest widgets! Parent them to a common game object instead. Forcefully changing the parent.", w); // If the error above gets triggered, it means that you parented one widget to another. // If left unchecked, this may lead to odd behavior in the UI. Consider restructuring your UI. // For example, if you were trying to do this: // Widget #1 // | // +- Widget #2 // You can do this instead, fixing the problem: // GameObject (scale 1, 1, 1) // | // +- Widget #1 // | // +- Widget #2 } } #endif #if OLD_UNITY UINode node = AddTransform(w.cachedTransform); if (node != null) { node.widget = w; w.visibleFlag = 1; if (!mWidgets.Contains(w)) { mWidgets.Add(w); if (!mChanged.Contains(w.material)) { mChanged.Add(w.material); } mDepthChanged = true; mWidgetsAdded = true; } } else { Debug.LogError("Unable to find an appropriate root for " + NGUITools.GetHierarchy(w.cachedGameObject) + "\nPlease make sure that there is at least one game object above this widget!", w.cachedGameObject); } #else if (!mWidgets.Contains(w)) { mWidgets.Add(w); if (!mChanged.Contains(w.material)) { mChanged.Add(w.material); } mDepthChanged = true; } #endif } }
/// <summary> /// Perform any logic related to starting the drag & drop operation. /// </summary> protected virtual void OnDragDropStart() { // Automatically disable the scroll view if (mDragScrollView != null) { mDragScrollView.enabled = false; } // Disable the collider so that it doesn't intercept events if (mButton != null) { mButton.isEnabled = false; } else if (mCollider != null) { mCollider.enabled = false; } else if (mCollider2D != null) { mCollider2D.enabled = false; } mTouchID = UICamera.currentTouchID; mParent = mTrans.parent; mRoot = NGUITools.FindInParents <UIRoot>(mParent); mGrid = NGUITools.FindInParents <UIGrid>(mParent); mTable = NGUITools.FindInParents <UITable>(mParent); // Re-parent the item if (UIDragDropRoot.root != null) { mTrans.parent = UIDragDropRoot.root; } Vector3 pos = mTrans.localPosition; pos.z = 0f; mTrans.localPosition = pos; TweenPosition tp = GetComponent <TweenPosition>(); if (tp != null) { tp.enabled = false; } SpringPosition sp = GetComponent <SpringPosition>(); if (sp != null) { sp.enabled = false; } // Notify the widgets that the parent has changed NGUITools.MarkParentAsChanged(gameObject); if (mTable != null) { mTable.repositionNow = true; } if (mGrid != null) { mGrid.repositionNow = true; } }
/// <summary> /// Recalculate the position of all elements within the grid, sorting them alphabetically if necessary. /// </summary> public void Reposition() { if (!mStarted) { repositionNow = true; return; } Transform myTrans = transform; int x = 0; int y = 0; if (sorted) { List <Transform> list = new List <Transform>(); for (int i = 0; i < myTrans.childCount; ++i) { Transform t = myTrans.GetChild(i); if (t && (!hideInactive || NGUITools.GetActive(t.gameObject))) { list.Add(t); } } list.Sort(SortByName); for (int i = 0, imax = list.Count; i < imax; ++i) { Transform t = list[i]; if (!NGUITools.GetActive(t.gameObject) && hideInactive) { continue; } float depth = t.localPosition.z; t.localPosition = (arrangement == Arrangement.Horizontal) ? new Vector3(cellWidth * x, -cellHeight * y, depth) : new Vector3(cellWidth * y, -cellHeight * x, depth); if (++x >= maxPerLine && maxPerLine > 0) { x = 0; ++y; } } } else { for (int i = 0; i < myTrans.childCount; ++i) { Transform t = myTrans.GetChild(i); if (!NGUITools.GetActive(t.gameObject) && hideInactive) { continue; } float depth = t.localPosition.z; t.localPosition = (arrangement == Arrangement.Horizontal) ? new Vector3(cellWidth * x, -cellHeight * y, depth) : new Vector3(cellWidth * y, -cellHeight * x, depth); if (++x >= maxPerLine && maxPerLine > 0) { x = 0; ++y; } } } UIDraggablePanel drag = NGUITools.FindInParents <UIDraggablePanel>(gameObject); if (drag != null) { drag.UpdateScrollbars(true); } }
/// <summary> /// Helper function that figures out the pixel size adjustment for the specified game object. /// </summary> static public float GetPixelSizeAdjustment(GameObject go) { UIRoot root = NGUITools.FindInParents <UIRoot>(go); return((root != null) ? root.pixelSizeAdjustment : 1f); }
/// <summary> /// Draw the custom wizard. /// </summary> void OnGUI() { List <UIPanel> panels = GetListOfPanels(); if (panels != null && panels.Count > 0) { UIPanel selectedPanel = NGUITools.FindInParents <UIPanel>(Selection.activeGameObject); // First, collect a list of panels with their associated widgets List <Entry> entries = new List <Entry>(); Entry selectedEntry = null; bool allEnabled = true; foreach (UIPanel panel in panels) { Entry ent = new Entry(); ent.panel = panel; ent.widgets = GetWidgets(panel); ent.isEnabled = panel.enabled && NGUITools.GetActive(panel.gameObject); ent.widgetsEnabled = ent.isEnabled; if (ent.widgetsEnabled) { foreach (UIWidget w in ent.widgets) { if (!NGUITools.GetActive(w.gameObject)) { allEnabled = false; ent.widgetsEnabled = false; break; } } } else { allEnabled = false; } entries.Add(ent); } // Sort the list by depth entries.Sort(Compare); mScroll = GUILayout.BeginScrollView(mScroll); NGUIEditorTools.SetLabelWidth(80f); bool showAll = DrawRow(null, null, allEnabled); NGUIEditorTools.DrawSeparator(); foreach (Entry ent in entries) { if (DrawRow(ent, selectedPanel, ent.widgetsEnabled)) { selectedEntry = ent; } } GUILayout.EndScrollView(); if (showAll) { foreach (Entry ent in entries) { NGUITools.SetActive(ent.panel.gameObject, !allEnabled); } } else if (selectedEntry != null) { NGUITools.SetActive(selectedEntry.panel.gameObject, !selectedEntry.widgetsEnabled); } } else { GUILayout.Label("No UI Panels found in the scene"); } }
/// <summary> /// Recenter the draggable list on the center-most child. /// </summary> public void Recenter() { if (mScrollView == null) { mScrollView = NGUITools.FindInParents <UIScrollView>(gameObject); if (mScrollView == null) { Debug.LogWarning(GetType() + " requires " + typeof(UIScrollView) + " on a parent object in order to work", this); enabled = false; return; } else { mScrollView.onDragFinished += OnDragFinished; if (mScrollView.horizontalScrollBar != null) { mScrollView.horizontalScrollBar.onDragFinished = OnDragFinished; } if (mScrollView.verticalScrollBar != null) { mScrollView.verticalScrollBar.onDragFinished = OnDragFinished; } } } if (mScrollView.panel == null) { return; } // Calculate the panel's center in world coordinates Vector3[] corners = mScrollView.panel.worldCorners; Vector3 panelCenter = (corners[2] + corners[0]) * 0.5f; // Offset this value by the momentum Vector3 pickingPoint = panelCenter - mScrollView.currentMomentum * (mScrollView.momentumAmount * 0.1f); mScrollView.currentMomentum = Vector3.zero; float min = float.MaxValue; Transform closest = null; Transform trans = transform; int index = 0; // Determine the closest child for (int i = 0, imax = trans.childCount; i < imax; ++i) { Transform t = trans.GetChild(i); float sqrDist = Vector3.SqrMagnitude(t.position - pickingPoint); if (sqrDist < min) { min = sqrDist; closest = t; index = i; } } // If we have a touch in progress and the next page threshold set if (nextPageThreshold > 0f && UICamera.currentTouch != null) { // If we're still on the same object if (mCenteredObject != null && mCenteredObject.transform == trans.GetChild(index)) { Vector2 totalDelta = UICamera.currentTouch.totalDelta; float delta = 0f; switch (mScrollView.movement) { case UIScrollView.Movement.Horizontal: { delta = totalDelta.x; break; } case UIScrollView.Movement.Vertical: { delta = totalDelta.y; break; } default: { delta = totalDelta.magnitude; break; } } if (delta > nextPageThreshold) { // Next page if (index > 0) { closest = trans.GetChild(index - 1); } } else if (delta < -nextPageThreshold) { // Previous page if (index < trans.childCount - 1) { closest = trans.GetChild(index + 1); } } } } CenterOn(closest, panelCenter); }
/// <summary> /// Cache the transform. /// </summary> void Start () { mTrans = transform; if (updateScrollView) mSv = NGUITools.FindInParents<UIScrollView>(gameObject); }
/// <summary> /// Draw the custom wizard. /// </summary> void OnGUI() { BetterList <UIDrawCall> dcs = UIDrawCall.activeList; dcs.Sort(delegate(UIDrawCall a, UIDrawCall b) { return(a.finalRenderQueue.CompareTo(b.finalRenderQueue)); }); if (dcs.size == 0) { EditorGUILayout.HelpBox("No NGUI draw calls present in the scene", MessageType.Info); return; } UIPanel selectedPanel = NGUITools.FindInParents <UIPanel>(Selection.activeGameObject); GUILayout.Space(12f); NGUIEditorTools.SetLabelWidth(100f); ShowFilter show = (NGUISettings.showAllDCs ? ShowFilter.AllPanels : ShowFilter.SelectedPanel); if ((ShowFilter)EditorGUILayout.EnumPopup("Draw Call Filter", show) != show) { NGUISettings.showAllDCs = !NGUISettings.showAllDCs; } GUILayout.Space(6f); if (selectedPanel == null && !NGUISettings.showAllDCs) { EditorGUILayout.HelpBox("No panel selected", MessageType.Info); return; } NGUIEditorTools.SetLabelWidth(80f); mScroll = GUILayout.BeginScrollView(mScroll); int dcCount = 0; for (int i = 0; i < dcs.size; ++i) { UIDrawCall dc = dcs.buffer[i]; string key = "Draw Call " + (i + 1); bool highlight = (selectedPanel == null || selectedPanel == dc.manager); if (!highlight) { if (!NGUISettings.showAllDCs) { continue; } if (UnityEditor.EditorPrefs.GetBool(key, true)) { GUI.color = new Color(0.85f, 0.85f, 0.85f); } else { GUI.contentColor = new Color(0.85f, 0.85f, 0.85f); } } else { GUI.contentColor = Color.white; } ++dcCount; string name = key + " of " + dcs.size; if (!dc.isActive) { name = name + " (HIDDEN)"; } else if (!highlight) { name = name + " (" + dc.manager.name + ")"; } if (NGUIEditorTools.DrawHeader(name, key)) { GUI.color = highlight ? Color.white : new Color(0.8f, 0.8f, 0.8f); NGUIEditorTools.BeginContents(); EditorGUILayout.ObjectField("Material", dc.dynamicMaterial, typeof(Material), false); int count = 0; for (int a = 0; a < UIPanel.list.Count; ++a) { UIPanel p = UIPanel.list[a]; for (int b = 0; b < p.widgets.Count; ++b) { UIWidget w = p.widgets[b]; if (w.drawCall == dc) { ++count; } } } string myPath = NGUITools.GetHierarchy(dc.manager.cachedGameObject); string remove = myPath + "\\"; string[] list = new string[count + 1]; list[0] = count.ToString(); count = 0; for (int a = 0; a < UIPanel.list.Count; ++a) { UIPanel p = UIPanel.list[a]; for (int b = 0; b < p.widgets.Count; ++b) { UIWidget w = p.widgets[b]; if (w.drawCall == dc) { string path = NGUITools.GetHierarchy(w.cachedGameObject); list[++count] = count + ". " + (string.Equals(path, myPath) ? w.name : path.Replace(remove, "")); } } } GUILayout.BeginHorizontal(); int sel = EditorGUILayout.Popup("Widgets", 0, list); NGUIEditorTools.DrawPadding(); GUILayout.EndHorizontal(); if (sel != 0) { count = 0; for (int a = 0; a < UIPanel.list.Count; ++a) { UIPanel p = UIPanel.list[a]; for (int b = 0; b < p.widgets.Count; ++b) { UIWidget w = p.widgets[b]; if (w.drawCall == dc && ++count == sel) { Selection.activeGameObject = w.gameObject; break; } } } } GUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Render Q", dc.finalRenderQueue.ToString(), GUILayout.Width(120f)); bool draw = (Visibility)EditorGUILayout.EnumPopup(dc.isActive ? Visibility.Visible : Visibility.Hidden) == Visibility.Visible; NGUIEditorTools.DrawPadding(); GUILayout.EndHorizontal(); if (dc.isActive != draw) { dc.isActive = draw; NGUITools.SetDirty(dc.manager); } GUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Triangles", dc.triangles.ToString(), GUILayout.Width(120f)); if (dc.manager != selectedPanel) { if (GUILayout.Button("Select the Panel")) { Selection.activeGameObject = dc.manager.gameObject; } NGUIEditorTools.DrawPadding(); } GUILayout.EndHorizontal(); if (dc.manager.clipping != UIDrawCall.Clipping.None && !dc.isClipped) { EditorGUILayout.HelpBox("You must switch this material's shader to Unlit/Transparent Colored or Unlit/Premultiplied Colored in order for clipping to work.", MessageType.Warning); } NGUIEditorTools.EndContents(); GUI.color = Color.white; } } if (dcCount == 0) { EditorGUILayout.HelpBox("No draw calls found", MessageType.Info); } GUILayout.EndScrollView(); }
/// <summary> /// Draw the custom wizard. /// </summary> void OnGUI() { List <UIPanel> panels = GetListOfPanels(); if (panels != null && panels.Count > 0) { UIPanel selectedPanel = NGUITools.FindInParents <UIPanel>(Selection.activeGameObject); // First, collect a list of panels with their associated widgets List <Entry> entries = new List <Entry>(); Entry selectedEntry = null; bool allEnabled = true; { var __list1 = panels; var __listCount1 = __list1.Count; for (int __i1 = 0; __i1 < __listCount1; ++__i1) { var panel = (UIPanel)__list1[__i1]; { Entry ent = new Entry(); ent.panel = panel; ent.widgets = GetWidgets(panel); ent.isEnabled = panel.enabled && NGUITools.GetActive(panel.gameObject); ent.widgetsEnabled = ent.isEnabled; if (ent.widgetsEnabled) { { var __list5 = ent.widgets; var __listCount5 = __list5.Count; for (int __i5 = 0; __i5 < __listCount5; ++__i5) { var w = (UIWidget)__list5[__i5]; { if (!NGUITools.GetActive(w.gameObject)) { allEnabled = false; ent.widgetsEnabled = false; break; } } } } } else { allEnabled = false; } entries.Add(ent); } } } // Sort the list by depth entries.Sort(Compare); mScroll = GUILayout.BeginScrollView(mScroll); NGUIEditorTools.SetLabelWidth(80f); bool showAll = DrawRow(null, null, allEnabled); NGUIEditorTools.DrawSeparator(); { var __list2 = entries; var __listCount2 = __list2.Count; for (int __i2 = 0; __i2 < __listCount2; ++__i2) { var ent = (Entry)__list2[__i2]; { if (DrawRow(ent, selectedPanel, ent.widgetsEnabled)) { selectedEntry = ent; } } } } GUILayout.EndScrollView(); if (showAll) { { var __list3 = entries; var __listCount3 = __list3.Count; for (int __i3 = 0; __i3 < __listCount3; ++__i3) { var ent = (Entry)__list3[__i3]; { NGUITools.SetActive(ent.panel.gameObject, !allEnabled); } } } } else if (selectedEntry != null) { NGUITools.SetActive(selectedEntry.panel.gameObject, !selectedEntry.widgetsEnabled); } } else { GUILayout.Label("No UI Panels found in the scene"); } }
public override void OnInspectorGUI() { GUILayout.Space(6f); NGUIEditorTools.SetLabelWidth(90f); string fieldName = "Item Size"; string error = null; UIScrollView sv = null; if (!serializedObject.isEditingMultipleObjects) { UIWrapContent list = target as UIWrapContent; sv = NGUITools.FindInParents <UIScrollView>(list.gameObject); if (sv == null) { error = "UIWrappedList needs a Scroll View on its parent in order to work properly"; } else if (sv.movement == UIScrollView.Movement.Horizontal) { fieldName = "Item Width"; } else if (sv.movement == UIScrollView.Movement.Vertical) { fieldName = "Item Height"; } else { error = "Scroll View needs to be using Horizontal or Vertical movement"; } } serializedObject.Update(); GUILayout.BeginHorizontal(); NGUIEditorTools.DrawProperty(fieldName, serializedObject, "itemSize", GUILayout.Width(130f)); GUILayout.Label("pixels"); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); SerializedProperty sp1 = NGUIEditorTools.DrawProperty("Range Limit", serializedObject, "minIndex", GUILayout.Width(130f)); NGUIEditorTools.SetLabelWidth(20f); SerializedProperty sp2 = NGUIEditorTools.DrawProperty("to", serializedObject, "maxIndex", GUILayout.Width(60f)); NGUIEditorTools.SetLabelWidth(90f); if (sp1.intValue == sp2.intValue) { GUILayout.Label("unlimited"); } GUILayout.EndHorizontal(); NGUIEditorTools.DrawProperty("Cull Content", serializedObject, "cullContent"); if (!string.IsNullOrEmpty(error)) { EditorGUILayout.HelpBox(error, MessageType.Error); if (sv != null && GUILayout.Button("Select the Scroll View")) { Selection.activeGameObject = sv.gameObject; } } serializedObject.ApplyModifiedProperties(); if (sp1.intValue != sp2.intValue) { if ((target as UIWrapContent).GetComponent <UICenterOnChild>() != null) { EditorGUILayout.HelpBox("Limiting indices doesn't play well with UICenterOnChild. You should either not limit the indices, or not use UICenterOnChild.", MessageType.Warning); } } }
private void FindParent() { Transform parent = base.cachedTransform.parent; mParentPanel = ((!(parent != null)) ? null : NGUITools.FindInParents <UIPanel>(parent.gameObject)); }
public void Reposition() { if (!this.mStarted) { this.repositionNow = true; return; } Transform transforms = base.transform; int num = 0; int num1 = 0; if (!this.sorted) { for (int i = 0; i < transforms.childCount; i++) { Transform child = transforms.GetChild(i); if (child.gameObject.activeInHierarchy || !this.hideInactive) { float single = child.localPosition.z; child.localPosition = (this.arrangement != UIGrid.Arrangement.Horizontal ? new Vector3(this.cellWidth * (float)num1, -this.cellHeight * (float)num, single) : new Vector3(this.cellWidth * (float)num, -this.cellHeight * (float)num1, single)); int num2 = num + 1; num = num2; if (num2 >= this.maxPerLine && this.maxPerLine > 0) { num = 0; num1++; } } } } else { List <Transform> transforms1 = new List <Transform>(); for (int j = 0; j < transforms.childCount; j++) { Transform child1 = transforms.GetChild(j); if (child1) { transforms1.Add(child1); } } transforms1.Sort(new Comparison <Transform>(UIGrid.SortByName)); int num3 = 0; int count = transforms1.Count; while (num3 < count) { Transform item = transforms1[num3]; if (item.gameObject.activeInHierarchy || !this.hideInactive) { float single1 = item.localPosition.z; item.localPosition = (this.arrangement != UIGrid.Arrangement.Horizontal ? new Vector3(this.cellWidth * (float)num1, -this.cellHeight * (float)num, single1) : new Vector3(this.cellWidth * (float)num, -this.cellHeight * (float)num1, single1)); int num4 = num + 1; num = num4; if (num4 >= this.maxPerLine && this.maxPerLine > 0) { num = 0; num1++; } } num3++; } } UIDraggablePanel uIDraggablePanel = NGUITools.FindInParents <UIDraggablePanel>(base.gameObject); if (uIDraggablePanel != null) { uIDraggablePanel.UpdateScrollbars(true); } }
protected virtual void Init() { this.mInitDone = true; this.mPanel = NGUITools.FindInParents <UIPanel>(base.get_gameObject()); }
/// <summary> /// Create a new UI. /// </summary> static public UIPanel CreateUI(Transform trans, bool advanced3D, int layer) { // Find the existing UI Root UIRoot root = (trans != null) ? NGUITools.FindInParents <UIRoot>(trans.gameObject) : null; if (root == null && UIRoot.list.Count > 0) { root = UIRoot.list[0]; } // If no root found, create one if (root == null) { GameObject go = NGUITools.AddChild(null, false); root = go.AddComponent <UIRoot>(); // Automatically find the layers if none were specified if (layer == -1) { layer = LayerMask.NameToLayer("UI"); } if (layer == -1) { layer = LayerMask.NameToLayer("2D UI"); } go.layer = layer; if (advanced3D) { go.name = "UI Root (3D)"; root.scalingStyle = UIRoot.Scaling.FixedSize; } else { go.name = "UI Root"; root.scalingStyle = UIRoot.Scaling.PixelPerfect; } } // Find the first panel UIPanel panel = root.GetComponentInChildren <UIPanel>(); if (panel == null) { // Find other active cameras in the scene Camera[] cameras = NGUITools.FindActive <Camera>(); float depth = -1f; bool colorCleared = false; int mask = (1 << root.gameObject.layer); for (int i = 0; i < cameras.Length; ++i) { Camera c = cameras[i]; // If the color is being cleared, we won't need to if (c.clearFlags == CameraClearFlags.Color || c.clearFlags == CameraClearFlags.Skybox) { colorCleared = true; } // Choose the maximum depth depth = Mathf.Max(depth, c.depth); // Make sure this WcCamera can't see the UI c.cullingMask = (c.cullingMask & (~mask)); } // Create a WcCamera that will draw the UI Camera cam = NGUITools.AddChild <Camera>(root.gameObject, false); cam.gameObject.AddComponent <UICamera>(); cam.clearFlags = colorCleared ? CameraClearFlags.Depth : CameraClearFlags.Color; cam.backgroundColor = Color.grey; cam.cullingMask = mask; cam.depth = depth + 1f; if (advanced3D) { cam.nearClipPlane = 0.1f; cam.farClipPlane = 4f; cam.transform.localPosition = new Vector3(0f, 0f, -700f); } else { cam.orthographic = true; cam.orthographicSize = 1; cam.nearClipPlane = -10; cam.farClipPlane = 10; } // Make sure there is an audio listener present AudioListener[] listeners = NGUITools.FindActive <AudioListener>(); if (listeners == null || listeners.Length == 0) { cam.gameObject.AddComponent <AudioListener>(); } // Add a panel to the root panel = root.gameObject.AddComponent <UIPanel>(); #if UNITY_EDITOR UnityEditor.Selection.activeGameObject = panel.gameObject; #endif } if (trans != null) { // Find the root object while (trans.parent != null) { trans = trans.parent; } if (NGUITools.IsChild(trans, panel.transform)) { // Odd hierarchy -- can't reparent panel = trans.gameObject.AddComponent <UIPanel>(); } else { // Reparent this root object to be a child of the panel trans.parent = panel.transform; trans.localScale = Vector3.one; trans.localPosition = Vector3.zero; SetChildLayer(panel.cachedTransform, panel.cachedGameObject.layer); } } return(panel); }
public void Recenter() { if (mScrollView == null) { mScrollView = NGUITools.FindInParents <UIScrollView>(gameObject); if (mScrollView == null) { Debug.LogWarning(GetType() + " requires " + typeof(UIScrollView) + " on a parent object in order to work", this); enabled = false; return; } else { if (mScrollView) { mScrollView.centerOnChild = this; mScrollView.onDragFinished += OnDragFinished; } if (mScrollView.horizontalScrollBar != null) { mScrollView.horizontalScrollBar.onDragFinished += OnDragFinished; } if (mScrollView.verticalScrollBar != null) { mScrollView.verticalScrollBar.onDragFinished += OnDragFinished; } } } if (mScrollView.panel == null) { return; } Transform trans = transform; if (trans.childCount == 0) { return; } // Calculate the panel's center in world coordinates Vector3[] corners = mScrollView.panel.worldCorners; Vector3 panelCenter = (corners[2] + corners[0]) * 0.5f; // Offset this value by the momentum Vector3 momentum = mScrollView.currentMomentum * mScrollView.momentumAmount; Vector3 moveDelta = NGUIMath.SpringDampen(ref momentum, 9f, 2f); Vector3 pickingPoint = panelCenter - moveDelta * 0.01f; // Magic number based on what "feels right" float min = float.MaxValue; Transform closest = null; int index = 0; int ignoredIndex = 0; UIGrid grid = GetComponent <UIGrid>(); List <Transform> list = null; // Determine the closest child if (grid != null) { list = grid.GetChildList(); for (int i = 0, imax = list.Count, ii = 0; i < imax; ++i) { Transform t = list[i]; if (!t.gameObject.activeInHierarchy) { continue; } float sqrDist = Vector3.SqrMagnitude(t.position - pickingPoint); if (sqrDist < min) { min = sqrDist; closest = t; index = i; ignoredIndex = ii; } ++ii; } } else { for (int i = 0, imax = trans.childCount, ii = 0; i < imax; ++i) { Transform t = trans.GetChild(i); if (!t.gameObject.activeInHierarchy) { continue; } float sqrDist = Vector3.SqrMagnitude(t.position - pickingPoint); if (sqrDist < min) { min = sqrDist; closest = t; index = i; ignoredIndex = ii; } ++ii; } } // If we have a touch in progress and the next page threshold set if (nextPageThreshold > 0f && UICamera.currentTouch != null) { // If we're still on the same object if (mCenteredObject != null && mCenteredObject.transform == (list != null ? list[index] : trans.GetChild(index))) { Vector3 totalDelta = UICamera.currentTouch.totalDelta; totalDelta = transform.rotation * totalDelta; float delta = 0f; switch (mScrollView.movement) { case UIScrollView.Movement.Horizontal: { delta = totalDelta.x; break; } case UIScrollView.Movement.Vertical: { delta = totalDelta.y; break; } default: { delta = totalDelta.magnitude; break; } } if (Mathf.Abs(delta) > nextPageThreshold) { if (delta > nextPageThreshold) { // Next page if (list != null) { if (ignoredIndex > 0) { closest = list[ignoredIndex - 1]; } else { closest = (GetComponent <UIWrapContent>() == null) ? list[0] : list[list.Count - 1]; } } else if (ignoredIndex > 0) { closest = trans.GetChild(ignoredIndex - 1); } else { closest = (GetComponent <UIWrapContent>() == null) ? trans.GetChild(0) : trans.GetChild(trans.childCount - 1); } } else if (delta < -nextPageThreshold) { // Previous page if (list != null) { if (ignoredIndex < list.Count - 1) { closest = list[ignoredIndex + 1]; } else { closest = (GetComponent <UIWrapContent>() == null) ? list[list.Count - 1] : list[0]; } } else if (ignoredIndex < trans.childCount - 1) { closest = trans.GetChild(ignoredIndex + 1); } else { closest = (GetComponent <UIWrapContent>() == null) ? trans.GetChild(trans.childCount - 1) : trans.GetChild(0); } } } } } CenterOn(closest, panelCenter); }
/// <summary> /// Helper function to check if the specified hit is visible by the panel. /// </summary> static bool IsVisible(ref DepthEntry de) { UIPanel panel = NGUITools.FindInParents <UIPanel>(de.hit.collider.gameObject); return(panel == null || panel.IsVisible(de.hit.point)); }
private void Start() { UiPanel = NGUITools.FindInParents <UIPanel>(gameObject); UiPanel.AddUiSort(this); }
void Start() { mCenter = NGUITools.FindInParents <UICenterOnChild>(gameObject); mPanel = NGUITools.FindInParents <UIPanel>(gameObject); }
/// <summary> /// Helper function that returns the selected root object. /// </summary> static public GameObject SelectedRoot(bool createIfMissing) { GameObject go = Selection.activeGameObject; // Only use active objects if (go != null && !NGUITools.GetActive(go)) { go = null; } // Try to find a panel UIPanel p = (go != null) ? NGUITools.FindInParents <UIPanel>(go) : null; // No selection? Try to find the root automatically if (p == null) { UIPanel[] panels = NGUITools.FindActive <UIPanel>(); if (panels.Length > 0) { go = panels[0].gameObject; } } // Now find the first uniformly scaled object if (go != null) { Transform t = go.transform; // Find the first uniformly scaled object while (!Mathf.Approximately(t.localScale.x, t.localScale.y) || !Mathf.Approximately(t.localScale.x, t.localScale.z)) { t = t.parent; if (t == null) { return((p != null) ? p.gameObject : null); } else { go = t.gameObject; } } } if (createIfMissing && go == null) { // No object specified -- find the first panel if (go == null) { UIPanel panel = GameObject.FindObjectOfType(typeof(UIPanel)) as UIPanel; if (panel != null) { go = panel.gameObject; } } // No UI present -- create a new one if (go == null) { go = UICreateNewUIWizard.CreateNewUI(); } } return(go); }
/// <summary> /// Find the necessary components. /// </summary> protected virtual void Init() { mInitDone = true; mPanel = NGUITools.FindInParents <UIPanel>(gameObject); }
public static float GetPixelSizeAdjustment(GameObject go) { UIRoot uIRoot = NGUITools.FindInParents <UIRoot>(go); return((!(uIRoot != null)) ? 1f : uIRoot.pixelSizeAdjustment); }
private void Start() { this.mRoot = NGUITools.FindInParents <UIRoot>(base.gameObject); }
/// <summary> /// Perform any logic related to starting the drag & drop operation. /// </summary> protected virtual void OnDragDropStart() { if (!draggedItems.Contains(this)) { draggedItems.Add(this); } // Automatically disable the scroll view if (mDragScrollView != null) { mDragScrollView.enabled = false; } // Disable the collider so that it doesn't intercept events if (mButton != null) { mButton.isEnabled = false; } else if (mCollider != null) { mCollider.enabled = false; } else if (mCollider2D != null) { mCollider2D.enabled = false; } mParent = mTrans.parent; mRoot = NGUITools.FindInParents <UIRoot>(mParent); mGrid = NGUITools.FindInParents <UIGrid>(mParent); mTable = NGUITools.FindInParents <UITable>(mParent); // Re-parent the item if (UIDragDropRoot.root != null) { mTrans.parent = UIDragDropRoot.root; } Vector3 pos = mTrans.localPosition; pos.z = 0f; mTrans.localPosition = pos; TweenPosition tp = GetComponent <TweenPosition>(); if (tp != null) { tp.enabled = false; } SpringPosition sp = GetComponent <SpringPosition>(); if (sp != null) { sp.enabled = false; } // Notify the widgets that the parent has changed NGUITools.MarkParentAsChanged(gameObject); if (mTable != null) { mTable.repositionNow = true; } if (mGrid != null) { mGrid.repositionNow = true; } //Added this to hook into these better ~digi gameObject.SendMessage("OnDragDropStartEvent", SendMessageOptions.DontRequireReceiver); }
/// <summary> /// Add commonly NGUI context menu options. /// </summary> static public void AddCommonItems(GameObject target) { if (target != null) { UIWidget widget = target.GetComponent <UIWidget>(); string myName = string.Format("Selected {0}", (widget != null) ? NGUITools.GetTypeName(widget) : "Object"); AddItem(myName + "/Bring to Front", false, delegate(object obj) { NGUITools.BringForward(Selection.activeGameObject); }, null); AddItem(myName + "/Push to Back", false, delegate(object obj) { NGUITools.PushBack(Selection.activeGameObject); }, null); AddItem(myName + "/Nudge Forward", false, delegate(object obj) { NGUITools.AdjustDepth(Selection.activeGameObject, 1); }, null); AddItem(myName + "/Nudge Back", false, delegate(object obj) { NGUITools.AdjustDepth(Selection.activeGameObject, -1); }, null); if (widget != null) { NGUIContextMenu.AddSeparator(myName + "/"); AddItem(myName + "/Make Pixel-Perfect", false, OnMakePixelPerfect, Selection.activeTransform); if (target.GetComponent <BoxCollider>() != null) { AddItem(myName + "/Reset Collider Size", false, OnBoxCollider, target); } } NGUIContextMenu.AddSeparator(myName + "/"); AddItem(myName + "/Delete", false, OnDelete, target); NGUIContextMenu.AddSeparator(""); if (Selection.activeTransform.parent != null && widget != null) { AddChildWidget("Create/Sprite/Child", false, NGUISettings.AddSprite); AddChildWidget("Create/Label/Child", false, NGUISettings.AddLabel); AddChildWidget("Create/Invisible Widget/Child", false, NGUISettings.AddWidget); AddChildWidget("Create/Simple Texture/Child", false, NGUISettings.AddTexture); #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2 AddChildWidget("Create/Unity 2D Sprite/Child", false, NGUISettings.Add2DSprite); #endif AddSiblingWidget("Create/Sprite/Sibling", false, NGUISettings.AddSprite); AddSiblingWidget("Create/Label/Sibling", false, NGUISettings.AddLabel); AddSiblingWidget("Create/Invisible Widget/Sibling", false, NGUISettings.AddWidget); AddSiblingWidget("Create/Simple Texture/Sibling", false, NGUISettings.AddTexture); #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2 AddSiblingWidget("Create/Unity 2D Sprite/Sibling", false, NGUISettings.Add2DSprite); #endif } else { AddChildWidget("Create/Sprite", false, NGUISettings.AddSprite); AddChildWidget("Create/Label", false, NGUISettings.AddLabel); AddChildWidget("Create/Invisible Widget", false, NGUISettings.AddWidget); AddChildWidget("Create/Simple Texture", false, NGUISettings.AddTexture); #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2 AddChildWidget("Create/Unity 2D Sprite", false, NGUISettings.Add2DSprite); #endif } NGUIContextMenu.AddSeparator("Create/"); AddItem("Create/Panel", false, AddPanel, target); AddItem("Create/Scroll View", false, AddScrollView, target); AddItem("Create/Grid", false, AddChild <UIGrid>, target); AddItem("Create/Table", false, AddChild <UITable>, target); AddItem("Create/Anchor (Legacy)", false, AddChild <UIAnchor>, target); if (target.GetComponent <UIPanel>() != null) { if (target.GetComponent <UIScrollView>() == null) { AddItem("Attach/Scroll View", false, delegate(object obj) { target.AddComponent <UIScrollView>(); }, null); NGUIContextMenu.AddSeparator("Attach/"); } } else if (target.collider == null) { AddItem("Attach/Box Collider", false, delegate(object obj) { NGUITools.AddWidgetCollider(target); }, null); NGUIContextMenu.AddSeparator("Attach/"); } bool header = false; UIScrollView scrollView = NGUITools.FindInParents <UIScrollView>(target); if (scrollView != null) { if (scrollView.GetComponentInChildren <UICenterOnChild>() == null) { AddItem("Attach/Center Scroll View on Child", false, delegate(object obj) { target.AddComponent <UICenterOnChild>(); }, null); header = true; } } if (target.collider != null) { if (scrollView != null) { if (target.GetComponent <UIDragScrollView>() == null) { AddItem("Attach/Drag Scroll View", false, delegate(object obj) { target.AddComponent <UIDragScrollView>(); }, null); header = true; } if (target.GetComponent <UICenterOnClick>() == null && NGUITools.FindInParents <UICenterOnChild>(target) != null) { AddItem("Attach/Center Scroll View on Click", false, delegate(object obj) { target.AddComponent <UICenterOnClick>(); }, null); header = true; } } if (header) { NGUIContextMenu.AddSeparator("Attach/"); } AddItem("Attach/Button Script", false, delegate(object obj) { target.AddComponent <UIButton>(); }, null); AddItem("Attach/Toggle Script", false, delegate(object obj) { target.AddComponent <UIToggle>(); }, null); AddItem("Attach/Slider Script", false, delegate(object obj) { target.AddComponent <UISlider>(); }, null); AddItem("Attach/Scroll Bar Script", false, delegate(object obj) { target.AddComponent <UIScrollBar>(); }, null); AddItem("Attach/Progress Bar Script", false, delegate(object obj) { target.AddComponent <UISlider>(); }, null); AddItem("Attach/Popup List Script", false, delegate(object obj) { target.AddComponent <UIPopupList>(); }, null); AddItem("Attach/Input Field Script", false, delegate(object obj) { target.AddComponent <UIInput>(); }, null); NGUIContextMenu.AddSeparator("Attach/"); if (target.GetComponent <UIDragResize>() == null) { AddItem("Attach/Drag Resize Script", false, delegate(object obj) { target.AddComponent <UIDragResize>(); }, null); } if (target.GetComponent <UIDragScrollView>() == null) { for (int i = 0; i < UIPanel.list.size; ++i) { UIPanel pan = UIPanel.list[i]; if (pan.clipping == UIDrawCall.Clipping.None) { continue; } UIScrollView dr = pan.GetComponent <UIScrollView>(); if (dr == null) { continue; } AddItem("Attach/Drag Scroll View", false, delegate(object obj) { target.AddComponent <UIDragScrollView>().scrollView = dr; }, null); header = true; break; } } AddItem("Attach/Key Binding Script", false, delegate(object obj) { target.AddComponent <UIKeyBinding>(); }, null); NGUIContextMenu.AddSeparator("Attach/"); AddItem("Attach/Play Tween Script", false, delegate(object obj) { target.AddComponent <UIPlayTween>(); }, null); AddItem("Attach/Play Animation Script", false, delegate(object obj) { target.AddComponent <UIPlayAnimation>(); }, null); AddItem("Attach/Play Sound Script", false, delegate(object obj) { target.AddComponent <UIPlaySound>(); }, null); } if (target.GetComponent <UILocalize>() == null) { AddItem("Attach/Localization Script", false, delegate(object obj) { target.AddComponent <UILocalize>(); }, null); } if (widget != null) { AddMissingItem <TweenAlpha>(target, "Tween/Alpha"); AddMissingItem <TweenColor>(target, "Tween/Color"); AddMissingItem <TweenWidth>(target, "Tween/Width"); AddMissingItem <TweenHeight>(target, "Tween/Height"); } else if (target.GetComponent <UIPanel>() != null) { AddMissingItem <TweenAlpha>(target, "Tween/Alpha"); } NGUIContextMenu.AddSeparator("Tween/"); AddMissingItem <TweenPosition>(target, "Tween/Position"); AddMissingItem <TweenRotation>(target, "Tween/Rotation"); AddMissingItem <TweenScale>(target, "Tween/Scale"); AddMissingItem <TweenTransform>(target, "Tween/Transform"); if (target.GetComponent <AudioSource>() != null) { AddMissingItem <TweenVolume>(target, "Tween/Volume"); } if (target.GetComponent <Camera>() != null) { AddMissingItem <TweenFOV>(target, "Tween/Field of View"); AddMissingItem <TweenOrthoSize>(target, "Tween/Orthographic Size"); } } }
/// <summary> /// Drop the item onto the specified object. /// </summary> protected virtual void OnDragDropRelease(GameObject surface) { if (!cloneOnDrag) { // Clear the reference to the scroll view since it might be in another scroll view now var drags = GetComponentsInChildren <UIDragScrollView>(); foreach (var d in drags) { d.scrollView = null; } // Re-enable the collider if (mButton != null) { mButton.isEnabled = true; } else if (mCollider != null) { mCollider.enabled = true; } else if (mCollider2D != null) { mCollider2D.enabled = true; } // Is there a droppable container? UIDragDropContainer container = surface ? NGUITools.FindInParents <UIDragDropContainer>(surface) : null; if (container != null) { // Container found -- parent this object to the container mTrans.parent = (container.reparentTarget != null) ? container.reparentTarget : container.transform; Vector3 pos = mTrans.localPosition; pos.z = 0f; mTrans.localPosition = pos; } else { // No valid container under the mouse -- revert the item's parent mTrans.parent = mParent; } // Update the grid and table references mParent = mTrans.parent; mGrid = NGUITools.FindInParents <UIGrid>(mParent); mTable = NGUITools.FindInParents <UITable>(mParent); // Re-enable the drag scroll view script if (mDragScrollView != null) { Invoke("EnableDragScrollView", 0.001f); } // Notify the widgets that the parent has changed NGUITools.MarkParentAsChanged(gameObject); if (mTable != null) { mTable.repositionNow = true; } if (mGrid != null) { mGrid.repositionNow = true; } } // We're now done OnDragDropEnd(surface); if (cloneOnDrag) { DestroySelf(); } //Added this to hook into these better ~digi gameObject.SendMessage("OnDragDropReleaseEvent", SendMessageOptions.DontRequireReceiver); }
/// <summary> /// Cache the root. /// </summary> void Start() { mRoot = NGUITools.FindInParents <UIRoot>(gameObject); }
// Use this for initialization void Start() { _Carema = NGUITools.FindInParents <Camera>(gameObject); }
/// <summary> /// Drop the item onto the specified object. /// </summary> protected virtual void OnDragDropRelease(GameObject surface) { if (!cloneOnDrag) { mTouchID = int.MinValue; // Re-enable the collider if (mButton != null) { mButton.isEnabled = true; } else if (mCollider != null) { mCollider.enabled = true; } else if (mCollider2D != null) { mCollider2D.enabled = true; } // Is there a droppable container? UIDragDropContainer container = surface ? NGUITools.FindInParents <UIDragDropContainer>(surface) : null; if (container != null) { // Container found -- parent this object to the container mTrans.parent = (container.reparentTarget != null) ? container.reparentTarget : container.transform; Vector3 pos = mTrans.localPosition; pos.z = 0f; mTrans.localPosition = pos; } else { // No valid container under the mouse -- revert the item's parent mTrans.parent = mParent; } // Update the grid and table references mParent = mTrans.parent; mGrid = NGUITools.FindInParents <UIGrid>(mParent); mTable = NGUITools.FindInParents <UITable>(mParent); // Re-enable the drag scroll view script if (mDragScrollView != null) { StartCoroutine(EnableDragScrollView()); } // Notify the widgets that the parent has changed NGUITools.MarkParentAsChanged(gameObject); if (mTable != null) { mTable.repositionNow = true; } if (mGrid != null) { mGrid.repositionNow = true; } } else { NGUITools.Destroy(gameObject); } }
public void DrawAnchorTransform() { if (NGUIEditorTools.DrawHeader("Anchors")) { NGUIEditorTools.BeginContents(); NGUIEditorTools.SetLabelWidth(NGUISettings.minimalisticLook ? 69f : 62f); EditorGUI.BeginDisabledGroup(!((target as UIRect).canBeAnchored)); GUILayout.BeginHorizontal(); AnchorType type = (AnchorType)EditorGUILayout.EnumPopup("Type", mAnchorType); NGUIEditorTools.DrawPadding(); GUILayout.EndHorizontal(); SerializedProperty[] tg = new SerializedProperty[4]; for (int i = 0; i < 4; ++i) { tg[i] = serializedObject.FindProperty(FieldName[i] + ".target"); } if (mAnchorType == AnchorType.None && type != AnchorType.None) { if (type == AnchorType.Unified) { if (mTarget[0] == null && mTarget[1] == null && mTarget[2] == null && mTarget[3] == null) { UIRect rect = target as UIRect; UIRect parent = NGUITools.FindInParents <UIRect>(rect.cachedTransform.parent); if (parent != null) { for (int i = 0; i < 4; ++i) { mTarget[i] = parent.cachedTransform; } } } } for (int i = 0; i < 4; ++i) { tg[i].objectReferenceValue = mTarget[i]; mTarget[i] = null; } UpdateAnchors(true); } if (type != AnchorType.None) { NGUIEditorTools.DrawPaddedProperty("Execute", serializedObject, "updateAnchors"); } if (type == AnchorType.Advanced) { DrawAnchor(0, true); DrawAnchor(1, true); DrawAnchor(2, true); DrawAnchor(3, true); } else if (type == AnchorType.Unified) { DrawSingleAnchorSelection(); DrawAnchor(0, false); DrawAnchor(1, false); DrawAnchor(2, false); DrawAnchor(3, false); } else if (type == AnchorType.None && mAnchorType != type) { // Save values to make it easy to "go back" for (int i = 0; i < 4; ++i) { mTarget[i] = tg[i].objectReferenceValue as Transform; tg[i].objectReferenceValue = null; } serializedObject.FindProperty("leftAnchor.relative").floatValue = 0f; serializedObject.FindProperty("bottomAnchor.relative").floatValue = 0f; serializedObject.FindProperty("rightAnchor.relative").floatValue = 1f; serializedObject.FindProperty("topAnchor.relative").floatValue = 1f; } mAnchorType = type; OnDrawFinalProperties(); EditorGUI.EndDisabledGroup(); NGUIEditorTools.EndContents(); } }