Stratasys-Fortus-450mc/Packages/MAVRIC.UX/Runtime/Utilities/AutoObjectName.cs

72 lines
2.0 KiB
C#

using System;
using UnityEngine;
namespace MAVRIC.UX.Utilities
{
using Object = UnityEngine.Object;
public class AutoObjectName : MonoBehaviour
{
internal enum Separator
{
None = -1,
Parentheses = 0,
Underscore = 1,
Hyphen = 2,
}
[SerializeField] private bool autoRename = true;
[SerializeField] private string objectName = String.Empty;
[SerializeField] private Separator separatorType = 0;
[SerializeField] private Object component;
private void UpdateObjectName()
{
bool includeObjectName = !(String.IsNullOrEmpty(objectName) || String.IsNullOrWhiteSpace(objectName));
var newObjectName = String.Empty;
if (includeObjectName)
{
newObjectName =
$"{objectName}";
}
if (component != null)
{
newObjectName += $"{ObjectNameSeparator(0)}" +
$"{component.GetType().Name}" +
$"{ObjectNameSeparator(1)}";
}
gameObject.name = newObjectName;
}
private string ObjectNameSeparator(int index)
{
switch (separatorType)
{
case Separator.Parentheses:
return index <= 0 ? " (" : ")";
case Separator.Underscore:
return index <= 0 ? "_" : String.Empty;
case Separator.Hyphen:
return index <= 0 ? "-" : String.Empty;
default:
return $" " + String.Empty;
}
}
private void OnValidate()
{
#if UNITY_EDITOR
if (UnityEditor.EditorUtility.IsPersistent(this.gameObject))
{
return;
}
#endif
UpdateObjectName();
}
}
}