动态修改按钮色块,达到保留选中效果。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Demo: MonoBehaviour
{
private Button _frontBtn;
private Button _backBtn;
public ColorBlock NormalColorBlock;
public ColorBlock chooseColorBlock;
// Start is called before the first frame update
void Start()
{
_frontBtn = transform.Find("FrontBtn").GetComponent<Button>();
_backBtn = transform.Find("BackBtn").GetComponent<Button>();
_frontBtn.onClick.AddListener(OnFrontBtn);
_backBtn.onClick.AddListener(OnBackBtn);
}
public void OnBackBtn()
{
backSelected = true;
frontSelected = false;
FrontBtnNormalUI();
BackBtnChooseUI();
FocusCabinetMnr.instance.FocusBack();
}
public void OnFrontBtn()
{
backSelected = false;
frontSelected = true;
FrontBtnChooseUI();
BackBtnNormalUI();
}
public void SetButtonsInteractableState(bool flag)
{
_returnBtn.interactable = flag;
_frontBtn.interactable = flag;
_backBtn.interactable = flag;
}
#region 按钮颜色表现
public void FrontBtnChooseUI()
{
_frontBtn.colors = new ColorBlock
{
normalColor = chooseColorBlock.normalColor,
highlightedColor = chooseColorBlock.highlightedColor,
pressedColor = chooseColorBlock.pressedColor,
selectedColor = chooseColorBlock.selectedColor,
disabledColor = chooseColorBlock.disabledColor,
colorMultiplier = 1.0f,
fadeDuration = 0.1f
};
}
private void FrontBtnNormalUI()
{
_frontBtn.colors = new ColorBlock
{
normalColor = NormalColorBlock.normalColor,
highlightedColor = NormalColorBlock.highlightedColor,
pressedColor = NormalColorBlock.pressedColor,
selectedColor = NormalColorBlock.selectedColor,
disabledColor = NormalColorBlock.disabledColor,
colorMultiplier = 1.0f,
fadeDuration = 0.1f
};
}
private void BackBtnChooseUI()
{
_backBtn.colors = new ColorBlock
{
normalColor = chooseColorBlock.normalColor,
highlightedColor = chooseColorBlock.highlightedColor,
pressedColor = chooseColorBlock.pressedColor,
selectedColor = chooseColorBlock.selectedColor,
disabledColor = chooseColorBlock.disabledColor,
colorMultiplier = 1.0f,
fadeDuration = 0.1f
};
}
private void BackBtnNormalUI()
{
_backBtn.colors = new ColorBlock
{
normalColor = NormalColorBlock.normalColor,
highlightedColor = NormalColorBlock.highlightedColor,
pressedColor = NormalColorBlock.pressedColor,
selectedColor = NormalColorBlock.selectedColor,
disabledColor = NormalColorBlock.disabledColor,
colorMultiplier = 1.0f,
fadeDuration = 0.1f
};
}
#endregion
}