コード例 #1
0
ファイル: PUIElements.cs プロジェクト: peterhaneve/ONIMods
        /// <summary>
        /// Creates a UI game object.
        /// </summary>
        /// <param name="parent">The parent of the UI object. If not set now, or added/changed
        /// later, the anchors must be redefined.</param>
        /// <param name="name">The object name.</param>
        /// <param name="canvas">true to add a canvas renderer, or false otherwise.</param>
        /// <param name="horizAnchor">How to anchor the object horizontally.</param>
        /// <param name="vertAnchor">How to anchor the object vertically.</param>
        /// <returns>The UI object with transform and canvas initialized.</returns>
        public static GameObject CreateUI(GameObject parent, string name, bool canvas = true,
                                          PUIAnchoring horizAnchor = PUIAnchoring.Stretch,
                                          PUIAnchoring vertAnchor  = PUIAnchoring.Stretch)
        {
            var element = new GameObject(name);

            if (parent != null)
            {
                element.SetParent(parent);
            }
            // Size and position
            var transform = element.AddOrGet <RectTransform>();

            transform.localScale = Vector3.one;
            SetAnchors(element, horizAnchor, vertAnchor);
            // Almost all UI components need a canvas renderer for some reason
            if (canvas)
            {
                element.AddComponent <CanvasRenderer>();
            }
            element.layer = LayerMask.NameToLayer("UI");
            return(element);
        }
コード例 #2
0
ファイル: PUIElements.cs プロジェクト: peterhaneve/ONIMods
        /// <summary>
        /// Sets the anchor location of a UI element. The offsets will be reset, use
        /// SetAnchorOffsets to adjust the offset from the new anchor locations.
        /// </summary>
        /// <param name="uiElement">The UI element to modify.</param>
        /// <param name="horizAnchor">The horizontal anchor mode.</param>
        /// <param name="vertAnchor">The vertical anchor mode.</param>
        /// <returns>The UI element, for call chaining.</returns>
        public static GameObject SetAnchors(GameObject uiElement, PUIAnchoring horizAnchor,
                                            PUIAnchoring vertAnchor)
        {
            Vector2 aMax = new Vector2(), aMin = new Vector2(), pivot = new Vector2();

            if (uiElement == null)
            {
                throw new ArgumentNullException(nameof(uiElement));
            }
            var transform = uiElement.rectTransform();

            // Anchor: horizontal
            switch (horizAnchor)
            {
            case PUIAnchoring.Center:
                aMin.x  = 0.5f;
                aMax.x  = 0.5f;
                pivot.x = 0.5f;
                break;

            case PUIAnchoring.End:
                aMin.x  = 1.0f;
                aMax.x  = 1.0f;
                pivot.x = 1.0f;
                break;

            case PUIAnchoring.Stretch:
                aMin.x  = 0.0f;
                aMax.x  = 1.0f;
                pivot.x = 0.5f;
                break;

            default:
                aMin.x  = 0.0f;
                aMax.x  = 0.0f;
                pivot.x = 0.0f;
                break;
            }
            // Anchor: vertical
            switch (vertAnchor)
            {
            case PUIAnchoring.Center:
                aMin.y  = 0.5f;
                aMax.y  = 0.5f;
                pivot.y = 0.5f;
                break;

            case PUIAnchoring.End:
                aMin.y  = 1.0f;
                aMax.y  = 1.0f;
                pivot.y = 1.0f;
                break;

            case PUIAnchoring.Stretch:
                aMin.y  = 0.0f;
                aMax.y  = 1.0f;
                pivot.y = 0.5f;
                break;

            default:
                aMin.y  = 0.0f;
                aMax.y  = 0.0f;
                pivot.y = 0.0f;
                break;
            }
            transform.anchorMax        = aMax;
            transform.anchorMin        = aMin;
            transform.pivot            = pivot;
            transform.anchoredPosition = Vector2.zero;
            transform.offsetMax        = Vector2.zero;
            transform.offsetMin        = Vector2.zero;
            return(uiElement);
        }