using System;
using System.Collections.Generic;
using Unity.XR.CoreUtils;
using UnityEngine.Assertions;
using UnityEngine.XR.Interaction.Toolkit.Utilities;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.XR.Interaction.Toolkit
{
///
/// Configuration class for interaction layers.
/// Stores all interaction layers.
///
[ScriptableSettingsPath(ProjectPath.k_XRInteractionSettingsFolder)]
class InteractionLayerSettings : ScriptableSettings, ISerializationCallbackReceiver
{
const string k_DefaultLayerName = "Default";
internal const int layerSize = 32;
internal const int builtInLayerSize = 1;
[SerializeField]
string[] m_LayerNames;
///
/// Returns the singleton settings instance or loads the settings asset if it exists.
/// Unlike , this method will not create the asset if it does not exist.
///
/// A settings class derived from , or .
internal static InteractionLayerSettings GetInstanceOrLoadOnly()
{
if (BaseInstance != null)
return BaseInstance;
// See CreateAndLoad() in base class.
Assert.IsTrue(HasCustomPath);
BaseInstance = Resources.Load(GetFilePath(), typeof(InteractionLayerSettings)) as InteractionLayerSettings;
return BaseInstance;
}
///
/// Check if the interaction layer name at the supplied index is empty.
///
/// The index of the target interaction layer.
/// Returns if the target interaction layer is empty.
internal bool IsLayerEmpty(int index)
{
return string.IsNullOrEmpty(m_LayerNames[index]);
}
///
/// Sets the interaction layer name at the supplied index.
///
/// The index of the target interaction layer.
/// The name of the target interaction layer.
internal void SetLayerNameAt(int index, string layerName)
{
#if UNITY_EDITOR
Undo.RecordObject(this, "Interaction Layer");
#endif
m_LayerNames[index] = layerName;
#if UNITY_EDITOR
EditorUtility.SetDirty(this);
#endif
}
///
/// Gets the interaction layer name at the supplied index.
///
/// The index of the target interaction layer.
/// Returns the target interaction layer name.
internal string GetLayerNameAt(int index)
{
return m_LayerNames[index];
}
///
/// Gets the value (or bit index) of the supplied interaction layer name.
///
/// The name of the interaction layer to search for its value.
/// Returns the interaction layer value.
internal int GetLayer(string layerName)
{
for (var i = 0; i < m_LayerNames.Length; i++)
{
if (string.Equals(layerName, m_LayerNames[i]))
return i;
}
return -1;
}
///
/// Fills in the supplied lists with the interaction layer name and its correspondent value in the same index.
///
/// The list to fill in with interaction layer names.
/// The list to fill in with interaction layer values.
internal void GetLayerNamesAndValues(List names, List values)
{
for (var i = 0; i < m_LayerNames.Length; i++)
{
var layerName = m_LayerNames[i];
if (string.IsNullOrEmpty(layerName))
continue;
names.Add(layerName);
values.Add(i);
}
}
///
public void OnBeforeSerialize()
{
if (m_LayerNames == null)
m_LayerNames = new string[layerSize];
if (m_LayerNames.Length != layerSize)
Array.Resize(ref m_LayerNames, layerSize);
if (!string.Equals(m_LayerNames[0], k_DefaultLayerName))
m_LayerNames[0] = k_DefaultLayerName;
}
///
public void OnAfterDeserialize()
{
}
}
}