Tri-Inspector/Editor/TriPropertyTreeForSerializedObject.cs

104 lines
3.4 KiB
C#
Raw Normal View History

using System;
2022-08-17 13:46:26 -04:00
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEditor;
namespace TriInspector
{
public sealed class TriPropertyTreeForSerializedObject : TriPropertyTree
{
private readonly SerializedObject _serializedObject;
private readonly SerializedProperty _scriptProperty;
public TriPropertyTreeForSerializedObject([NotNull] SerializedObject serializedObject)
{
_serializedObject = serializedObject ?? throw new ArgumentNullException(nameof(serializedObject));
_scriptProperty = serializedObject.FindProperty("m_Script");
TargetObjectType = _serializedObject.targetObject.GetType();
TargetsCount = _serializedObject.targetObjects.Length;
TargetIsPersistent = _serializedObject.targetObject is var targetObject &&
targetObject != null && EditorUtility.IsPersistent(targetObject);
2022-06-01 03:35:33 -04:00
RootPropertyDefinition = new TriPropertyDefinition(
memberInfo: null,
2022-08-17 13:46:26 -04:00
ownerType: null,
2022-06-01 03:35:33 -04:00
order: -1,
fieldName: "ROOT",
fieldType: TargetObjectType,
valueGetter: (self, targetIndex) => _serializedObject.targetObjects[targetIndex],
valueSetter: (self, targetIndex, value) => _serializedObject.targetObjects[targetIndex],
2022-08-17 13:46:26 -04:00
attributes: new List<Attribute>(),
2022-06-01 03:35:33 -04:00
isArrayElement: false);
RootProperty = new TriProperty(this, null, RootPropertyDefinition, serializedObject);
2022-06-02 03:18:13 -04:00
RootProperty.ValueChanged += OnPropertyChanged;
RootProperty.ChildValueChanged += OnPropertyChanged;
}
2022-06-01 03:35:33 -04:00
public override void Dispose()
{
2022-06-02 03:18:13 -04:00
RootProperty.ChildValueChanged -= OnPropertyChanged;
RootProperty.ValueChanged -= OnPropertyChanged;
2022-06-01 03:35:33 -04:00
base.Dispose();
}
2022-06-02 03:18:13 -04:00
public override void Update(bool forceUpdate = false)
{
2022-06-02 03:18:13 -04:00
if (forceUpdate)
{
_serializedObject.SetIsDifferentCacheDirty();
_serializedObject.Update();
}
base.Update(forceUpdate);
}
2023-03-22 03:38:33 -04:00
public override void Draw()
{
DrawMonoScriptProperty();
2023-03-22 03:38:33 -04:00
base.Draw();
}
2022-06-02 03:18:13 -04:00
public override bool ApplyChanges()
{
2022-06-02 03:18:13 -04:00
var changed = base.ApplyChanges();
changed |= _serializedObject.ApplyModifiedProperties();
return changed;
}
2022-06-02 03:18:13 -04:00
public override void ForceCreateUndoGroup()
{
2022-06-02 03:18:13 -04:00
Undo.RegisterCompleteObjectUndo(_serializedObject.targetObjects, "Inspector");
Undo.FlushUndoRecordObjects();
}
2022-06-02 03:18:13 -04:00
private void OnPropertyChanged(TriProperty changedProperty)
{
foreach (var targetObject in _serializedObject.targetObjects)
{
EditorUtility.SetDirty(targetObject);
}
RequestValidation();
2022-06-02 03:18:13 -04:00
RequestRepaint();
}
private void DrawMonoScriptProperty()
{
if (RootProperty.TryGetAttribute(out HideMonoScriptAttribute _))
{
return;
}
EditorGUI.BeginDisabledGroup(true);
var scriptRect = EditorGUILayout.GetControlRect(true);
scriptRect.xMin += 3;
EditorGUI.PropertyField(scriptRect, _scriptProperty);
EditorGUI.EndDisabledGroup();
}
}
}