Skip to content

Instantly share code, notes, and snippets.

@takashi1975
Created March 4, 2017 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takashi1975/730143181eb8d1e6509eb04024e2c1a5 to your computer and use it in GitHub Desktop.
Save takashi1975/730143181eb8d1e6509eb04024e2c1a5 to your computer and use it in GitHub Desktop.
Unity-UGUI-Multi-image-button
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
/**
* Buttonの下にImageを追加した場合、Buttonを押した時の色を子供のImageにも追従させる
* 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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment