using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using UnityEngine;
namespace Phantom.XRMOD.UnityFusion.Editor
{
///
/// Extension methods for that can be used to
/// get or set the
/// name of the .
///
public static class ComponentExtensions
{
///
/// Gets the name of the .
///
/// In the editor this corresponds with the name of the component as shown in the Inspector.
///
///
/// In builds this always returns the type name of the component class.
///
///
/// The whose name to get.
///
[return: NotNull]
public static string GetName([DisallowNull] this Component component)
{
#if UNITY_EDITOR
return Editor.ComponentName.Get(component).name;
#else
return component.GetType().Name;
#endif
}
///
/// Sets the name of the .
///
/// In builds calls to this method are ignored and will always return
/// the name of the component class.
///
///
/// The whose name to set.
/// The new name for the .
[Conditional("UNITY_EDITOR")]
public static void SetName([DisallowNull] this Component component, string name)
{
#if UNITY_EDITOR
Editor.ComponentName.Set(component, name, Editor.ModifyOptions.Defaults);
#endif
}
///
/// Sets the name of the .
///
/// In builds calls to this method are ignored and will always return
/// the name of the component class.
///
///
/// The whose name to set.
/// The new name for the .
///
/// If then the default name of the component (which is usually the name
/// of the component class) will be shown in the Inspector as a suffix after the .
///
[Conditional("UNITY_EDITOR")]
public static void SetName([DisallowNull] this Component component, string name, bool classNameSuffix)
{
#if UNITY_EDITOR
Editor.ComponentName.Set(component, name, classNameSuffix, Editor.ModifyOptions.Defaults);
#endif
}
///
/// Sets the name of the .
///
/// In builds calls to this method are ignored and will always return
/// the name of the component class.
///
///
/// The whose name to set.
/// The new name for the .
///
/// Auxiliary suffix text to show in the Inspector after the .
///
/// If this is or then no suffix
/// will be shown after the name in the Inspector.
///
///
[Conditional("UNITY_EDITOR")]
public static void SetName([DisallowNull] this Component component, string name, [MaybeNull] string suffix)
{
#if UNITY_EDITOR
Editor.ComponentName.Set(component, name, suffix, Editor.ModifyOptions.Defaults);
#endif
}
///
/// Sets the name of the and its tooltip in the Inspector.
///
/// In builds calls to this method are ignored and will always return
/// the name of the component class.
///
///
/// The whose name to set.
/// The new name for the .
///
/// If then the default name of the component (which is usually the name
/// of the component class) will be shown in the Inspector as a suffix after the .
///
/// The new Inspector tooltip for the .
[Conditional("UNITY_EDITOR")]
public static void SetName([DisallowNull] this Component component, string name, bool classNameSuffix, string tooltip)
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.delayCall += ()=>
{
Editor.ComponentTooltip.Set(component, tooltip, Editor.ModifyOptions.Immediate | Editor.ModifyOptions.DisallowRemoveNameContainer);
Editor.ComponentName.Set(component, name, classNameSuffix, Editor.ModifyOptions.Immediate);
};
#endif
}
///
/// Sets the name of the and its tooltip in the Inspector.
///
/// In builds calls to this method are ignored and will always return
/// the name of the component class.
///
///
/// The whose name to set.
///
/// The Inspector title to give the , consisting of a name, suffix and tooltip.
///
[Conditional("UNITY_EDITOR")]
public static void SetName([DisallowNull] this Component component, HeaderContent headerContent)
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.delayCall += ()=>
{
Editor.ComponentTooltip.Set(component, headerContent.tooltip, Editor.ModifyOptions.Immediate | Editor.ModifyOptions.DisallowRemoveNameContainer);
Editor.ComponentName.Set(component, new Editor.NameWithSuffix(headerContent, component), Editor.ModifyOptions.Immediate);
};
#endif
}
///
/// Sets the name of the .
///
/// In builds calls to this method are ignored and will always return
/// the name of the component class.
///
///
/// The whose name to set.
/// The new name and Inspector tooltip for the .
///
/// If then the default name of the component (which is usually the name
/// of the component class) will be shown in the Inspector as a suffix after the name.
///
[Conditional("UNITY_EDITOR")]
public static void SetName([DisallowNull] this Component component, GUIContent nameAndTooltip, bool classNameSuffix)
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.delayCall += ()=>
{
Editor.ComponentTooltip.Set(component, nameAndTooltip.tooltip, Editor.ModifyOptions.Immediate | Editor.ModifyOptions.DisallowRemoveNameContainer);
Editor.ComponentName.Set(component, nameAndTooltip.text, classNameSuffix, Editor.ModifyOptions.Immediate);
};
#endif
}
///
/// Sets the name of the .
///
/// In builds calls to this method are ignored and will always return
/// the name of the component class.
///
///
/// The whose name to set.
/// The new name for the .
///
/// Auxiliary suffix text to show in the Inspector after the .
///
/// If this is or then no suffix
/// will be shown after the name in the Inspector.
///
///
/// The new Inspector tooltip for the .
[Conditional("UNITY_EDITOR")]
public static void SetName([DisallowNull] this Component component, string name, [MaybeNull] string suffix, string tooltip)
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.delayCall += ()=>
{
Editor.ComponentTooltip.Set(component, tooltip, Editor.ModifyOptions.Immediate | Editor.ModifyOptions.DisallowRemoveNameContainer);
Editor.ComponentName.Set(component, name, suffix, Editor.ModifyOptions.Immediate);
};
#endif
}
}
}