コード例 #1
0
        private static void BuildPersistantCanvas()
        {
            GadgetCore.CoreLogger.Log("Building Persistant Canvas");
            EventSystem eventSystem = new GameObject("EventSystem", typeof(EventSystem), typeof(StandaloneInputModule)).GetComponent <EventSystem>();

            UnityEngine.Object.DontDestroyOnLoad(eventSystem.gameObject);
            StandaloneInputModule inputModule = eventSystem.GetComponent <StandaloneInputModule>();

            inputModule.horizontalAxis = "Horizontal1";
            inputModule.verticalAxis   = "Vertical1";
            inputModule.submitButton   = "Jump";
            inputModule.cancelButton   = "Cancel";

            PersistantCanvas = new GameObject("Persistant Canvas", typeof(RectTransform), typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster), typeof(CanvasGroup)).GetComponent <Canvas>();
            UnityEngine.Object.DontDestroyOnLoad(PersistantCanvas.gameObject);
            PersistantCanvas.GetComponent <CanvasGroup>().alpha          = 1;
            PersistantCanvas.GetComponent <CanvasGroup>().interactable   = true;
            PersistantCanvas.GetComponent <CanvasGroup>().blocksRaycasts = true;
            PersistantCanvas.GetComponent <RectTransform>().pivot        = new Vector2(0.5f, 0.5f);
            PersistantCanvas.sortingOrder = 100;
            PersistantCanvas.renderMode   = RenderMode.ScreenSpaceOverlay;
            PersistantCanvas.pixelPerfect = true;
            CanvasScaler scaler = PersistantCanvas.GetComponent <CanvasScaler>();

            scaler.uiScaleMode            = CanvasScaler.ScaleMode.ConstantPixelSize;
            scaler.referencePixelsPerUnit = 100;

            GadgetConsole console = new GameObject("Console", typeof(RectTransform), typeof(GadgetConsole)).GetComponent <GadgetConsole>();

            console.gameObject.SetActive(false);
            console.transform.SetParent(PersistantCanvas.transform);
            console.GetComponent <RectTransform>().anchorMin = new Vector2(0f, 0f);
            console.GetComponent <RectTransform>().anchorMax = new Vector2(1f, 1f);
            console.GetComponent <RectTransform>().offsetMin = new Vector2(10f, 10f);
            console.GetComponent <RectTransform>().offsetMax = new Vector2(-10f, -10f);
            console.InputField = new GameObject("Input Field", typeof(RectTransform), typeof(InputField), typeof(CanvasRenderer), typeof(Image)).GetComponent <InputField>();
            console.InputField.GetComponent <RectTransform>().SetParent(console.transform);
            console.InputField.GetComponent <RectTransform>().anchorMin = new Vector2(0.1f, 0.1f);
            console.InputField.GetComponent <RectTransform>().anchorMax = new Vector2(0.9f, 0.1f);
            console.InputField.GetComponent <RectTransform>().offsetMin = new Vector2(0f, 0f);
            console.InputField.GetComponent <RectTransform>().offsetMax = new Vector2(0f, 50f);
            console.InputField.GetComponent <Image>().sprite            = BoxSprite;
            console.InputField.GetComponent <Image>().type       = Image.Type.Sliced;
            console.InputField.GetComponent <Image>().fillCenter = true;
            Text inputText = new GameObject("Text", typeof(RectTransform), typeof(CanvasRenderer), typeof(Text)).GetComponent <Text>();

            inputText.rectTransform.SetParent(console.InputField.transform);
            inputText.rectTransform.anchorMin = new Vector2(0f, 0f);
            inputText.rectTransform.anchorMax = new Vector2(1f, 1f);
            inputText.rectTransform.offsetMin = new Vector2(10f, 0f);
            inputText.rectTransform.offsetMax = new Vector2(-10f, 0f);
            inputText.alignment          = TextAnchor.MiddleLeft;
            inputText.horizontalOverflow = HorizontalWrapMode.Overflow;
            inputText.font     = ModMenuPanel.descText.font;
            inputText.fontSize = 24;
            console.InputField.textComponent = inputText;

            ScrollRect scrollableChat = new GameObject("Scrollable Chat", typeof(RectTransform), typeof(ScrollRect)).GetComponent <ScrollRect>();

            scrollableChat.GetComponent <RectTransform>().SetParent(console.transform);
            scrollableChat.GetComponent <RectTransform>().anchorMin = new Vector2(0.1f, 0.1f);
            scrollableChat.GetComponent <RectTransform>().anchorMax = new Vector2(0.9f, 1f);
            scrollableChat.GetComponent <RectTransform>().offsetMin = new Vector2(0f, 50f);
            scrollableChat.GetComponent <RectTransform>().offsetMax = new Vector2(0f, 0f);
            scrollableChat.GetComponent <RectTransform>().pivot     = new Vector2(0.5f, 0.5f);
            scrollableChat.inertia                     = false;
            scrollableChat.horizontal                  = false;
            scrollableChat.scrollSensitivity           = 5;
            scrollableChat.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
            scrollableChat.movementType                = ScrollRect.MovementType.Clamped;
            Scrollbar chatScrollBar = new GameObject("Scrollbar", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image), typeof(Scrollbar)).GetComponent <Scrollbar>();

            chatScrollBar.GetComponent <RectTransform>().SetParent(console.transform);
            chatScrollBar.GetComponent <RectTransform>().anchorMin = new Vector2(1f, 0.1f);
            chatScrollBar.GetComponent <RectTransform>().anchorMax = new Vector2(1f, 1f);
            chatScrollBar.GetComponent <RectTransform>().offsetMin = new Vector2(-50f, 0f);
            chatScrollBar.GetComponent <RectTransform>().offsetMax = new Vector2(0f, 0f);
            chatScrollBar.GetComponent <Image>().sprite            = BoxSprite;
            chatScrollBar.GetComponent <Image>().type       = Image.Type.Sliced;
            chatScrollBar.GetComponent <Image>().fillCenter = true;
            RectTransform charScrollBarHandle = new GameObject("Handle", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image)).GetComponent <RectTransform>();

            charScrollBarHandle.SetParent(chatScrollBar.transform);
            charScrollBarHandle.anchorMin = new Vector2(0.05f, 0.05f);
            charScrollBarHandle.anchorMax = new Vector2(0.95f, 0.95f);
            charScrollBarHandle.offsetMin = Vector2.zero;
            charScrollBarHandle.offsetMax = Vector2.zero;
            charScrollBarHandle.GetComponent <Image>().sprite     = BoxSprite;
            charScrollBarHandle.GetComponent <Image>().type       = Image.Type.Sliced;
            charScrollBarHandle.GetComponent <Image>().fillCenter = true;
            chatScrollBar.targetGraphic = charScrollBarHandle.GetComponent <Image>();
            chatScrollBar.handleRect    = charScrollBarHandle;
            chatScrollBar.direction     = Scrollbar.Direction.BottomToTop;
            Mask chatViewport = new GameObject("Viewport", typeof(RectTransform), typeof(Mask), typeof(CanvasRenderer), typeof(Image)).GetComponent <Mask>();

            chatViewport.rectTransform.SetParent(scrollableChat.transform);
            chatViewport.rectTransform.anchorMin           = new Vector2(0f, 0f);
            chatViewport.rectTransform.anchorMax           = new Vector2(1f, 1f);
            chatViewport.rectTransform.offsetMin           = new Vector2(0f, 0f);
            chatViewport.rectTransform.offsetMax           = new Vector2(0f, 0f);
            chatViewport.rectTransform.pivot               = Vector2.zero;
            chatViewport.showMaskGraphic                   = false;
            chatViewport.GetComponent <Image>().sprite     = BoxSprite;
            chatViewport.GetComponent <Image>().type       = Image.Type.Sliced;
            chatViewport.GetComponent <Image>().fillCenter = true;
            console.TextPanel = new GameObject("Content", typeof(RectTransform), typeof(VerticalLayoutGroup), typeof(ContentSizeFitter)).GetComponent <RectTransform>();
            console.TextPanel.SetParent(chatViewport.rectTransform);
            console.TextPanel.anchorMin = new Vector2(0f, 0f);
            console.TextPanel.anchorMax = new Vector2(1f, 1f);
            console.TextPanel.offsetMin = new Vector2(0f, 0f);
            console.TextPanel.offsetMax = new Vector2(0f, 0f);
            console.TextPanel.pivot     = Vector2.zero;
            console.TextPanel.GetComponent <VerticalLayoutGroup>().padding                = new RectOffset(10, 10, 10, 10);
            console.TextPanel.GetComponent <VerticalLayoutGroup>().spacing                = 10;
            console.TextPanel.GetComponent <VerticalLayoutGroup>().childAlignment         = TextAnchor.LowerLeft;
            console.TextPanel.GetComponent <VerticalLayoutGroup>().childControlWidth      = true;
            console.TextPanel.GetComponent <VerticalLayoutGroup>().childControlHeight     = true;
            console.TextPanel.GetComponent <VerticalLayoutGroup>().childForceExpandWidth  = true;
            console.TextPanel.GetComponent <VerticalLayoutGroup>().childForceExpandHeight = true;
            console.TextPanel.GetComponent <ContentSizeFitter>().horizontalFit            = ContentSizeFitter.FitMode.Unconstrained;
            console.TextPanel.GetComponent <ContentSizeFitter>().verticalFit              = ContentSizeFitter.FitMode.PreferredSize;
            scrollableChat.verticalScrollbar = chatScrollBar;
            scrollableChat.viewport          = chatViewport.rectTransform;
            scrollableChat.content           = console.TextPanel;
            console.AlwaysActivePanel        = new GameObject("Always Active Content", typeof(RectTransform), typeof(VerticalLayoutGroup), typeof(ContentSizeFitter)).GetComponent <RectTransform>();
            console.AlwaysActivePanel.SetParent(PersistantCanvas.transform);
            console.AlwaysActivePanel.anchorMin = new Vector2(0.1f, 0.1f);
            console.AlwaysActivePanel.anchorMax = new Vector2(0.9f, 1f);
            console.AlwaysActivePanel.offsetMin = new Vector2(10f, 60f);
            console.AlwaysActivePanel.offsetMax = new Vector2(-10f, -10f);
            console.AlwaysActivePanel.pivot     = Vector2.zero;
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().padding                = new RectOffset(10, 10, 10, 10);
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().spacing                = 10;
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().childAlignment         = TextAnchor.LowerLeft;
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().childControlWidth      = true;
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().childControlHeight     = true;
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().childForceExpandWidth  = true;
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().childForceExpandHeight = true;
            console.AlwaysActivePanel.GetComponent <ContentSizeFitter>().horizontalFit            = ContentSizeFitter.FitMode.Unconstrained;
            console.AlwaysActivePanel.GetComponent <ContentSizeFitter>().verticalFit              = ContentSizeFitter.FitMode.PreferredSize;

            GadgetConsole.PrintQueuedMessages();

            ConfirmationDialog = new GameObject("Confirmation Dialog", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image));
            ConfirmationDialog.GetComponent <RectTransform>().SetParent(PersistantCanvas.transform);
            ConfirmationDialog.GetComponent <RectTransform>().anchorMin = new Vector2(0.25f, 0.25f);
            ConfirmationDialog.GetComponent <RectTransform>().anchorMax = new Vector2(0.75f, 0.75f);
            ConfirmationDialog.GetComponent <RectTransform>().offsetMin = new Vector2(10, 10);
            ConfirmationDialog.GetComponent <RectTransform>().offsetMax = new Vector2(-10, -10);
            ConfirmationDialog.GetComponent <Image>().sprite            = BoxSprite;
            ConfirmationDialog.GetComponent <Image>().type       = Image.Type.Sliced;
            ConfirmationDialog.GetComponent <Image>().fillCenter = true;
            ConfirmationDialog.SetActive(false);
            Button yesButton = new GameObject("Yes Button", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image), typeof(Button)).GetComponent <Button>();

            yesButton.GetComponent <RectTransform>().SetParent(ConfirmationDialog.transform);
            yesButton.GetComponent <RectTransform>().anchorMin = new Vector2(0f, 0f);
            yesButton.GetComponent <RectTransform>().anchorMax = new Vector2(0.5f, 0.25f);
            yesButton.GetComponent <RectTransform>().offsetMin = new Vector2(10, 10);
            yesButton.GetComponent <RectTransform>().offsetMax = new Vector2(-10, -10);
            yesButton.GetComponent <Image>().sprite            = BoxSprite;
            yesButton.GetComponent <Image>().type       = Image.Type.Sliced;
            yesButton.GetComponent <Image>().fillCenter = true;
            yesButton.targetGraphic = yesButton.GetComponent <Image>();
            ConfirmationYesText     = new GameObject("Text", typeof(RectTransform), typeof(CanvasRenderer), typeof(Text)).GetComponent <Text>();
            ConfirmationYesText.rectTransform.SetParent(yesButton.transform);
            ConfirmationYesText.rectTransform.anchorMin = new Vector2(0f, 0f);
            ConfirmationYesText.rectTransform.anchorMax = new Vector2(1f, 1f);
            ConfirmationYesText.rectTransform.offsetMin = Vector2.zero;
            ConfirmationYesText.rectTransform.offsetMax = Vector2.zero;
            ConfirmationYesText.alignment = TextAnchor.MiddleCenter;
            ConfirmationYesText.font      = ModMenuPanel.descText.font;
            ConfirmationYesText.fontSize  = 24;
            ConfirmationYesText.text      = "Yes";
            Button noButton = new GameObject("No Button", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image), typeof(Button)).GetComponent <Button>();

            noButton.GetComponent <RectTransform>().SetParent(ConfirmationDialog.transform);
            noButton.GetComponent <RectTransform>().anchorMin = new Vector2(0.5f, 0f);
            noButton.GetComponent <RectTransform>().anchorMax = new Vector2(1f, 0.25f);
            noButton.GetComponent <RectTransform>().offsetMin = new Vector2(10, 10);
            noButton.GetComponent <RectTransform>().offsetMax = new Vector2(-10, -10);
            noButton.GetComponent <Image>().sprite            = BoxSprite;
            noButton.GetComponent <Image>().type       = Image.Type.Sliced;
            noButton.GetComponent <Image>().fillCenter = true;
            noButton.targetGraphic = noButton.GetComponent <Image>();
            ConfirmationNoText     = new GameObject("Text", typeof(RectTransform), typeof(CanvasRenderer), typeof(Text)).GetComponent <Text>();
            ConfirmationNoText.rectTransform.SetParent(noButton.transform);
            ConfirmationNoText.rectTransform.anchorMin = new Vector2(0f, 0f);
            ConfirmationNoText.rectTransform.anchorMax = new Vector2(1f, 1f);
            ConfirmationNoText.rectTransform.offsetMin = Vector2.zero;
            ConfirmationNoText.rectTransform.offsetMax = Vector2.zero;
            ConfirmationNoText.alignment = TextAnchor.MiddleCenter;
            ConfirmationNoText.font      = ModMenuPanel.descText.font;
            ConfirmationNoText.fontSize  = 24;
            ConfirmationNoText.text      = "No";
            yesButton.onClick.AddListener(() => {
                GadgetCoreAPI.CloseDialog();
                ConfirmationYesAction?.Invoke();
            });
            noButton.onClick.AddListener(() => {
                GadgetCoreAPI.CloseDialog();
                ConfirmationNoAction?.Invoke();
            });
            ConfirmationText = new GameObject("Text", typeof(RectTransform), typeof(CanvasRenderer), typeof(Text)).GetComponent <Text>();
            ConfirmationText.rectTransform.SetParent(ConfirmationDialog.transform);
            ConfirmationText.rectTransform.anchorMin = new Vector2(0f, 0.25f);
            ConfirmationText.rectTransform.anchorMax = new Vector2(1f, 1f);
            ConfirmationText.rectTransform.offsetMin = Vector2.zero;
            ConfirmationText.rectTransform.offsetMax = Vector2.zero;
            ConfirmationText.alignment = TextAnchor.MiddleCenter;
            ConfirmationText.font      = ModMenuPanel.descText.font;
            ConfirmationText.fontSize  = 24;
            ConfirmationText.text      = "";
        }