public float GetContentWidth() { float width = 0f; bool first = true; foreach (RectTransform child in this.transform) { if (child != null && child.gameObject.activeSelf) { if (first) { first = false; } else { width += _spacing; } width += RectTransformUtils.GetWidth(child); } } return(width); }
private void UpdateChildren(float movementTime) { float xPos = GetInitalX(); List <GameObject> activeChildren = new List <GameObject>(); foreach (RectTransform child in this.transform) { if (child != null && child.gameObject.activeSelf) { if (!_childMovers.TryGetValue(child, out RectTransformMover mover) || mover == null) { mover = child.GetComponent <RectTransformMover>(); _childMovers.Add(child, mover); } activeChildren.Add(child.gameObject); if (mover != null) { bool wasActive = _activeChildren.Contains(child.gameObject); mover.SetX(xPos, wasActive ? movementTime : -1f, _movementInterpolationType); } else { RectTransformUtils.SetX(child, xPos); } xPos += RectTransformUtils.GetWidth(child) + _spacing; } } _activeChildren = activeChildren; }
private void UpdateScrollBar(ScrollRect scrollView, Entity scrollBarEntity, float2 contentToViewportRatio, ref ScrollViewTransforms transforms, ScrollBarAxis axis) { var scrollBar = ScrollBarFromEntity[scrollBarEntity]; var areaTransform = RectTransformFromEntity[scrollBarEntity]; var handleTransform = RectTransformFromEntity[scrollBar.ScrollHandle]; var scrollBarSize = math.saturate(contentToViewportRatio); float2 moveRange = (transforms.ViewportRect.Size - transforms.ContentRect.Size); float2 contentSize = transforms.ContentRect.Size; if (axis == ScrollBarAxis.Vertical) { areaTransform.AnchorMin = new float2(areaTransform.AnchorMin.x, 0.0f); areaTransform.AnchorMax = new float2(areaTransform.AnchorMax.x, 1.0f); areaTransform.Pivot = new float2(1.0f, 1.0f); areaTransform.SizeDelta = new float2(areaTransform.SizeDelta.x, -(areaTransform.SizeDelta.x + scrollView.HorizontalBarSpacing)); var areaRect = RectTransformUtils.CalculateWorldSpaceRect(transforms.ScrollViewRect, ElementScaleFromEntity[scrollBarEntity].Value, areaTransform); handleTransform.AnchorMin = new float2(0.0f, scrollBar.Value * (1.0f - scrollBarSize.y)); handleTransform.AnchorMax = new float2(1.0f, handleTransform.AnchorMin.y + scrollBarSize.y); WorldSpaceRect handleRect = RectTransformUtils.CalculateWorldSpaceRect(areaRect, ElementScaleFromEntity[scrollBarEntity].Value, handleTransform); scrollBar.HandleDragSensitivity = 1.0f / (areaRect.Size.y - handleRect.Size.y); scrollBar.RectDragSensitivity = scrollBar.HandleDragSensitivity / ((transforms.ContentRect.Size.y - transforms.ViewportRect.Size.y) / (areaRect.Size.y - handleRect.Size.y)); transforms.ContentRect.Min.y = transforms.ViewportRect.Min.y + scrollBar.Value * moveRange.y; transforms.ContentRect.Max.y = transforms.ContentRect.Min.y + contentSize.y; } else { areaTransform.AnchorMin = new float2(0.0f, areaTransform.AnchorMin.y); areaTransform.AnchorMax = new float2(1.0f, areaTransform.AnchorMax.y); areaTransform.Pivot = new float2(0.0f, 0.0f); areaTransform.SizeDelta = new float2(-(areaTransform.SizeDelta.y + scrollView.VerticalBarSpacing), areaTransform.SizeDelta.y); var areaRect = RectTransformUtils.CalculateWorldSpaceRect(transforms.ScrollViewRect, ElementScaleFromEntity[scrollBarEntity].Value, areaTransform); handleTransform.AnchorMin = new float2(scrollBar.Value * (1.0f - scrollBarSize.x), 0.0f); handleTransform.AnchorMax = new float2(handleTransform.AnchorMin.x + scrollBarSize.x, 1.0f); WorldSpaceRect handleRect = RectTransformUtils.CalculateWorldSpaceRect(areaRect, ElementScaleFromEntity[scrollBarEntity].Value, handleTransform); scrollBar.HandleDragSensitivity = 1.0f / (areaRect.Size.x - handleRect.Size.x); scrollBar.RectDragSensitivity = scrollBar.HandleDragSensitivity / ((transforms.ContentRect.Size.x - transforms.ViewportRect.Size.x) / (areaRect.Size.x - handleRect.Size.x)); transforms.ContentRect.Min.x = transforms.ViewportRect.Min.x + scrollBar.Value * moveRange.x; transforms.ContentRect.Max.x = transforms.ContentRect.Min.x + contentSize.x; } ScrollBarFromEntity[scrollBarEntity] = scrollBar; RectTransformFromEntity[scrollBar.ScrollHandle] = handleTransform; RectTransformFromEntity[scrollBarEntity] = areaTransform; }
void Update() { // Slide out when mouse not over trigger area and not over this RectTransform itself if (isInside && !mouseOverTriggerArea && !RectTransformUtils.IsMouseOverRectTransform(rectTransform)) { SlideOut(); } }
public void Attach(WindowController window) { if (!window) { Debug.LogError("UICanvas.Attach: window is null."); return; } var trans = window.CachedTransform; RectTransformUtils.UpdateLocalPosition(trans, Vector2.zero); }
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) { NativeArray <ScrollRect> scrollRect = chunk.GetNativeArray(ScrollRectType); NativeArray <Entity> scrollEntity = chunk.GetNativeArray(EntityType); var rebuildContext = new HierarchyRebuildContext() { ChildrenFromEntity = ChildrenFromEntity, WorldSpaceRectFromEntity = WorldSpaceRectFromEntity, RectTransformFromEntity = RectTransformFromEntity, RebuildFlagFromEntity = RebuildFlagFromEntity, ElementScaleFromEntity = ElementScaleFromEntity, WorldSpaceMaskFromEntity = WorldSpaceMaskFromEntity, RectMaskFromEntity = RectMaskFromEntity }; for (int i = 0; i < scrollRect.Length; i++) { ScrollRect scrollView = scrollRect[i]; ScrollViewTransforms transforms = GatherTransforms(scrollEntity[i], scrollView); var oldContentRect = transforms.ContentRect; transforms.ViewportTransform.AnchorMin = new float2(0.0f, 0.0f); transforms.ViewportTransform.AnchorMax = new float2(1.0f, 1.0f); transforms.ViewportTransform.SizeDelta = new float2(-transforms.VerticalAreaTransform.SizeDelta.x - scrollView.VerticalBarSpacing, -transforms.HorizontalAreaTransform.SizeDelta.y - scrollView.HorizontalBarSpacing); transforms.ViewportRect = RectTransformUtils.CalculateWorldSpaceRect(transforms.ScrollViewRect, ElementScaleFromEntity[scrollEntity[i]].Value, transforms.ViewportTransform); RectTransformFromEntity[scrollView.Viewport] = transforms.ViewportTransform; var contentToViewportRatio = transforms.ViewportRect.Size / transforms.ContentRect.Size; var verticalEntity = scrollView.VerticalBar; if (ScrollBarFromEntity.Exists(verticalEntity)) { UpdateScrollBar(scrollView, verticalEntity, contentToViewportRatio, ref transforms, ScrollBarAxis.Vertical); } var horizontalEntity = scrollView.HorizontalBar; if (ScrollBarFromEntity.Exists(horizontalEntity)) { UpdateScrollBar(scrollView, horizontalEntity, contentToViewportRatio, ref transforms, ScrollBarAxis.Horizontal); } if (NeedUpdate(oldContentRect, transforms.ContentRect)) { var newContentTransform = RectTransformUtils.CalculateInverseTransformWithAnchors(transforms.ContentRect, transforms.ViewportRect, transforms.ContentTransform, ElementScaleFromEntity[scrollView.Content].Value); RectTransformFromEntity[scrollView.Content] = newContentTransform; UpdateScrollRectTransform(scrollEntity[i], transforms.ScrollViewRect, rebuildContext); } } }
//================================================================================ // 関数(static) //================================================================================ /// <summary> /// 指定された UI オブジェクトがクリックできる場合 true を返します /// </summary> public static bool CanClick(Graphic graphic, Func <GameObject, bool> isClick) { if (m_raycastResults == null) { m_raycastResults = new List <RaycastResult>(); } if (graphic == null) { return(false); } var gameObject = graphic.gameObject; if (!gameObject.activeInHierarchy || !graphic || !EventSystem.current) { return(false); } var canvas = graphic.canvas; var rectTransform = graphic.rectTransform; var position = RectTransformUtils.RectTransformToScreenPoint(canvas, rectTransform); var pointerEventData = new PointerEventData(EventSystem.current) { position = position, }; m_raycastResults.Clear(); EventSystem.current.RaycastAll(pointerEventData, m_raycastResults); if (m_raycastResults.Count <= 0) { m_raycastResults.Clear(); return(false); } var go = m_raycastResults.FirstOrDefault(x => isClick == null || isClick(x.gameObject)).gameObject; m_raycastResults.Clear(); if (go == null) { return(false); } return (go == gameObject || go.GetComponent <Graphic>() && go.transform.IsChildOf(gameObject.transform) && go.GetComponentInParent <Selectable>().gameObject == gameObject ); }
// Start is called before the first frame update void Start() { RenderedOptions = Render(GameMode); RenderedOptions.First().Align(RectTransform.Edge.Top, OptionHolderPanel.GetEdgePosition_AsFloat(RectTransform.Edge.Top)); RectTransformUtils.LineUp(RenderedOptions, RectTransform.Edge.Bottom); RenderedOptions.ForEach(it => it.Align(RectTransform.Edge.Left, OptionHolderPanel.GetEdgePosition_AsFloat(RectTransform.Edge.Left))); SimpleImages = CreateImages(SimpleImage, ImagesToCreate); RectTransformUtils.LineUp(OptionHolderPanel, SimpleImages, RectTransform.Edge.Bottom); RenderedOptions.ForEach(it => it.Align(RectTransform.Edge.Left, OptionHolderPanel.GetEdgePosition_AsFloat((RectTransform.Edge.Left)))); }
protected override void OnUpdateAnimations(float showLerp, bool showing) { _backer.color = ColorUtils.SetAlpha(_backer.color, _backerAlphaCurve.Evaluate(showLerp)); if (_content != null) { _content.color = ColorUtils.SetAlpha(_content.color, _contentAlphaCurve.Evaluate(showLerp)); } RectTransformUtils.SetWidth(_rectTransform, Mathf.Lerp(_hiddenSize.x, _size.x, _widthCurve.Evaluate(showLerp))); RectTransformUtils.SetHeight(_rectTransform, Mathf.Lerp(_hiddenSize.y, _size.y, _heightCurve.Evaluate(showLerp))); }
private void UpdateChildrenInEditMode() { float xPos = GetInitalX(); foreach (RectTransform child in this.transform) { if (child.gameObject.activeSelf) { RectTransformUtils.SetX(child, xPos); xPos += RectTransformUtils.GetWidth(child) + _spacing; } } }
private void UpdateScrollRectTransform(Entity scrollEntity, WorldSpaceRect scrollRectWorldSpace, HierarchyRebuildContext rebuildContext) { var children = ChildrenFromEntity[scrollEntity]; var scrollMask = WorldSpaceMaskFromEntity[scrollEntity]; var scale = ElementScaleFromEntity[scrollEntity].Value; for (int j = 0; j < children.Length; j++) { RectTransformUtils.UpdateTransformRecursive(ref scrollRectWorldSpace, scrollMask, children[j].Value, scale, ref rebuildContext); } }
private float GetContentWidth() { float width = 0f; foreach (RectTransform child in this.transform) { if (child != null && child.gameObject.activeSelf) { width += RectTransformUtils.GetWidth(child) + _spacing; } } return(width - _spacing); }
private void SetScrollAreaEnabled(bool enabled) { _scrollArea.enabled = enabled; if (!enabled) { RectTransformUtils.SetY(_scrollArea.content, 0f); } RectMask2D mask = _scrollArea.viewport.GetComponent <RectMask2D>(); if (mask != null) { mask.enabled = enabled; } }
void Update() { // Move caret always to the end if (inputField.caretPosition < Text.Length) { inputField.caretPosition = Text.Length; } if (eventSystem.currentSelectedGameObject == inputField.gameObject) { RectTransformSlideIntoViewport.SlideIn(); } else if (!RectTransformUtils.IsMouseOverRectTransform(RectTransformSlideIntoViewport.triggerArea)) { RectTransformSlideIntoViewport.SlideOut(); } }
private void UpdateContentSize() { //TO DO also check items being destroyed if (_items != null) { float contentHeight = StartPadding; for (int i = 0; i < _items.Count; i++) { RectTransform transform = _items[i]._item.GetTransform(); contentHeight += transform.sizeDelta.y; } contentHeight += EndPadding; RectTransformUtils.SetHeight(_scrollArea.content, contentHeight); SetScrollAreaEnabled(contentHeight > _scrollArea.viewport.rect.height); } }
private static void OnInputToggleMenu(InputAction.CallbackContext context) { if (_visuals == null) { GameObject prefab = Resources.Load <GameObject>("Debug Menu"); _visuals = GameObject.Instantiate(prefab).GetComponent <DebugMenuVisuals>(); GameObject.DontDestroyOnLoad(_visuals.gameObject); //Create items GameObject itemPrefab = _visuals._items[0].gameObject; TextMeshProUGUI[] items = new TextMeshProUGUI[_visuals._maxItems]; items[0] = _visuals._items[0]; float y = 0f; for (int i = 1; i < items.Length; i++) { items[i] = GameObject.Instantiate(itemPrefab, itemPrefab.transform.parent, false).GetComponent <TextMeshProUGUI>(); y -= RectTransformUtils.GetHeight(items[i].rectTransform); RectTransformUtils.SetY(items[i].rectTransform, y); } _visuals._items = items; _visuals.gameObject.SetActive(false); _currentMenu = _rootMenu; _currentItemIndex = _currentMenu._items.Count - 1; } if (_visuals.gameObject.activeSelf) { _visuals.gameObject.SetActive(false); } else { _visuals.gameObject.SetActive(true); RefreshMenu(); } }
private void UpdateLines(float deltaTime) { //keep lines in sync TMP_TextInfo textInfo = _textMesh.textInfo; float lineHeight = _lineHeight * _textMesh.fontSize; //Need to add new lines if (_lines.Length < textInfo.lineCount) { Image[] lines = new Image[textInfo.lineCount]; for (int i = 0; i < lines.Length; i++) { if (i < _lines.Length) { lines[i] = _lines[i]; } else { lines[i] = _linePrefabPool.Instantiate(_textMesh.rectTransform).GetComponent <Image>(); lines[i].rectTransform.pivot = new Vector2(0f, 0.5f); lines[i].rectTransform.anchorMin = new Vector2(0f, 0f); lines[i].rectTransform.anchorMax = new Vector2(0f, 0f); lines[i].rectTransform.sizeDelta = new Vector2(0f, lineHeight); } } _lines = lines; if (!_animated) { _lineIndex = textInfo.lineCount; } } //Need to remove lines else if (_lines.Length > textInfo.lineCount) { Image[] lines = new Image[textInfo.lineCount]; for (int i = 0; i < _lines.Length; i++) { if (i < textInfo.lineCount) { lines[i] = _lines[i]; } else { _linePrefabPool.Destroy(_lines[i].gameObject); } } _lines = lines; if (_lineIndex > textInfo.lineCount) { _lineIndex = textInfo.lineCount; _currentLineSpeed = _speed; } } //Update animations if (_animated && _lineIndex < _lines.Length && deltaTime > 0f) { RectTransform currentLine = _lines[_lineIndex].rectTransform; currentLine.anchoredPosition = GetLinePosition(textInfo.lineInfo[_lineIndex], out float lineWidth); float currentWidth = RectTransformUtils.GetWidth(currentLine); if (currentWidth < lineWidth) { _currentLineSpeed += _acceleration * deltaTime; currentWidth += _currentLineSpeed * deltaTime; if (currentWidth >= lineWidth) { currentWidth = lineWidth; _currentLinePauseTimer = _endOfLinePause; } } else { currentWidth = lineWidth; _currentLinePauseTimer -= deltaTime; if (_currentLinePauseTimer <= 0f) { _currentLineSpeed = _speed; _lineIndex++; } } currentLine.sizeDelta = new Vector2(currentWidth, lineHeight); } //Update shown lines for (int i = 0; i < Mathf.Min(_lines.Length, _lineIndex); i++) { _lines[i].rectTransform.anchoredPosition = GetLinePosition(textInfo.lineInfo[i], out float lineWidth); _lines[i].rectTransform.sizeDelta = new Vector2(lineWidth, lineHeight); _lines[i].color = _lineColor; } }
public void InverseRectTransform() { NativeArray <WorldSpaceRect> parentRects = new NativeArray <WorldSpaceRect>(4, Allocator.Temp); parentRects[0] = new WorldSpaceRect() { Min = new float2(0.0f, 0.0f), Max = new float2(100.0f, 100.0f) }; parentRects[1] = new WorldSpaceRect() { Min = new float2(10.0f, 10.0f), Max = new float2(100.0f, 100.0f) }; parentRects[2] = new WorldSpaceRect() { Min = new float2(-10.0f, -10.0f), Max = new float2(100.0f, 100.0f) }; parentRects[3] = new WorldSpaceRect() { Min = new float2(-100.0f, -100.0f), Max = new float2(-10.0f, -10.0f) }; NativeArray <RectTransform> rectTransforms = new NativeArray <RectTransform>(4, Allocator.Temp); rectTransforms[0] = new RectTransform() { AnchorMin = new float2(0.0f, 0.0f), AnchorMax = new float2(1.0f, 1.0f), Pivot = new float2(0.5f, 0.5f), Position = new float2(5.0f, 5.0f), SizeDelta = new float2(20.0f, 20.0f) }; rectTransforms[1] = new RectTransform() { AnchorMin = new float2(0.0f, 0.0f), AnchorMax = new float2(0.0f, 1.0f), Pivot = new float2(1.0f, 1.0f), Position = new float2(5.0f, 5.0f), SizeDelta = new float2(20.0f, 20.0f) }; rectTransforms[2] = new RectTransform() { AnchorMin = new float2(0.5f, 0.5f), AnchorMax = new float2(0.5f, 0.5f), Pivot = new float2(0.5f, 0.5f), Position = new float2(-5.0f, -5.0f), SizeDelta = new float2(20.0f, 20.0f) }; rectTransforms[3] = new RectTransform() { AnchorMin = new float2(1.0f, 1.0f), AnchorMax = new float2(1.0f, 1.0f), Pivot = new float2(0.0f, 0.0f), Position = new float2(-5.0f, -5.0f), SizeDelta = new float2(20.0f, 20.0f) }; for (int i = 0; i < parentRects.Length; i++) { for (int j = 0; j < rectTransforms.Length; j++) { var worldSpace = RectTransformUtils.CalculateWorldSpaceRect(parentRects[i], new float2(1.0f, 1.0f), rectTransforms[j]); var emptyTransform = new RectTransform() { AnchorMin = rectTransforms[j].AnchorMin, AnchorMax = rectTransforms[j].AnchorMax, Pivot = rectTransforms[j].Pivot }; var calculatedTransform = RectTransformUtils.CalculateInverseTransformWithAnchors(worldSpace, parentRects[i], emptyTransform, new float2(1.0f, 1.0f)); Assert.IsTrue(RectTransformEquals(rectTransforms[j], calculatedTransform)); } } }
public static void ShowWindow() { window = (RectTransformUtils)EditorWindow.GetWindow(typeof(RectTransformUtils)); }
private void UpdateWidth() { RectTransformUtils.SetWidth(_textMesh.rectTransform, _textMesh.preferredWidth); }