using System;
using Unity.XR.CoreUtils;
namespace UnityEngine.XR.Interaction.Toolkit.AffordanceSystem.Rendering
{
///
/// Base class for renderer bridge components that abstract the work of setting up material instances or property blocks.
///
[Obsolete("The Affordance System namespace and all associated classes have been deprecated. The existing affordance system will be moved, replaced and updated with a new interaction feedback system in a future version of XRI.")]
public abstract class MaterialHelperBase : MonoBehaviour
{
[SerializeField]
Renderer m_Renderer;
///
/// The renderer to set material parameter overrides on.
///
///
/// Changing this value after being initialized is not supported.
///
public Renderer rendererTarget
{
get => m_Renderer;
set => m_Renderer = value;
}
[SerializeField]
int m_MaterialIndex;
///
/// The index of the material you want to set the parameters of.
///
///
/// Changing this value after being initialized is not supported.
///
public int materialIndex
{
get => m_MaterialIndex;
set => m_MaterialIndex = value;
}
///
/// Whether has been called. The component is automatically initialized during OnEnable.
///
///
protected bool isInitialized { get; private set; }
///
/// See .
///
protected void OnEnable()
{
// If we've already initialized with a valid material instance, then do nothing.
if (isInitialized)
return;
if (m_Renderer == null)
m_Renderer = GetComponentInParent();
if (m_Renderer == null)
{
XRLoggingUtils.LogError($"No renderer found on {this}. Disabling this material helper component.", this);
enabled = false;
return;
}
if (m_Renderer.sharedMaterials.Length == 0)
{
XRLoggingUtils.LogError($"Renderer found on {this} does not have any shared materials. Disabling this material helper component.", this);
enabled = false;
return;
}
if (m_MaterialIndex > m_Renderer.sharedMaterials.Length)
{
XRLoggingUtils.LogWarning($"Insufficient number of materials set on associated render for {this}." +
" Setting target material index to 0.", this);
m_MaterialIndex = 0;
return;
}
Initialize();
}
///
/// Initialize the property block or material instance.
///
protected virtual void Initialize()
{
isInitialized = true;
}
///
/// Returns the for the located in array location
///
/// A from the current
public Material GetSharedMaterialForTarget()
{
return m_Renderer.sharedMaterials[materialIndex];
}
}
}