The Road to HoloLens2 - Configuration File (4)

Copyright statement: Davidwang's original article is strictly prohibited from being used in any commercial way and may be reproduced only with authorization.

  The configuration file defines all functional features and some technical details of the application, and determines the type and appearance of the application. Fine and correct configuration file settings can make more effective use of hardware resources and improve application performance while meeting application needs. Among these configuration files, some configuration files can be enabled/disabled or even changed to other configuration files while the application is running. Some configuration files cannot be modified after the application completes initialization. Generally speaking, if we need to dynamically replace the configuration file at runtime based on the hardware device capabilities to enable/disable certain functions, we can complete the replacement of the corresponding configuration file before MRTK initialization, as shown in the following code list. It should be noted that, The script that replaces the configuration file must have a higher execution priority to ensure that the replacement is completed before MRTK is initialized.

//方法一
using Microsoft.MixedReality.Toolkit;
using UnityEditor;
using UnityEngine;
public class ProfileSwapper : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        // 加载配置文件,MixedRealityToolkitConfigurationProfile可以换成任何需要替换的配置文件
        var profile = AssetDatabase.LoadAssetAtPath<MixedRealityToolkitConfigurationProfile>("Assets/MixedRealityToolkit.Generated/CustomProfiles/RuntimeSwapProfile.asset");
        MixedRealityToolkit.Instance.ActiveProfile = profile;
    }
}

  If you want to change the behavior of certain features by replacing the configuration file after MRTK completes initialization, this needs to be decided based on the specific configuration file and its configuration management function. Some configuration files can be dynamically switched at runtime, while some configuration files cannot. To perform dynamic switching, you should first fully understand the details of the configuration file you want to switch.

In MRTK2.7 and later, MRTK provides a new method SetProfileBeforeInitialization(), which can be used to ensure that the original configuration file is replaced before MRTK is initialized. Of course, as before, the premise is that this script has higher execution priority. The code As follows.

//方法二
using Microsoft.MixedReality.Toolkit;
using UnityEngine;
public class PreInitProfileSwapper : MonoBehaviour
{
    
    

    [SerializeField]
    private MixedRealityToolkitConfigurationProfile profileToUse = null;

    private void Awake()
    {
    
    
        //profileToUse为需要替换运行的配置文件
        MixedRealityToolkit.SetProfileBeforeInitialization(profileToUse);
    }
}

  In addition to using the above method of dynamically switching configuration files, MRTK2.7 also introduces hot switching, which can dynamically switch configuration files at runtime. The code is as follows,

//方法三
using Microsoft.MixedReality.Toolkit;
using UnityEngine;
public class PreInitProfileSwapper : MonoBehaviour
{
    
    

    [SerializeField]
    private MixedRealityToolkitConfigurationProfile profileToUse = null;

    private void swapperProfile()
    {
    
    
        //profileToUse为需要替换运行的配置文件
        MixedRealityToolkit.Instance.ActiveProfile = profileToUse;
    }
}

  When using hot switch, the current configuration file uninstallation is found after the last LateUpdate() method of the current service is executed, and the new configuration file loading occurs before all Update() methods of the current service are executed. Since it is a hot switch, the MR application will be affected during this process and may freeze or even freeze, but the UI elements will remain unchanged. In addition, scripts with higher priority may access configuration files during this switch process. If This is a new configuration file that has not yet been set up, which will cause an exception.

  Many common configuration files can be configured during development, but in actual development, it may also be necessary to dynamically modify the parameters of some configuration items at runtime based on operating conditions. Due to the good tree structure of the MRTK configuration file cluster, we It is very easy to obtain the required sub-configuration file or a certain configuration item. The typical sample code is as follows.

//需要引入Microsoft.MixedReality.Toolkit命名空间
void GetProfile()
{
    
    
    var mainProfile = MixedRealityToolkit.Instance.ActiveProfile;
    var inputProfile = mainProfile.InputSystemProfile;
    var pointerProfile = inputProfile.PointerProfile;
    //获取配置属性参数
    float pointingExtent = pointerProfile.PointingExtent;
    //设置配置属性参数
    inputProfile.HandTrackingProfile.HandMeshVisualizationModes = Microsoft.MixedReality.Toolkit.Utilities.SupportedApplicationModes.Player;
}

  Through the MixedRealityToolkit.Instance.ActiveProfile property we can get the current MR application main configuration file. This is a single instance. There is only one instance when the MR application is running. Then we can get the desired sub-configuration file according to the tree structure of the configuration file. Configuration files or configuration properties. It should be noted that some configuration file parameters cannot be modified at runtime, while others can, which is related to the specific configuration file.

  For HoloLens2 devices, MRTK provides the default DefaultHoloLens2ConfigurationProfile configuration file and its sub-configuration file cluster. This configuration file cluster ensures that the configuration used is universal and reliable. When configuring the project, it is recommended to perform targeted optimization based on this configuration file cluster. , faster and safer.

(over)
  

references

1. Configuration file configuration file

Guess you like

Origin blog.csdn.net/yolon3000/article/details/118737552