コード例 #1
0
 private void Awake()
 {
     if (Instance == null)
     {
         Instance        = this;
         Instance.Active = false;
         DontDestroyOnLoad(gameObject);
     }
     else
     {
         Destroy(gameObject);
     }
 }
コード例 #2
0
    public static void Init()
    {
        if (UnityCommandConsole.Instance != null)
        {
            return;
        }

        //  GameObject
        GameObject go     = new GameObject("UnityCommandConsoleObject", typeof(UnityCommandConsole));
        GameObject canvas = CreateConsoleGui();

        UnityCommandConsole.SetParentAndAlign(canvas, go);

        UnityCommandConsole.Instance = go.GetComponent <UnityCommandConsole>();

        Instance.CommandsList = new List <Command>();
        Instance.Commands     = new Dictionary <string, CommandDelegate>();
        Instance.Hotkeys      = new Dictionary <KeyCode, CommandDelegate>();

        AddCommands();
    }
コード例 #3
0
    private static GameObject CreateConsoleGui()
    {
        //  Canvas
        GameObject canvas = new GameObject("UCC_Canvas");

        UnityCommandConsole.Instance.consoleCanvas              = canvas.AddComponent <Canvas>();
        UnityCommandConsole.Instance.consoleCanvas.renderMode   = RenderMode.ScreenSpaceOverlay;
        UnityCommandConsole.Instance.consoleCanvas.sortingOrder = 32767;

        UnityCommandConsole.Instance.consoleCanvas.gameObject.AddComponent <GraphicRaycaster>();

        CanvasScaler canvasScaler = canvas.AddComponent <CanvasScaler>();

        canvasScaler.uiScaleMode         = CanvasScaler.ScaleMode.ScaleWithScreenSize;
        canvasScaler.screenMatchMode     = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
        canvasScaler.referenceResolution = new Vector2(800f, 600f);

        UnityCommandConsole.Instance.consoleCanvas.gameObject.AddComponent <GraphicRaycaster>();

        //  Panel
        Instance.consolePanel = UnityCommandConsole.CreatePanel(GetStandardResources());

        RectTransform panelRT = Instance.consolePanel.GetComponent <RectTransform>();

        panelRT.anchorMin        = new Vector2(0f, 0f);
        panelRT.anchorMax        = new Vector2(1f, 0.3f);
        panelRT.anchoredPosition = new Vector2(0f, 0f);
        panelRT.sizeDelta        = new Vector2(0f, 0f);

        Image panelImage = Instance.consolePanel.GetComponent <Image>();

        panelImage.color = new Color(0f, 0f, 0f, 0.4f);

        UnityCommandConsole.SetParentAndAlign(Instance.consolePanel, canvas);

        //  ScrollView
        GameObject scrollview = UnityCommandConsole.CreateScrollView(GetStandardResources());

        ScrollRect scrollviewSR = scrollview.GetComponent <ScrollRect>();

        scrollviewSR.movementType = ScrollRect.MovementType.Clamped;

        RectTransform scrollviewRT = scrollview.GetComponent <RectTransform>();

        scrollviewRT.anchorMin        = new Vector2(0f, 0f);
        scrollviewRT.anchorMax        = new Vector2(1f, 1f);
        scrollviewRT.sizeDelta        = new Vector2(-15f, -41f);
        scrollviewRT.localPosition    = new Vector3(0f, 11.5f, 0f);
        scrollviewRT.anchoredPosition = new Vector2(0f, 11.5f);

        Image scrollviewImage = scrollview.GetComponent <Image>();

        scrollviewImage.color = new Color(0f, 0f, 0f, 0.43f);

        // remove horizontal scrollbar
        GameObject.Destroy(GameObject.Find("UCC_Scrollbar_Horizontal"));

        // add Text to scrollview content
        GameObject scrollViewContent = GameObject.Find("UCC_Content");

        RectTransform scrollViewContentRT = scrollViewContent.GetComponent <RectTransform>();

        scrollViewContentRT.pivot = new Vector2(0f, 0f);

        GameObject scrollViewContentText = UnityCommandConsole.CreateText(GetStandardResources());

        UnityCommandConsole.Instance.consoleText                    = scrollViewContentText.GetComponent <Text>();
        UnityCommandConsole.Instance.consoleText.text               = "DEBUG CONSOLE\n------------------------\ntype 'help' to view available commands";
        UnityCommandConsole.Instance.consoleText.color              = new Color(1f, 1f, 1f, 0.90f);
        UnityCommandConsole.Instance.consoleText.fontSize           = 11;
        UnityCommandConsole.Instance.consoleText.horizontalOverflow = HorizontalWrapMode.Wrap;
        UnityCommandConsole.Instance.consoleText.verticalOverflow   = VerticalWrapMode.Overflow;
        UnityCommandConsole.Instance.consoleText.alignment          = TextAnchor.LowerLeft;

        UnityCommandConsole.SetParentAndAlign(scrollViewContentText, scrollViewContent);

        VerticalLayoutGroup verticalLayoutGroup = scrollViewContent.AddComponent <VerticalLayoutGroup>();

        verticalLayoutGroup.childForceExpandHeight = false;
        verticalLayoutGroup.childControlHeight     = true;
        verticalLayoutGroup.childAlignment         = TextAnchor.LowerLeft;
        verticalLayoutGroup.padding.left           = 5;
        verticalLayoutGroup.padding.right          = 5;
        verticalLayoutGroup.padding.top            = 0;
        verticalLayoutGroup.padding.bottom         = 8;

        ContentSizeFitter contentSizeFitter = scrollViewContent.AddComponent <ContentSizeFitter>();

        contentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
        contentSizeFitter.verticalFit   = ContentSizeFitter.FitMode.PreferredSize;

        UnityCommandConsole.SetParentAndAlign(scrollview, Instance.consolePanel);


        //  InputField
        GameObject inputField = UnityCommandConsole.CreateInputField(GetStandardResources());

        UnityCommandConsole.Instance.consoleInputField = inputField.GetComponent <InputField>();

        RectTransform inputFieldRT = inputField.GetComponent <RectTransform>();

        inputFieldRT.anchorMin        = new Vector2(0.275f, 0f);
        inputFieldRT.anchorMax        = new Vector2(0.725f, 0f);
        inputFieldRT.sizeDelta        = new Vector2(425f, 23f);
        inputFieldRT.localPosition    = new Vector3(0f, -90f, 0f);
        inputFieldRT.anchoredPosition = new Vector2(0f, 18f);

        Image inputFieldImage = inputField.GetComponent <Image>();

        inputFieldImage.color = new Color(0f, 0f, 0f, 0.43f);

        UnityCommandConsole.Instance.inputText           = UnityCommandConsole.Instance.consoleInputField.gameObject.transform.Find("UCC_Text").gameObject.GetComponent <Text>();
        UnityCommandConsole.Instance.inputText.fontSize  = 10;
        UnityCommandConsole.Instance.inputText.alignment = TextAnchor.MiddleLeft;
        UnityCommandConsole.Instance.inputText.color     = new Color(1f, 1f, 1f);

        RectTransform inputTextRT = UnityCommandConsole.Instance.inputText.gameObject.GetComponent <RectTransform>();

        inputTextRT.localPosition    = new Vector3(0f, 1.1f);
        inputTextRT.anchorMin        = new Vector2(0f, 0f);
        inputTextRT.anchorMax        = new Vector2(1f, 1f);
        inputTextRT.anchoredPosition = new Vector2(0f, 1.1f);
        inputTextRT.sizeDelta        = new Vector2(-11.42f, -9.42f);

        Text inputFieldPlaceholder = UnityCommandConsole.Instance.consoleInputField.gameObject.transform.Find("UCC_Placeholder").gameObject.GetComponent <Text>();

        inputFieldPlaceholder.text     = "ENTER A COMMAND";
        inputFieldPlaceholder.color    = new Color(1f, 1f, 1f, 0.90f);
        inputFieldPlaceholder.fontSize = 10;

        UnityCommandConsole.SetParentAndAlign(inputField, Instance.consolePanel);

        return(canvas);
    }
コード例 #4
0
 // Update is called once per frame
 void Update()
 {
     UnityCommandConsole.Init();
 }