Unity 버튼에 이미지가 추가되었지만 눌렀을 때의 색을 좋아하지 않습니다...

10357 단어 imagebuttonUnity

버튼에 추가된 이미지 색상은 클릭 시 변경되지 않습니다.


Unity를 사용하여 단추에 이미지를 추가했지만 단추를 눌렀을 때 추가된 이미지의 색깔은 변하지 않습니다...
자꾸 사진이 떠오르는 것 같아...

이거 곤란한데...그리고 불평을 해봤어요...
UGUI; Multi image button transition
http://answers.unity3d.com/questions/820311/ugui-multi-image-button-transition.html
그렇구나, 이거 괜찮아 보이네...이번에는 끼워 넣은 내용을 기록해 보았다.

MultiImageButton.cs 만들기


먼저, MultimageButon.cs를 만들었지만, Graphics 속성에 대입된 m_graphics 표현식을 변경했습니다. (단추 자체의 아이를 지정합니다.)
MultiImageButton.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;
using UnityEngine.EventSystems;


/**
 * 参考にした URL
 * http://answers.unity3d.com/questions/820311/ugui-multi-image-button-transition.html
 */
public class MultiImageButton : Button
{
    private Graphic[] m_graphics;
    protected Graphic[] Graphics
    {
        get
        {
            if (this.m_graphics == null)
            {
                //変更点 ボタンオブジェクト以下の子オブジェクトを対象としました。
                this.m_graphics = targetGraphic.transform.GetComponentsInChildren<Graphic>();
            }

            return this.m_graphics;
        }
    }


    protected override void DoStateTransition (SelectionState state, bool instant)
    {
        Color color;
        switch (state)
        {
            case (Selectable.SelectionState.Normal):
                color = this.colors.normalColor;
                break;

            case (Selectable.SelectionState.Highlighted):
                color = this.colors.highlightedColor;
                break;

            case (Selectable.SelectionState.Pressed):
                color = this.colors.pressedColor;
                break;

            case (Selectable.SelectionState.Disabled):
                color = this.colors.disabledColor;
                break;

            default:
                color = Color.black;
                break;
        }

        if (base.gameObject.activeInHierarchy)
        {
            switch (this.transition)
            {
                case (Selectable.Transition.ColorTint):
                    this.ColorTween(color * this.colors.colorMultiplier, instant);
                    break;

                default:
                    throw new System.NotSupportedException();
            }
        }
    }


    private void ColorTween(Color targetColor, bool instant)
    {
        if (this.targetGraphic == null)
        {
            return;
        }

        foreach (var g in this.Graphics)
        {
            g.CrossFadeColor(targetColor, (!instant) ? this.colors.fadeDuration : 0f, true, true);
        }
    }
}

버튼 스크립트 바꾸기


버튼의 Button(Script)을 MultiButton(Script)으로 바꿉니다.
버튼에서 먼저 Buton(Script) 구성 요소를 제거합니다..

버튼의 Add Component에 Multi Image Button(Script) 구성 요소가 추가되었습니다.

추가된 Multi Image Buton(Script) 설정은 이전과 동일하게 설정할 수 있습니다.

동작 확인


디버깅을 해보면 밀었을 때의 불협화감이 사라진다.
(예라면 이해하기 어려운데...)

좋은 웹페이지 즐겨찾기