// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using System;
using UnityEngine;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UltimateXR.Extensions.Unity
{
///
/// Unity extensions
///
public static class ObjectExt
{
#region Public Methods
#if UNITY_EDITOR
///
/// Assigns a serialized property value if code that only executes in the editor.
///
/// The object (GameObject or component) with the serialized property
/// The property name
/// Action that gets the serialized property as argument and enables to assign any value
///
///
/// component.AssignSerializedProperty("_myBoolVar", p => p.boolValue = true);
///
///
public static void AssignSerializedProperty(this Object self, string propertyName, Action assigner)
{
SerializedObject serializedObject = new SerializedObject(self);
SerializedProperty serializedProperty = serializedObject.FindProperty(propertyName);
if (serializedProperty == null)
{
Debug.LogError($"{nameof(AssignSerializedProperty)}(): Cannot find property {propertyName}");
return;
}
assigner.Invoke(serializedProperty);
serializedObject.ApplyModifiedProperties();
}
#endif
///
/// Controls whether to show a given object in the inspector.
///
/// The object to show
/// Whether to show the object or now
public static void ShowInInspector(this Object self, bool show = true)
{
if (show)
{
self.hideFlags &= ~HideFlags.HideInInspector;
}
else
{
self.hideFlags |= HideFlags.HideInInspector;
}
}
///
/// Controls whether to show a given object in the inspector and whether it is editable.
///
/// The object to set
/// Whether to show it in the inspector
/// Whether it is editable
public static void ShowInInspector(this Object self, bool show, bool editable)
{
if (show)
{
self.hideFlags &= ~HideFlags.HideInInspector;
}
else
{
self.hideFlags |= HideFlags.HideInInspector;
}
if (editable)
{
self.hideFlags &= ~HideFlags.NotEditable;
}
else
{
self.hideFlags |= HideFlags.NotEditable;
}
}
#endregion
}
}