/// <summary> /// Function to get the Stencil ID /// </summary> /// <param name="obj"></param> /// <returns></returns> public static int GetStencilID(GameObject obj) { // Implementation is almost copied from Unity UI var count = 0; var transform = obj.transform; var stopAfter = FindRootSortOverrideCanvas(transform); if (transform == stopAfter) { return(count); } var t = transform.parent; var components = TMP_ListPool <Mask> .Get(); while (t != null) { t.GetComponents <Mask>(components); for (var i = 0; i < components.Count; ++i) { var mask = components[i]; if (mask != null && mask.MaskEnabled() && mask.graphic.IsActive()) { ++count; break; } } if (t == stopAfter) { break; } t = t.parent; } TMP_ListPool <Mask> .Release(components); return(Mathf.Min((1 << count) - 1, 255)); }
public static Material GetMaterialForRendering(MaskableGraphic graphic, Material baseMaterial) { if (baseMaterial == null) { return(null); } var modifiers = TMP_ListPool <IMaterialModifier> .Get(); graphic.GetComponents(modifiers); var result = baseMaterial; for (int i = 0; i < modifiers.Count; i++) { result = modifiers[i].GetModifiedMaterial(result); } TMP_ListPool <IMaterialModifier> .Release(modifiers); return(result); }
private static Transform FindRootSortOverrideCanvas(Transform start) { // Implementation is copied from Unity UI var canvasList = TMP_ListPool <Canvas> .Get(); start.GetComponentsInParent(false, canvasList); Canvas canvas = null; for (int i = 0; i < canvasList.Count; ++i) { canvas = canvasList[i]; // We found the canvas we want to use break if (canvas.overrideSorting) { break; } } TMP_ListPool <Canvas> .Release(canvasList); return(canvas != null ? canvas.transform : null); }
// Show the dropdown. // // Plan for dropdown scrolling to ensure dropdown is contained within screen. // // We assume the Canvas is the screen that the dropdown must be kept inside. // This is always valid for screen space canvas modes. // For world space canvases we don't know how it's used, but it could be e.g. for an in-game monitor. // We consider it a fair constraint that the canvas must be big enough to contains dropdowns. public void Show() { if (!IsActive() || !IsInteractable() || m_Dropdown != null) { return; } if (!validTemplate) { SetupTemplate(); if (!validTemplate) { return; } } // Get root Canvas. var list = TMP_ListPool <Canvas> .Get(); gameObject.GetComponentsInParent(false, list); if (list.Count == 0) { return; } Canvas rootCanvas = list[0]; TMP_ListPool <Canvas> .Release(list); m_Template.gameObject.SetActive(true); // Instantiate the drop-down template m_Dropdown = CreateDropdownList(m_Template.gameObject); m_Dropdown.name = "Dropdown List"; m_Dropdown.SetActive(true); // Make drop-down RectTransform have same values as original. RectTransform dropdownRectTransform = m_Dropdown.transform as RectTransform; dropdownRectTransform.SetParent(m_Template.transform.parent, false); // Instantiate the drop-down list items // Find the dropdown item and disable it. DropdownItem itemTemplate = m_Dropdown.GetComponentInChildren <DropdownItem>(); GameObject content = itemTemplate.rectTransform.parent.gameObject; RectTransform contentRectTransform = content.transform as RectTransform; itemTemplate.rectTransform.gameObject.SetActive(true); // Get the rects of the dropdown and item Rect dropdownContentRect = contentRectTransform.rect; Rect itemTemplateRect = itemTemplate.rectTransform.rect; // Calculate the visual offset between the item's edges and the background's edges Vector2 offsetMin = itemTemplateRect.min - dropdownContentRect.min + (Vector2)itemTemplate.rectTransform.localPosition; Vector2 offsetMax = itemTemplateRect.max - dropdownContentRect.max + (Vector2)itemTemplate.rectTransform.localPosition; Vector2 itemSize = itemTemplateRect.size; m_Items.Clear(); Toggle prev = null; for (int i = 0; i < options.Count; ++i) { OptionData data = options[i]; DropdownItem item = AddItem(data, value == i, itemTemplate, m_Items); if (item == null) { continue; } // Automatically set up a toggle state change listener item.toggle.isOn = value == i; item.toggle.onValueChanged.AddListener(x => OnSelectItem(item.toggle)); // Select current option if (item.toggle.isOn) { item.toggle.Select(); } // Automatically set up explicit navigation if (prev != null) { Navigation prevNav = prev.navigation; Navigation toggleNav = item.toggle.navigation; prevNav.mode = Navigation.Mode.Explicit; toggleNav.mode = Navigation.Mode.Explicit; prevNav.selectOnDown = item.toggle; prevNav.selectOnRight = item.toggle; toggleNav.selectOnLeft = prev; toggleNav.selectOnUp = prev; prev.navigation = prevNav; item.toggle.navigation = toggleNav; } prev = item.toggle; } // Reposition all items now that all of them have been added Vector2 sizeDelta = contentRectTransform.sizeDelta; sizeDelta.y = itemSize.y * m_Items.Count + offsetMin.y - offsetMax.y; contentRectTransform.sizeDelta = sizeDelta; float extraSpace = dropdownRectTransform.rect.height - contentRectTransform.rect.height; if (extraSpace > 0) { dropdownRectTransform.sizeDelta = new Vector2(dropdownRectTransform.sizeDelta.x, dropdownRectTransform.sizeDelta.y - extraSpace); } // Invert anchoring and position if dropdown is partially or fully outside of canvas rect. // Typically this will have the effect of placing the dropdown above the button instead of below, // but it works as inversion regardless of initial setup. Vector3[] corners = new Vector3[4]; dropdownRectTransform.GetWorldCorners(corners); RectTransform rootCanvasRectTransform = rootCanvas.transform as RectTransform; Rect rootCanvasRect = rootCanvasRectTransform.rect; for (int axis = 0; axis < 2; axis++) { bool outside = false; for (int i = 0; i < 4; i++) { Vector3 corner = rootCanvasRectTransform.InverseTransformPoint(corners[i]); if (corner[axis] < rootCanvasRect.min[axis] || corner[axis] > rootCanvasRect.max[axis]) { outside = true; break; } } if (outside) { RectTransformUtility.FlipLayoutOnAxis(dropdownRectTransform, axis, false, false); } } for (int i = 0; i < m_Items.Count; i++) { RectTransform itemRect = m_Items[i].rectTransform; itemRect.anchorMin = new Vector2(itemRect.anchorMin.x, 0); itemRect.anchorMax = new Vector2(itemRect.anchorMax.x, 0); itemRect.anchoredPosition = new Vector2(itemRect.anchoredPosition.x, offsetMin.y + itemSize.y * (m_Items.Count - 1 - i) + itemSize.y * itemRect.pivot.y); itemRect.sizeDelta = new Vector2(itemRect.sizeDelta.x, itemSize.y); } // Fade in the popup AlphaFadeList(0.15f, 0f, 1f); // Make drop-down template and item template inactive m_Template.gameObject.SetActive(false); itemTemplate.gameObject.SetActive(false); m_Blocker = CreateBlocker(rootCanvas); }