public override void OnInspectorGUI()
        {
            DrawDefaultInspector();
            RectTransformSnap myTarget = (RectTransformSnap)target;

            if (GUILayout.Button("Snap"))
            {
                myTarget.GetComponent <RectTransformSnap>().Snap();
            }
        }
Exemple #2
0
        public void Convert(bool noExitGUI = false)
        {
#if UNITY_EDITOR
            string flatRoundedSquare   = "Assets/MaterialUI/Images/RoundedSquare/roundedsquare_";
            string raisedRoundedSquare = "Assets/MaterialUI/Images/RoundedSquare_Stroke/roundedsquare_stroke_";

            string imagePath = "";

            if (!isCircularButton)
            {
                imagePath = isRaisedButton ? flatRoundedSquare : raisedRoundedSquare;
            }

            if (isRaisedButton)
            {
                DestroyImmediate(m_ShadowsCanvasGroup.gameObject);
                m_ShadowsCanvasGroup = null;

                if (materialShadow)
                {
                    DestroyImmediate(materialShadow);
                }

                if (materialRipple != null)
                {
                    materialRipple.highlightWhen = MaterialRipple.HighlightActive.Hovered;
                }
            }
            else
            {
                string path = isCircularButton ? pathToCirclePrefab : pathToRectPrefab;

                GameObject tempButton = Instantiate(AssetDatabase.LoadAssetAtPath <GameObject>(path));

                GameObject newShadow = tempButton.transform.Find("Shadows").gameObject;

                m_ShadowsCanvasGroup = newShadow.GetComponent <CanvasGroup>();

                RectTransform newShadowRectTransform = (RectTransform)newShadow.transform;

                newShadowRectTransform.SetParent(rectTransform);
                newShadowRectTransform.SetAsFirstSibling();
                newShadowRectTransform.localScale       = Vector3.one;
                newShadowRectTransform.localEulerAngles = Vector3.zero;

                RectTransform tempRectTransform = m_BackgroundImage != null
                    ? (RectTransform)m_BackgroundImage.transform
                    : rectTransform;

                if (isCircularButton)
                {
                    newShadowRectTransform.anchoredPosition = Vector2.zero;
                    RectTransformSnap newSnapper = newShadow.GetAddComponent <RectTransformSnap>();
                    newSnapper.sourceRectTransform = tempRectTransform;
                    newSnapper.valuesArePercentage = true;
                    newSnapper.snapWidth           = true;
                    newSnapper.snapHeight          = true;
                    newSnapper.snapEveryFrame      = true;
                    newSnapper.paddingPercent      = new Vector2(225, 225);
                    Vector3 tempVector3 = rectTransform.GetPositionRegardlessOfPivot();
                    tempVector3.y -= 1f;
                    newShadowRectTransform.position = tempVector3;
                }
                else
                {
                    newShadowRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tempRectTransform.GetProperSize().x + 54);
                    newShadowRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, tempRectTransform.GetProperSize().y + 54);
                    Vector3 tempVector3 = rectTransform.GetPositionRegardlessOfPivot();
                    newShadowRectTransform.position = tempVector3;
                }

                DestroyImmediate(tempButton);

                gameObject.AddComponent <MaterialShadow>();

                materialShadow.shadowsActiveWhen = MaterialShadow.ShadowsActive.Hovered;

                materialShadow.animatedShadows = newShadow.GetComponentsInChildren <AnimatedShadow>();

                materialShadow.isEnabled = true;

                if (materialRipple != null)
                {
                    materialRipple.highlightWhen = MaterialRipple.HighlightActive.Clicked;
                }
            }

            if (!isCircularButton)
            {
                SpriteSwapper spriteSwapper = GetComponent <SpriteSwapper>();

                if (spriteSwapper != null)
                {
                    spriteSwapper.sprite1X = AssetDatabase.LoadAssetAtPath <Sprite>(imagePath + "100%.png");
                    spriteSwapper.sprite2X = AssetDatabase.LoadAssetAtPath <Sprite>(imagePath + "200%.png");
                    spriteSwapper.sprite4X = AssetDatabase.LoadAssetAtPath <Sprite>(imagePath + "400%.png");
                }
                else
                {
                    if (m_BackgroundImage != null)
                    {
                        ((Image)m_BackgroundImage).sprite = AssetDatabase.LoadAssetAtPath <Sprite>(imagePath + "100%.png");
                    }
                }
            }
            else
            {
                if (!isRaisedButton)
                {
                    RectTransform tempRectTransform = (RectTransform) new GameObject("Stroke", typeof(VectorImage)).transform;

                    tempRectTransform.SetParent(m_BackgroundImage.rectTransform);
                    tempRectTransform.localScale       = Vector3.one;
                    tempRectTransform.localEulerAngles = Vector3.zero;
                    tempRectTransform.anchorMin        = Vector2.zero;
                    tempRectTransform.anchorMax        = Vector2.one;
                    tempRectTransform.anchoredPosition = Vector2.zero;
                    tempRectTransform.sizeDelta        = Vector2.zero;

                    VectorImage vectorImage = tempRectTransform.GetComponent <VectorImage>();
                    vectorImage.vectorImageData = MaterialUIIconHelper.GetIcon("circle_stroke_thin").vectorImageData;
                    vectorImage.sizeMode        = VectorImage.SizeMode.MatchMin;
                    vectorImage.color           = new Color(0f, 0f, 0f, 0.125f);

                    tempRectTransform.name = "Stroke";
                }
                else
                {
                    VectorImage[] images = backgroundImage.GetComponentsInChildren <VectorImage>();

                    for (int i = 0; i < images.Length; i++)
                    {
                        if (images[i].name == "Stroke")
                        {
                            DestroyImmediate(images[i].gameObject);
                        }
                    }
                }
            }

            name = isRaisedButton ? name.Replace("Raised", "Flat") : name.Replace("Flat", "Raised");

            if (m_BackgroundImage != null)
            {
                if (!isRaisedButton)
                {
                    if (m_BackgroundImage.color == Color.clear)
                    {
                        m_BackgroundImage.color = Color.white;
                    }
                }
                else
                {
                    if (m_BackgroundImage.color == Color.white)
                    {
                        m_BackgroundImage.color = Color.clear;
                    }
                }
            }

            m_IsRaisedButton = !m_IsRaisedButton;

            if (!noExitGUI)
            {
                GUIUtility.ExitGUI();
            }
#endif
        }