【问题标题】:Unity - Image change color not applying in sceneUnity - 图像更改颜色不适用于场景
【发布时间】:2021-09-13 22:38:24
【问题描述】:

我只是想创建一个标签页。为此,我为每个选项卡按钮使用了一个按钮,并在单击时显示特定页面。但是我需要按钮来保持选定的颜色,直到选择了同一列表的另一个 TabButton,默认情况下不这样做。因此,我尝试编辑 .colorBlock 属性,但更改仅应用于检查器而不应用于场景。所以我删除了按钮组件并尝试操作Image.color,但结果相同。所以我创建了自己的UI/Default - Material 并尝试更改材质的颜色Tint。但是这样做时,一旦我设置了颜色,所有的 UI 都会永远不可见,我必须重新启动 Unity。是我做错了什么还是 Unity 完全出问题了?

更改色块:

private void UpdateColor(TabButton button, Color color) {
    ColorBlock colorBlock = button.Button.colors;
    colorBlock.normalColor = color;
    colorBlock.highlightedColor = color;
    button.Button.colors = colorBlock;
}

更改图像颜色:

private void UpdateColor(TabButton button, Color color) {
    button.Image.color = color;
}

更改材质颜色:

private void UpdateColor(TabButton button, Color color) {
    button.Image.material.SetColor("_Color", color);
}

【问题讨论】:

  • 我相信ColorBlock 是一个持续存在的错误,其中任何对象都是Canvas 组件的子对象。至于其他两个,我希望通过.material 调用来创建材质的实例。由于这似乎没有发生,您可以通过执行button.Image.material = new Material(button.Image.material)StartAwake 中实例化材质,然后只需设置颜色button.Image.material.SetColor("_Color", color) 或类似地使用button.Image.color = color,因为这将使用着色器@默认为 987654336@。

标签: c# unity3d


【解决方案1】:

在处理 UI 选项卡时,我始终致力于编写自己的逻辑,以便完全控制发生的事情。

TabsController.cs

using UnityEngine;
using UnityEngine.Events;

public class TabsController : MonoBehaviour
{
    [SerializeField]
    private TabButton[] _tabButtons;

    public UnityAction<int> TabIdChangedAction;

    private void OnEnable()
    {
        TabIdChangedAction += OnTabIdChanged;
    }

    private void OnDisable()
    {
        TabIdChangedAction -= OnTabIdChanged;
    }

    private void Start()
    {
        Initialize();
    }

    private void Initialize()
    {
        // add listener for each tab button with
        // its tab id from the array
        for (int i = 0; i < _tabButtons.Length; i++)
        {
            int id = i;

            _tabButtons[id].TButton.onClick.AddListener(() =>
            {
                ChangedCurrentTab(id);
            });
        }

        // set first tab as current
        ChangedCurrentTab(0);
    }

    public void ChangedCurrentTab(int id)
    {
        TabIdChangedAction?.Invoke(id);
    }

    // update tab buttons visuals
    // on tab id changed
    // while passing to each tab button if it's the current
    private void OnTabIdChanged(int tabId)
    {
        for (int i = 0; i < _tabButtons.Length; i++)
        {
            _tabButtons[i].UpdateVisuals(i == tabId);
        }
    }
}

TabButton.cs

using UnityEngine;
using UnityEngine.UI;

public class TabButton : MonoBehaviour
{
    [SerializeField]
    private Button _tabButton;

    public Button TButton => _tabButton;

    public void UpdateVisuals(bool isCurrent)
    {
        _tabButton.image.color = isCurrent ? Color.green : Color.white;
    }
}

执行概述

https://gfycat.com/flimsydangerousairedale


注意: 这是一个原始的实现,只是为了配合这个想法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多