Skip to content

Instantly share code, notes, and snippets.

@soraphis
Created December 11, 2016 19:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soraphis/2125e3430ccac4be258720a9534c10a5 to your computer and use it in GitHub Desktop.
Save soraphis/2125e3430ccac4be258720a9534c10a5 to your computer and use it in GitHub Desktop.
/*
this is an example
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public struct SomeTestStruct{
public int field1;
public int field2;
public int field3;
public int field4;
}
public class DummyComponent : MonoBehaviour {
[StructCopy] public SomeTestStruct structA;
[StructCopy] public SomeTestStruct structB;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
public class StructCopy : PropertyAttribute {
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(StructCopy))]
public class StructCopyDrawer : PropertyDrawer{
private static Dictionary<string, object> Clipboard = new Dictionary<string, object>();
public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
{
var additional_height = 0f;
if (property.isExpanded) {
additional_height = property.CountInProperty () * EditorGUIUtility.singleLineHeight;
}
return 2 * EditorGUIUtility.singleLineHeight + additional_height + 5;
}
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label){
var left = new Rect (position.position, new Vector2 (position.width / 2, EditorGUIUtility.singleLineHeight));
var right = new Rect (new Vector2(left.position.x + left.width, left.position.y), new Vector2 (position.width / 2, EditorGUIUtility.singleLineHeight));
if (GUI.Button (left, "copy this")) {
var obj = property.serializedObject.targetObject;
var type = property.serializedObject.targetObject.GetType();
var field = type.GetField (property.name);
var val = field.GetValue (obj);
if (Clipboard.ContainsKey (property.type)) {
Clipboard [property.type] = val;
} else {
Clipboard.Add (property.type, val);
}
}
GUI.enabled = Clipboard.ContainsKey (property.type);
if (GUI.Button (right, "paste into this")) {
var val = Clipboard [property.type];
var obj = property.serializedObject.targetObject;
var type = property.serializedObject.targetObject.GetType();
var field = type.GetField (property.name);
field.SetValue (obj, val);
property.serializedObject.ApplyModifiedProperties ();
}
GUI.enabled = true;
position.y += EditorGUIUtility.singleLineHeight + 5;
EditorGUI.PropertyField (position, property, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment