首页 > 解决方案 > 考虑将值存储在临时变量 (UI.graphic.color)

问题描述

我正在尝试更改多个 UI 元素的 alpha。

“UnityEngine.UI.Graphic.color 考虑将值存储在临时变量中”

public class DialogueManager : MonoBehaviour {     
public Text nameText;
public Text dialogueText;
public Image facePlate;
public PlayerController thePlayer;

void Awake () {
    thePlayer = FindObjectOfType<PlayerController> ();
}

void Update () {

    if (!thePlayer.isTalking) {
        Color temp = facePlate.color;
        temp.a = 0f;
        nameText.color.a = temp.a;
        dialogueText.color.a = temp.a;
        facePlate.color.a = temp.a;
    }

我尝试了多种方法来做到这一点,尽管我总是以同样的错误告终。

标签: c#unity3d2d

解决方案


您不能直接更改 Color 的任何变量。因此,您可以将 Color 结构的值分配给临时变量并更改它。然后将温度重新分配给颜色部分。在这里,我们每次基本上都是取颜色变量的值并修改它并重新分配新值

if (!thePlayer.isTalking) {
    Color temp = facePlate.color;
    temp.a = 0f;
    facePlate.color = temp;

    temp = nameText.color;
    temp.a = 0f;
    nameText.color = temp;

    temp = dialogueText.color;
    temp.a = 0f;
    dialogueText.color = temp;
}

推荐阅读