forked from PassivePicasso/ThunderKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComposableObject.cs
More file actions
65 lines (52 loc) · 2.04 KB
/
Copy pathComposableObject.cs
File metadata and controls
65 lines (52 loc) · 2.04 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Serialization;
using System.IO;
namespace ThunderKit.Core
{
public abstract class ComposableObject : ScriptableObject
{
[FormerlySerializedAs("runSteps")]
public ComposableElement[] Data;
public abstract bool SupportsType(Type type);
public abstract Type ElementType { get; }
public abstract string ElementTemplate { get; }
public void InsertElement(ComposableElement instance, int index)
{
if (!ElementType.IsAssignableFrom(instance.GetType())) return;
if (!SupportsType(instance.GetType())) return;
AssetDatabase.AddObjectToAsset(instance, this);
var so = new SerializedObject(this);
var dataArray = so.FindProperty(nameof(Data));
dataArray.InsertArrayElementAtIndex(index);
var stepField = dataArray.GetArrayElementAtIndex(index);
stepField.objectReferenceValue = instance;
stepField.serializedObject.SetIsDifferentCacheDirty();
stepField.serializedObject.ApplyModifiedProperties();
}
public void RemoveElement(ComposableElement instance, int index)
{
var so = new SerializedObject(this);
var dataArray = so.FindProperty(nameof(Data));
var elementAtIndex = dataArray.GetArrayElementAtIndex(index).objectReferenceValue as ComposableElement;
if (elementAtIndex != instance)
{
Debug.LogError("ComposableObject.RemoveElement: instance does not match index");
return;
}
AssetDatabase.RemoveObjectFromAsset(instance);
DeleteElementAtIndex(dataArray, index);
DestroyImmediate(instance);
so.SetIsDifferentCacheDirty();
so.ApplyModifiedProperties();
}
private void DeleteElementAtIndex(SerializedProperty array, int index)
{
var elementToDelete = array.GetArrayElementAtIndex(index);
if (elementToDelete.propertyType == SerializedPropertyType.ObjectReference)
elementToDelete.objectReferenceValue = null;
array.DeleteArrayElementAtIndex(index);
}
}
}