-
Notifications
You must be signed in to change notification settings - Fork 4
/
SerializedObjectExtensions.cs
45 lines (40 loc) · 1.77 KB
/
SerializedObjectExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using UnityEngine;
using UnityEditor;
namespace ArteHacker.UITKEditorAid.Utils
{
/// <summary>
/// Extension methods for <see cref="SerializedObject"/> that are especially useful in combination with UI Toolkit.
/// </summary>
public static class SerializedObjectExtensions
{
/// <summary>
/// Returns whether a <see cref="SerializedObject"/> is considered editable.
/// It will return false if any of its <see cref="SerializedObject.targetObjects"/> are marked with <see cref="HideFlags.NotEditable"/>
/// or if there's an asset that isn't open for editing in version control.
/// It's especially useful in combination with <see cref="Disabler"/>.
/// </summary>
/// <param name="obj"> The object to check. </param>
/// <returns> Whether object is editable. </returns>
public static bool IsEditable(this SerializedObject obj)
{
return IsEditable(obj, StatusQueryOptions.UseCachedIfPossible);
}
public static bool IsEditable(this SerializedObject obj, StatusQueryOptions queryOptions)
{
if (obj == null) return false;
if (!obj.isEditingMultipleObjects)
return IsTargetEditable(obj.targetObject, queryOptions);
foreach (var target in obj.targetObjects)
{
if (!IsTargetEditable(target, queryOptions))
return false;
}
return true;
}
private static bool IsTargetEditable(Object target, StatusQueryOptions queryOptions)
{
return target && (target.hideFlags & HideFlags.NotEditable) == 0
&& (!EditorUtility.IsPersistent(target) || AssetDatabase.IsOpenForEdit(target, queryOptions));
}
}
}