更改MRTK中Hololens的UI按钮颜色

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;
    }
}
hxcn2004 回答:更改MRTK中Hololens的UI按钮颜色

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

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

因此,要根据现有代码逻辑更改MRTK按钮控件的默认颜色,需要修改的color属性处于可交互组件中所应用主题的默认状态。

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

var colorTheme = this.GetComponent<Interactable>().ActiveThemes[0];
colorTheme.StateProperties[0].Values[0].Color = Color.green;
本文链接:https://www.f2er.com/2971149.html

大家都在问