public CopiedItem(object obj, CopyModes copyMode) { this.obj = obj; this.copyMode = copyMode; }
/// <summary> /// Copies the specified object. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj">The object.</param> /// <param name="copyMode">The copy mode.</param> public static void Copy <T>(T obj, CopyModes copyMode) { if (obj == null) { return; } Clipboard.copyMode = copyMode; var type = obj.GetType(); if (type == typeof(string) || obj as UnityEngine.Object || type.IsValueType || type.IsEnum) { copyMode = CopyModes.CopyReference; Clipboard.obj = obj; } else if (copyMode == CopyModes.DeepCopy) { using (var stream = new MemoryStream()) using (var serializationContext = Cache <SerializationContext> .Claim()) using (var deserializationContext = Cache <DeserializationContext> .Claim()) { serializationContext.Value.Config.SerializationPolicy = SerializationPolicies.Everything; deserializationContext.Value.Config.SerializationPolicy = SerializationPolicies.Everything; List <UnityEngine.Object> unityReferences; SerializationUtility.SerializeValue(obj, stream, DataFormat.Binary, out unityReferences, serializationContext); stream.Position = 0; Clipboard.obj = SerializationUtility.DeserializeValue <object>(stream, DataFormat.Binary, unityReferences, deserializationContext); } } else if (copyMode == CopyModes.ShallowCopy) { if (obj.GetType().IsArray) { Array oldArray = (Array)(object)obj; Array newArray; if (oldArray.Rank > 1) { long[] lengths = new long[oldArray.Rank]; for (int i = 0; i < lengths.Length; i++) { lengths[i] = oldArray.GetLongLength(i); } newArray = Array.CreateInstance(oldArray.GetType().GetElementType(), lengths); } else { newArray = Array.CreateInstance(oldArray.GetType().GetElementType(), oldArray.LongLength); } Array.Copy(oldArray, newArray, 0); Clipboard.obj = newArray; } else { Clipboard.obj = obj.GetType().GetMethod("MemberwiseClone", Flags.AllMembers).Invoke(obj, null); } } else { Clipboard.obj = obj; } if (obj as string != null) { GUIUtility.systemCopyBuffer = obj as string; } else { GUIUtility.systemCopyBuffer = null; } }