Skip to content

Commit 3693852

Browse files
committed
add: basic copy paste functionality
1 parent f2e2948 commit 3693852

4 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using UnityEditor;
2+
3+
namespace BrunoMikoski.ScriptableObjectCollections
4+
{
5+
public static class CopyCollectableUtils
6+
{
7+
private static CollectableScriptableObject source;
8+
9+
public static void SetSource(CollectableScriptableObject targetSource)
10+
{
11+
source = targetSource;
12+
}
13+
14+
public static bool CanPasteToTarget(CollectableScriptableObject target)
15+
{
16+
if (source == null)
17+
return false;
18+
19+
return target.GetType() == source.GetType();
20+
}
21+
22+
public static void ApplySourceToStart(CollectableScriptableObject target)
23+
{
24+
if (source == null)
25+
return;
26+
27+
Undo.RecordObject(target, "Paste Changes");
28+
EditorUtility.CopySerializedManagedFieldsOnly(source, target);
29+
EditorUtility.SetDirty(target);
30+
}
31+
32+
}
33+
}

Scripts/Editor/CopyCollectableUtils.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Editor/ScriptableObjectCollectionCustomEditor.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,22 @@ private void DrawItem(int index)
382382
filteredSerializedList[index].ApplyModifiedProperties();
383383
}
384384
}
385+
386+
using (new EditorGUILayout.HorizontalScope())
387+
{
388+
if (GUILayout.Button("Copy", EditorStyles.toolbarButton))
389+
{
390+
CopyCollectableUtils.SetSource(collectionItem);
391+
}
392+
393+
using (new EditorGUI.DisabledScope(!CopyCollectableUtils.CanPasteToTarget(collectionItem)))
394+
{
395+
if (GUILayout.Button("Paste", EditorStyles.toolbarButton))
396+
{
397+
CopyCollectableUtils.ApplySourceToStart(collectionItem);
398+
}
399+
}
400+
}
385401
EditorGUI.indentLevel--;
386402
}
387403
}

Scripts/Runtime/ScriptableObjectCollection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public CollectableScriptableObject AddNew(Type collectionType, string assetName
161161

162162
this.Add(item);
163163
UnityEditor.AssetDatabase.CreateAsset(item, newFileName);
164+
ObjectUtility.SetDirty(this);
164165
isReadyOnlyListDirty = true;
165166
return item;
166167
}

0 commit comments

Comments
 (0)