示例#1
0
        private static void CreateComponent(MenuCommand menuCommand)
        {
            var go = new GameObject(MenuUtils.UIToggle_GameObject_Name, typeof(RectTransform), typeof(Toggle), typeof(UIToggle));

            GameObjectUtility.SetParentAndAlign(go, GetParent(menuCommand.context as GameObject));
            Undo.RegisterCreatedObjectUndo(go, "Create " + go.name); //undo option
            var uiToggle = go.GetComponent <UIToggle>();

            uiToggle.RectTransform.Center(true);
            uiToggle.RectTransform.sizeDelta = new Vector2(UIToggleSettings.DEFAULT_BUTTON_WIDTH, UIToggleSettings.DEFAULT_BUTTON_HEIGHT);

            GameObject background = DoozyUtils.CreateBackgroundImage(go);
            var        backgroundRectTransform = background.GetComponent <RectTransform>();

            backgroundRectTransform.anchorMin        = new Vector2(0, 1);
            backgroundRectTransform.anchorMax        = new Vector2(0, 1);
            backgroundRectTransform.anchoredPosition = new Vector2(10, -10);
            backgroundRectTransform.sizeDelta        = new Vector2(20, 20);
            var backgroundImage = background.GetComponent <Image>();

            backgroundImage.sprite     = UnityResources.UISprite;
            backgroundImage.type       = Image.Type.Sliced;
            backgroundImage.fillCenter = true;

            var checkmark = new GameObject("Checkmark", typeof(RectTransform), typeof(Image));

            GameObjectUtility.SetParentAndAlign(checkmark, background);
            var checkmarkRectTransform = checkmark.GetComponent <RectTransform>();

            checkmarkRectTransform.ResetLocalScaleToOne();
            checkmarkRectTransform.AnchorMinToZero();
            checkmarkRectTransform.AnchorMaxToOne();
            checkmarkRectTransform.anchoredPosition = new Vector2(0, 0);
            checkmarkRectTransform.SizeDeltaToZero();
            checkmarkRectTransform.CenterPivot();
            var checkmarkImage = checkmark.GetComponent <Image>();

            checkmarkImage.sprite     = UnityResources.Checkmark;
            checkmarkImage.type       = Image.Type.Simple;
            checkmarkImage.fillCenter = true;
            checkmarkImage.color      = DoozyUtils.CheckmarkColor;

#if dUI_TextMeshPro
            CreateTextMeshProLabel(uiToggle);
#else
            CreateTextLabel(uiToggle);
#endif

            uiToggle.Toggle.interactable  = true;
            uiToggle.Toggle.transition    = Selectable.Transition.ColorTint;
            uiToggle.Toggle.targetGraphic = backgroundImage;
            uiToggle.Toggle.graphic       = checkmarkImage;
            uiToggle.Toggle.isOn          = true;

            Selection.activeObject = go; //select the newly created gameObject
        }