【问题标题】:Change UI button color in MRTK for hololens在 MRTK 中为 hololens 更改 UI 按钮颜色
【发布时间】:2019-12-05 10:29:12
【问题描述】:

Scene view我需要一些帮助来制作 Hololens 的简单原型。

我在移动键盘等场景中有 9 个 UI 附近的交互可触摸按钮。我希望他们随机地一一突出显示。我将按下突出显示的按钮,下一个随机按钮应突出显示,依此类推。我已经成功地为 VR 制作了相同类型的应用程序,但在 MRTK 中,很难突出显示按钮。如果有人帮助我提供示例代码,我将非常感激。

我还附上了我的场景视图。 我已使用以下代码突出显示 VR 中的代码,但我如何在 MRTK 中使用该代码。它不工作。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Diagnostics;
using UnityEngine.UI;

public class Randomcolor : MonoBehaviour
{
    public static GameObject B1, B2, B3, B4, B5, B6, B7, B8, B9, BX, ST;
    public static int r, r_prev;

    void Start()
    {
        B1 = GameObject.Find("Button1");
        B2 = GameObject.Find("Button2");
        B3 = GameObject.Find("Button3");
        B4 = GameObject.Find("Button4");
        B5 = GameObject.Find("Button5");
        B6 = GameObject.Find("Button6");
        B7 = GameObject.Find("Button7");
        B8 = GameObject.Find("Button8");
        B9 = GameObject.Find("Button9");
        ST = GameObject.Find("ButtonSRT");

        B1.GetComponent<Button>().interactable = true;
        B2.GetComponent<Button>().interactable = true;
        B3.GetComponent<Button>().interactable = true;
        B4.GetComponent<Button>().interactable = true;
        B5.GetComponent<Button>().interactable = true;
        B6.GetComponent<Button>().interactable = true;
        B7.GetComponent<Button>().interactable = true;
        B8.GetComponent<Button>().interactable = true;
        B9.GetComponent<Button>().interactable = true;
    }

    public void clickB1()
    {
        Color_Highlight();
    }

    public void Color_Highlight()
    {
        System.Random random_number = new System.Random();
        r = random_number.Next(9) + 1;
        while (r == r_prev)
        {
             r = random_number.Next(9) + 1;
         }
        r_prev = r;

        if (r == 1) { BX = B1; }
        if (r == 2) { BX = B2; }
        if (r == 3) { BX = B3; }
        if (r == 4) { BX = B4; }
        if (r == 5) { BX = B5; }
        if (r == 6) { BX = B6; }
        if (r == 7) { BX = B7; }
        if (r == 8) { BX = B8; }
        if (r == 9) { BX = B9; }

        var colors = BX.GetComponent<Button>().colors;
        colors.normalColor = Color.cyan;
        BX.GetComponent<Button>().colors = colors;
    }

    public void Color_Reset()
    {
        var colors = BX.GetComponent<Button>().colors;
        colors.normalColor = new Color32(69, 154, 43, 255);
        BX.GetComponent<Button>().colors = colors;
    }
}

【问题讨论】:

    标签: c# unity3d hololens mrtk


    【解决方案1】:

    我们看到你在代码中修改了按钮的normalColor,所以MRTK Control中对应的属性可能就是你所期望的。 Interactable 脚本是正确的方向,您的屏幕截图显示它已添加到按钮中。

    实际上,在 MRTKv2 中,Interactable 组件中的States 定义了交互阶段,例如按下或观察,而 MRTK 附带的DefaultInteractableStates 包含四种状态:默认、焦点、按下、禁用。视觉主题将响应这些状态转换。它可能涉及更改按钮的颜色、调整元素的大小以响应焦点等。

    因此,要根据您现有的代码逻辑更改 MRTK 按钮控件的默认颜色,您需要修改的颜色属性是在 Interactable 组件中应用的主题的默认状态。

    以 MixedRealityToolkit.Examples/Demos/UX/Interactables/Scenes 中的 InteractablesExamples 场景为例,下面的代码是更改默认状态下 HolographicButton 对象颜色的最简单方法:

    var colorTheme = this.GetComponent<Interactable>().ActiveThemes[0];
    colorTheme.StateProperties[0].Values[0].Color = Color.green;
    

    【讨论】:

    • 非常感谢您的回复。几天以来,我一直坚持这一点。回复我的只有你一个。我尝试应用您的建议,但现在甚至没有通过给出以下错误来播放。如果有一种简单的方法,我可以更改整个按钮结构和代码。请建议我。 ' NullReferenceException: 对象引用未设置为对象 Timer.Start () 的实例(在 Assets/Scripts/Timer.cs:37)' @Hernando-MSFT
    • 您需要研究这两个文档(microsoft.github.io/MixedRealityToolkit-Unity/Documentation/… & microsoft.github.io/MixedRealityToolkit-Unity/Documentation/…) 并正确配置您的可交互组件,然后您可以在代码中修改主题的颜色。此外,MRTKv2 提供的 InteractablesExamples 场景是熟悉 Interactable 组件的绝佳开端。因此,强烈建议您参考此示例来修复您的项目。上面的代码也是基于示例。
    猜你喜欢
    • 2011-05-22
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 2014-07-11
    相关资源
    最近更新 更多