public KsmGuiBase(KsmGuiBase parent) { TopObject = new GameObject(Name); TopTransform = TopObject.AddComponent <RectTransform>(); TopObject.AddComponent <CanvasRenderer>(); if (parent != null) { layoutOptimizer = parent.layoutOptimizer; TopTransform.SetParentFixScale(parent.ParentTransformForChilds); } else { layoutOptimizer = TopObject.AddComponent <KsmGuiLayoutOptimizer>(); } TopObject.SetLayerRecursive(5); }
private void Awake() { Instance = this; tooltipObject = new GameObject("KsmGuiTooltip"); TopTransform = tooltipObject.AddComponent <RectTransform>(); // default of 0, 1 mean pivot is at the window top-left corner // pivotX = 0 => left, pivotX = 1 => right // pivotY = 0 => bottom, pivotY = 1 => top TopTransform.pivot = new Vector2(0f, 0f); // distance in pixels between the pivot and the center of the screen TopTransform.anchoredPosition = new Vector2(0f, 0f); TopTransform.sizeDelta = new Vector2(KsmGuiStyle.tooltipMaxWidth, 0f); // max width of tooltip, text wrap will occur if larger. // set the parent canvas // render order of the various UI canvases (lower value = on top) // maincanvas => Z 750 // appCanvas => Z 625 // actionCanvas => Z 500 // screenMessageCanvas => Z 450 // dialogCanvas => Z 400 // dragDropcanvas => Z 333 // debugCanvas => Z 315 // tooltipCanvas => Z 300 TopTransform.SetParentFixScale(UIMasterController.Instance.tooltipCanvas.transform); tooltipObject.AddComponent <CanvasRenderer>(); VerticalLayoutGroup toplayout = tooltipObject.AddComponent <VerticalLayoutGroup>(); toplayout.childAlignment = TextAnchor.UpperLeft; toplayout.childControlHeight = true; toplayout.childControlWidth = true; toplayout.childForceExpandHeight = false; toplayout.childForceExpandWidth = false; ContentSizeFitter topFitter = tooltipObject.AddComponent <ContentSizeFitter>(); topFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained; topFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; // first child : 1px white border GameObject border = new GameObject("KsmGuiTooltipBorder"); ContentTransform = border.AddComponent <RectTransform>(); ContentTransform.SetParentFixScale(TopTransform); border.AddComponent <CanvasRenderer>(); VerticalLayoutGroup borderLayout = border.AddComponent <VerticalLayoutGroup>(); borderLayout.padding = new RectOffset(1, 1, 1, 1); borderLayout.childAlignment = TextAnchor.UpperLeft; borderLayout.childControlHeight = true; borderLayout.childControlWidth = true; borderLayout.childForceExpandHeight = false; borderLayout.childForceExpandWidth = false; Image borderImage = border.AddComponent <Image>(); borderImage.color = KsmGuiStyle.tooltipBorderColor; borderImage.raycastTarget = false; // 2nd child : black background GameObject background = new GameObject("KsmGuiTooltipBackground"); RectTransform backgroundTranform = background.AddComponent <RectTransform>(); backgroundTranform.SetParentFixScale(ContentTransform); background.AddComponent <CanvasRenderer>(); VerticalLayoutGroup backgroundLayout = background.AddComponent <VerticalLayoutGroup>(); backgroundLayout.padding = new RectOffset(5, 5, 2, 2); backgroundLayout.childAlignment = TextAnchor.UpperLeft; backgroundLayout.childControlHeight = true; backgroundLayout.childControlWidth = true; backgroundLayout.childForceExpandHeight = false; backgroundLayout.childForceExpandWidth = false; Image backgroundImage = background.AddComponent <Image>(); backgroundImage.color = KsmGuiStyle.tooltipBackgroundColor; backgroundImage.raycastTarget = false; // last child : text GameObject textObject = new GameObject("KsmGuiTooltipText"); RectTransform textTransform = textObject.AddComponent <RectTransform>(); textTransform.SetParentFixScale(backgroundTranform); textObject.AddComponent <CanvasRenderer>(); textComponent = textObject.AddComponent <TextMeshProUGUI>(); textComponent.raycastTarget = false; textComponent.color = KsmGuiStyle.textColor; textComponent.font = KsmGuiStyle.textFont; textComponent.fontSize = KsmGuiStyle.textSize; textComponent.alignment = TextAlignmentOptions.Top; tooltipObject.SetLayerRecursive(5); //KsmGuiBase.ApplyCanvasScalerScale(TopTransform); tooltipObject.SetActive(false); IsVisible = false; }
public KsmGuiWindow ( LayoutGroupType topLayout, bool destroyOnClose = true, float opacity = 0.8f, bool isDraggable = false, int dragOffset = 0, TextAnchor groupAlignment = TextAnchor.UpperLeft, float groupSpacing = 0f, TextAnchor screenAnchor = TextAnchor.MiddleCenter, TextAnchor windowPivot = TextAnchor.MiddleCenter, int posX = 0, int posY = 0 ) : base(null) { this.destroyOnClose = destroyOnClose; TopTransform.SetAnchorsAndPosition(screenAnchor, windowPivot, posX, posY); TopTransform.SetParentFixScale(KsmGuiMasterController.Instance.KsmGuiTransform); TopTransform.localScale = Vector3.one; // our custom lock manager InputLockManager = TopObject.AddComponent <KsmGuiInputLock>(); InputLockManager.rectTransform = TopTransform; // if draggable, add the stock dragpanel component IsDraggable = isDraggable; if (IsDraggable) { DragPanel = TopObject.AddComponent <DragPanel>(); DragPanel.edgeOffset = dragOffset; } Image img = TopObject.AddComponent <Image>(); img.sprite = Textures.KsmGuiSpriteBackground; img.type = Image.Type.Sliced; img.color = new Color(1.0f, 1.0f, 1.0f, opacity); SizeFitter = TopObject.AddComponent <ContentSizeFitter>(); SizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; SizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; if (topLayout == LayoutGroupType.Vertical) { LayoutGroup = TopObject.AddComponent <VerticalLayoutGroup>(); } else { LayoutGroup = TopObject.AddComponent <HorizontalLayoutGroup>(); } LayoutGroup.spacing = groupSpacing; LayoutGroup.padding = new RectOffset(5, 5, 5, 5); LayoutGroup.childControlHeight = true; LayoutGroup.childControlWidth = true; LayoutGroup.childForceExpandHeight = false; LayoutGroup.childForceExpandWidth = false; LayoutGroup.childAlignment = groupAlignment; // close on scene changes GameEvents.onGameSceneLoadRequested.Add(OnSceneChange); }