예제 #1
0
    public override void OnInspectorGUI()
    {
        EditorGUIUtility.LookLikeControls(80f);
        mButton = target as NGUIImageButton;
        mSprite = EditorGUILayout.ObjectField("Sprite", mButton.target, typeof(NGUISprite), true) as NGUISprite;

        if (mButton.target != mSprite)
        {
            NGUIEditorTools.RegisterUndo("Image Button Change", mButton);
            mButton.target = mSprite;
            if (mSprite != null)
            {
                mSprite.spriteName = mButton.normalSprite;
            }
        }

        if (mSprite != null)
        {
            ComponentSelector.Draw <NGUIAtlas>(mSprite.atlas, OnSelectAtlas);

            if (mSprite.atlas != null)
            {
                NGUIEditorTools.SpriteField("Normal", mSprite.atlas, mButton.normalSprite, OnNormal);
                NGUIEditorTools.SpriteField("Hover", mSprite.atlas, mButton.hoverSprite, OnHover);
                NGUIEditorTools.SpriteField("Pressed", mSprite.atlas, mButton.pressedSprite, OnPressed);
                NGUIEditorTools.SpriteField("Disabled", mSprite.atlas, mButton.disabledSprite, OnDisabled);
            }
        }
    }
    public override void OnInspectorGUI()
    {
        EditorGUIUtility.LookLikeControls(80f);
        mButton = target as NGUIImageButton;
        mSprite = EditorGUILayout.ObjectField("Sprite", mButton.target, typeof(NGUISprite), true) as NGUISprite;

        if (mButton.target != mSprite)
        {
            NGUIEditorTools.RegisterUndo("Image Button Change", mButton);
            mButton.target = mSprite;
            if (mSprite != null) mSprite.spriteName = mButton.normalSprite;
        }

        if (mSprite != null)
        {
            ComponentSelector.Draw<NGUIAtlas>(mSprite.atlas, OnSelectAtlas);

            if (mSprite.atlas != null)
            {
                NGUIEditorTools.SpriteField("Normal", mSprite.atlas, mButton.normalSprite, OnNormal);
                NGUIEditorTools.SpriteField("Hover", mSprite.atlas, mButton.hoverSprite, OnHover);
                NGUIEditorTools.SpriteField("Pressed", mSprite.atlas, mButton.pressedSprite, OnPressed);
                NGUIEditorTools.SpriteField("Disabled", mSprite.atlas, mButton.disabledSprite, OnDisabled);
            }
        }
    }
    /// <summary>
    /// Button creation function.
    /// </summary>

    void CreateImageButton(GameObject go)
    {
        if (NGUISettings.atlas != null)
        {
            NGUIEditorTools.SpriteField("Normal", "Normal state sprite", NGUISettings.atlas, mImage0, OnImage0);
            NGUIEditorTools.SpriteField("Hover", "Hover state sprite", NGUISettings.atlas, mImage1, OnImage1);
            NGUIEditorTools.SpriteField("Pressed", "Pressed state sprite", NGUISettings.atlas, mImage2, OnImage2);
            NGUIEditorTools.SpriteField("Disabled", "Disabled state sprite", NGUISettings.atlas, mImage3, OnImage3);
        }

        if (ShouldCreate(go, NGUISettings.atlas != null))
        {
            int depth = NGUITools.CalculateNextDepth(go);
            go      = NGUITools.AddChild(go);
            go.name = "Image Button";

            NGUIAtlas.Sprite sp     = NGUISettings.atlas.GetSprite(mImage0);
            NGUISprite       sprite = NGUITools.AddWidget <NGUISprite>(go);
            sprite.type                 = (sp.inner == sp.outer) ? NGUISprite.Type.Simple : NGUISprite.Type.Sliced;
            sprite.name                 = "Background";
            sprite.depth                = depth;
            sprite.atlas                = NGUISettings.atlas;
            sprite.spriteName           = mImage0;
            sprite.transform.localScale = new Vector3(150f, 40f, 1f);
            sprite.MakePixelPerfect();

            if (NGUISettings.font != null)
            {
                NGUILabel lbl = NGUITools.AddWidget <NGUILabel>(go);
                lbl.font = NGUISettings.font;
                lbl.text = go.name;
                lbl.MakePixelPerfect();
            }

            // Add a collider
            NGUITools.AddWidgetCollider(go);

            // Add the scripts
            NGUIImageButton ib = go.AddComponent <NGUIImageButton>();
            ib.target        = sprite;
            ib.normalSprite  = mImage0;
            ib.hoverSprite   = mImage1;
            ib.pressedSprite = mImage2;
            go.AddComponent <NGUIButtonSound>();

            Selection.activeGameObject = go;
        }
    }