在 Unity 中忽略除 alpha 之外的所有父画布组属性?

问题描述 投票:0回答:2

我可以让子画布组中的元素忽略父画布组中的

Interactable
Block Raycasts
而不是
Alpha
吗?

在我当前的设置中,子游戏对象不服从父画布组的淡出动画。 (无关,但我正在使用 LeanTween 的补间库)

unity3d canvas
2个回答
0
投票

不,您不能使用开箱即用的画布组设置。

最简单的解决方案是在两个组上调用相同的 alpha tween。

如果您需要子组受其祖父母的 alpha 影响,还有几个额外的步骤:

  • 在子组上取消选择
    Ignore Parent Group
  • 将子组移出成为父组的兄弟
  • 对两组调用相同的 alpha tween。

0
投票

我制作了一个脚本来在每次更新时复制特定的值。因此,您将画布组设置为忽略父组,然后将此 CanvasGroupInheritor 脚本也添加到同一游戏对象上,然后设置您要继承的内容(在本例中为 alpha)。为我的用例做我需要的,即使它感觉很愚蠢。

using UnityEngine;

namespace Studio.Game
{
    // Canvas Groups by default inherit all the attributes of their parent
    // But in some cases you only want to inherit some aspects
    // In that case, set your canvas group to have ignoreParentGroups=true, then attach this script to explicitly inherit from the parent
    // Currently we search upwards for the parent to copy from, but this script could later be amended to accept a reference the the parent also
    public class CanvasGroupInheritor : MonoBehaviour
    {
        [SerializeField] private bool inheritAlpha = false;
        [SerializeField] private bool inheritInteractable = false;
        [SerializeField] private bool inheritBlocksRaycasts = false;

        private CanvasGroup myCanvasGroup = null;

        public void Update()
        {
            SetMyCanvasGroup();
            var parentCanvasGroup = GetParentCanvasGroup();
            if (myCanvasGroup == null || parentCanvasGroup == null)
            {
                return;
            }

            if (inheritAlpha)
            {
                myCanvasGroup.alpha = parentCanvasGroup.alpha;
            }
            if (inheritInteractable)
            {
                myCanvasGroup.interactable = parentCanvasGroup.interactable;
            }
            if (inheritBlocksRaycasts)
            {
                myCanvasGroup.blocksRaycasts = parentCanvasGroup.blocksRaycasts;
            }
        }

        private void SetMyCanvasGroup()
        {
            if (myCanvasGroup == null)
            {
                myCanvasGroup = this.gameObject.GetComponent<CanvasGroup>();
            }
        }

        private CanvasGroup GetParentCanvasGroup()
        {
            return this.gameObject.transform.parent.GetComponentInParent<CanvasGroup>();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.