VR4Medical/ICI/Library/PackageCache/com.unity.xr.interaction.toolkit@42ef3600567b/Runtime/AffordanceSystem/Rendering/MaterialInstanceHelper.cs
2025-07-29 13:45:50 +03:00

58 lines
2.2 KiB
C#

using System;
namespace UnityEngine.XR.Interaction.Toolkit.AffordanceSystem.Rendering
{
/// <summary>
/// Creates material instance for a material associated with a given renderer material index and provide accessor to it.
/// </summary>
[AddComponentMenu("Affordance System/Rendering/Material Instance Helper", 12)]
[HelpURL(XRHelpURLConstants.k_MaterialInstanceHelper)]
[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 class MaterialInstanceHelper : MaterialHelperBase
{
Material m_MaterialInstance;
/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
protected void OnDestroy()
{
if (m_MaterialInstance != null)
{
Destroy(m_MaterialInstance);
m_MaterialInstance = null;
}
}
/// <summary>
/// Try to get initialized material instance as configured on the component.
/// </summary>
/// <param name="materialInstance">Material instance. Will be <see langword="null"/> if invalid.</param>
/// <returns>Returns <see langword="true"/> if material instance is initialized. Otherwise, returns <see langword="false"/>.</returns>
public bool TryGetMaterialInstance(out Material materialInstance)
{
if (!isInitialized)
{
materialInstance = null;
return false;
}
materialInstance = m_MaterialInstance;
return true;
}
/// <inheritdoc/>
protected override void Initialize()
{
if (m_MaterialInstance == null)
{
var sharedMaterials = rendererTarget.sharedMaterials;
m_MaterialInstance = new Material(sharedMaterials[materialIndex]);
sharedMaterials[materialIndex] = m_MaterialInstance;
rendererTarget.sharedMaterials = sharedMaterials;
base.Initialize();
}
}
}
}