private void CopyProperties(SerializedObject origin, SerializedObject destiny) { destiny.Update(); destiny.CopyFromSerializedProperty(origin.FindProperty("m_loadAllOnStart")); destiny.CopyFromSerializedProperty(origin.FindProperty("m_layerNames")); destiny.CopyFromSerializedProperty(origin.FindProperty("m_groupsNames")); destiny.CopyFromSerializedProperty(origin.FindProperty("m_groupsMenus")); destiny.ApplyModifiedProperties(); }
private void SetupReferencedClip(string otherModelImporterPath) { SerializedObject targetImporter = GetModelImporterSerializedObject(otherModelImporterPath); // We may receive a path that doesn't have a importer. if (targetImporter != null) { targetImporter.CopyFromSerializedProperty(serializedObject.FindProperty("m_AnimationType")); SerializedProperty copyAvatar = targetImporter.FindProperty("m_CopyAvatar"); if (copyAvatar != null) { copyAvatar.boolValue = true; } SerializedProperty avatar = targetImporter.FindProperty("m_LastHumanDescriptionAvatarSource"); if (avatar != null) { avatar.objectReferenceValue = m_Avatar; } CopyHumanDescriptionToDestination(serializedObject, targetImporter); targetImporter.ApplyModifiedProperties(); targetImporter.Dispose(); } }
/// <summary> /// Copy/paste all properties from original to the clone using Unity's Serialization. /// </summary> /// <param name="original"></param> /// <param name="clone"></param> protected virtual void CopyComponents(Component original, Component clone) { var newSerializedObject = new SerializedObject(clone); var originalSerializedObject = new SerializedObject(original); SerializedProperty prop = originalSerializedObject.GetIterator(); if (prop.NextVisible(true)) { do { // Ignore unity's Script property if (prop.name == "m_Script") { // If we don't do this the new component's serialized data // will be pointing to the original script. // TODO: Find a better way of doing this without hard-codding the variable's name. continue; } newSerializedObject.CopyFromSerializedProperty(prop); }while (prop.NextVisible(false)); } // Persist changes on the new component. newSerializedObject.ApplyModifiedPropertiesWithoutUndo(); }
public static void CopySerializedObject(this SerializedObject dst, SerializedObject src, string[] exclusions, bool canUndo = true) { src.Update(); dst.Update(); var sp = src.GetIterator(); sp.Next(true); while (true) { if (exclusions == null || !exclusions.Contains(sp.name)) { dst.CopyFromSerializedProperty(sp); } if (!sp.Next(false)) { break; } } if (canUndo) { dst.ApplyModifiedProperties(); } else { dst.ApplyModifiedPropertiesWithoutUndo(); } }
public static int MatchingPropertyCount(SerializedObject source, SerializedObject dest) { if (source == null || dest == null) { return(0); } int counter = 0; var propIterator = source.GetIterator(); //jump into serialized object, this will skip script type so that we dont override the destination component's type if (propIterator.NextVisible(true)) { while (propIterator.NextVisible(true)) //iterate through all serializedProperties { //try obtaining the property in destination component var propElement = dest.FindProperty(propIterator.name); //validate that the properties are present in both components, and that they're the same type if (propElement != null && propElement.propertyType == propIterator.propertyType) { //copy value from source to destination component dest.CopyFromSerializedProperty(propIterator); counter++; } } } return(counter); }
static void PropagateEvents(MenuCommand command) { // we create a temporary duplicate of the importer so we can change its serialization and fake whatever data we need to copy on the real one. var fakeImporter = new SerializedObject(Object.Instantiate(command.context)); var fakeAnimation = fakeImporter.FindProperty("m_ClipAnimations"); // get a serialization of the actual importer we want to modify. var importer = new SerializedObject(command.context); var animationsProperty = importer.FindProperty("m_ClipAnimations"); // force array size 1, so we remove any events information for other animations fakeAnimation.arraySize = 1; // set it back to the correct size, the serializedProperty system will then duplicate the last entry on each new element fakeAnimation.arraySize = animationsProperty.arraySize; // for each entry, lets take the fake one that got duplicated and paste it in the real importer. for (int i = 0; i < animationsProperty.arraySize; i++) { // get the duplicated event property var events = fakeAnimation.GetArrayElementAtIndex(i).FindPropertyRelative("events"); // apply it to the actual importer importer.CopyFromSerializedProperty(events); } importer.ApplyModifiedPropertiesWithoutUndo(); AssetDatabase.ImportAsset(((AssetImporter)command.context).assetPath); }
public static void CopyPastSerialized(Object componentSource, Object componentTarget) { var source = new SerializedObject(componentSource); //check if they're the same type - if so do the ordinary copy/paste if (source.targetObject.GetType() == componentTarget.GetType()) { EditorUtility.CopySerialized(source.targetObject, componentTarget); return; } SerializedObject dest = new SerializedObject(componentTarget); SerializedProperty prop_iterator = source.GetIterator(); //jump into serialized object, this will skip script type so that we dont override the destination component's type if (prop_iterator.NextVisible(true)) { while (prop_iterator.NextVisible(true)) //itterate through all serializedProperties { //try obtaining the property in destination component SerializedProperty prop_element = dest.FindProperty(prop_iterator.name); //validate that the properties are present in both components, and that they're the same type if (prop_element != null && prop_element.propertyType == prop_iterator.propertyType) { //copy value from source to destination component dest.CopyFromSerializedProperty(prop_iterator); } } } dest.ApplyModifiedProperties(); }
static void CopyEvents(MenuCommand command) { var paths = Selection.objects.Select(AssetDatabase.GetAssetPath).Where(p => !string.IsNullOrEmpty(p)); foreach (var path in paths) { var destination = new SerializedObject(AssetImporter.GetAtPath(path)); var importer = new SerializedObject(command.context); var animationsProperty = importer.FindProperty("m_ClipAnimations"); var eAnimationsProperty = destination.FindProperty("m_ClipAnimations"); for (int i = 0; i < animationsProperty.arraySize && i < eAnimationsProperty.arraySize; i++) { var events = animationsProperty.GetArrayElementAtIndex(i).FindPropertyRelative("events"); destination.CopyFromSerializedProperty(events); } destination.ApplyModifiedPropertiesWithoutUndo(); } AssetDatabase.StartAssetEditing(); foreach (var path in paths) { AssetDatabase.ImportAsset(path); } AssetDatabase.StopAssetEditing(); }
public static void DrawAnimationPresetField(Rect position, GUIContent label, SerializedProperty presetProperty, SerializedProperty animationDataProperty) { var oldPreset = presetProperty.objectReferenceValue as UIAnimationPreset; DrawAssetField <UIAnimationPreset>(position, null, presetProperty, (asset, created) => { if (created) { var so = new SerializedObject(asset); so.CopyFromSerializedProperty(animationDataProperty); so.FindProperty("m_AnimationData").FindPropertyRelative("m_Preset").objectReferenceValue = null; so.ApplyModifiedProperties(); } else if (oldPreset && !asset) { var so = new SerializedObject(oldPreset); animationDataProperty.serializedObject.CopyFromSerializedProperty(so.FindProperty("m_AnimationData")); animationDataProperty.serializedObject.ApplyModifiedProperties(); } else if (asset) { animationDataProperty.FindPropertyRelative("m_TweenDatas").ClearArray(); animationDataProperty.serializedObject.ApplyModifiedProperties(); } }); }
/// <summary> /// Updates all non modified properties in this serialziedObject from the prototype. /// </summary> public void UpdateNonModifiedProperties() { SerializedProperty prop = serializedObject.GetIterator(); if (prop.NextVisible(true)) { do { if (prop.name != "m_Script" && prop.name != "prototype" && prop.name != "modifiedValues") { bool isPropertyModified = IsPropertyModified(prop.name); if (!PropertyExistsInPrototype(prop.name)) { isPropertyModified = true; } if (!isPropertyModified && prototypeSerialized != null) { serializedObject.CopyFromSerializedProperty(prototypeSerialized.FindProperty(prop.name)); } } }while (prop.NextVisible(false)); } serializedObject.ApplyModifiedPropertiesWithoutUndo(); }
public static void SaveOrUpdate <TAsset>(this TAsset asset, string path) where TAsset : Object { var existing = AssetDatabase.LoadAssetAtPath <TAsset>(path); if (existing) { var srcObj = new SerializedObject(asset); var dstObj = new SerializedObject(existing); var propIt = srcObj.GetIterator(); if (propIt.Next(true)) { while (propIt.Next(false)) { dstObj.CopyFromSerializedProperty(propIt); } } dstObj.ApplyModifiedProperties(); } else { AssetDatabase.CreateAsset(asset, path); } }
public override void OnGUI(string searchContext) { using (var changeCheck = new EditorGUI.ChangeCheckScope()) { var settings = new SerializedObject(CellTypeSettings.instance); settings.Update(); var typesProperty = settings.FindProperty("m_cellTypes"); // For some reason ScriptableSingletons serialize with editable == false, // which makes EditorGUILayout.PropertyField readonly. We use an intermediary // object to get around this. var dummyObject = ScriptableObject.CreateInstance <DummyArray>(); { var dummy = new SerializedObject(dummyObject); dummy.CopyFromSerializedProperty(typesProperty); var dummyTypesProperty = dummy.FindProperty("m_cellTypes"); EditorGUILayout.PropertyField(dummyTypesProperty); settings.CopyFromSerializedProperty(dummyTypesProperty); } Object.DestroyImmediate(dummyObject); if (changeCheck.changed) { settings.ApplyModifiedProperties(); CellTypeSettings.instance.Save(); } } }
public bool Apply(SerializedObject toObject) { toObject.CopyFromSerializedProperty(TemplateSerializedProperty); if (!toObject.ApplyModifiedProperties()) { Debug.LogError("Copying of SerialisedProperty failed for - " + toObject.targetObject.name); return(false); } return(true); }
//public static UnityObject DuplicateAsset( UnityObject obj, string prefix = "" ) { // Type type = obj.GetType(); // var path = AssetDatabase.GetAssetPath( obj ); // var dir = Path.GetDirectoryName( path ); // var fname = Path.GetFileName( path ); // var newPath = dir + "/" + prefix + fname; // var uniquePath = AssetDatabase.GenerateUniqueAssetPath( newPath ); // AssetDatabase.CopyAsset( path, uniquePath ); // AssetDatabase.Refresh(); // var asset = AssetDatabase.LoadAssetAtPath( uniquePath, type ); // return asset; //} // preview #if UNITY_2018_3_OR_NEWER // IsSubAsset public static UnityObject DuplicateAsset2(UnityObject obj, string prefix = "") { if (!AssetDatabase.IsSubAsset(obj)) { return(DuplicateAsset(obj)); } void _pasteparameters(UnityObject src, UnityObject dst) { if (src.GetType() == dst.GetType()) { var so1 = new SerializedObject(dst); var so2 = new SerializedObject(src); so2.Update(); so1.Update(); var it = so2.GetIterator(); it.NextVisible(true); while (it.NextVisible(true)) { //Debug.Log( it.name ); var prop = so2.FindProperty(it.name); if (prop == null) { continue; } so1.CopyFromSerializedProperty(prop); } so1.ApplyModifiedProperties(); } else { //Debug.LogError( $"{obj.GetType().Name} != {copyObject.GetType().Name}: {S._Typedoesnotmatch}" ); } } Type type = obj.GetType(); var path = AssetDatabase.GetAssetPath(obj); var dir = Path.GetDirectoryName(path); var fname = obj.name.IsEmpty() ? type.Name : obj.name; var newPath = $"{dir}/{prefix}{fname}.asset"; var uniquePath = AssetDatabase.GenerateUniqueAssetPath(newPath); //AssetDatabase.CopyAsset( path, uniquePath ); var newobj = UnityObject.Instantiate(obj); AssetDatabase.CreateAsset(newobj, uniquePath); _pasteparameters(obj, newobj); AssetDatabase.Refresh(); var asset = AssetDatabase.LoadAssetAtPath(uniquePath, type); return(asset); }
private static void CheckSettingsVersion() { if (HQ.settings != null && HQ.settings.version != NGSettings.Version) { EditorApplication.delayCall += () => { if (EditorUtility.DisplayDialog(Constants.PackageTitle, string.Format(LC.G("Preferences_AskResetSettings"), HQ.settings.version, NGSettings.Version), LC.G("Yes"), LC.G("No")) == true) { GUICallbackWindow.Open(() => { SerializedObject obj = new SerializedObject(HQ.settings); NGSettings newSettings = ScriptableObject.CreateInstance <NGSettings>(); newSettings.hideFlags = HQ.settings.hideFlags; if (NGSettings.sharedSettings == HQ.settings) { File.Delete(NGSettings.GetSharedSettingsPath()); NGSettings.sharedSettings = newSettings; } SerializedObject newObject = new SerializedObject(newSettings); SerializedProperty it = obj.GetIterator(); it.Next(true); SerializedProperty end = it.GetEndProperty(); while (SerializedProperty.EqualContents(it, end) == false && it.Next(true) == true) { newObject.CopyFromSerializedProperty(it); } newObject.ApplyModifiedProperties(); string path = AssetDatabase.GetAssetPath(HQ.settings.GetInstanceID()); if (string.IsNullOrEmpty(path) == false) { AssetDatabase.CreateAsset(newSettings, path); } HQ.settings = newSettings; if (HQ.SettingsChanged != null) { HQ.SettingsChanged(); } }); } }; } }
private void Copy(Object source, Object dest) { if (source == null) { Debug.LogError($"{nameof(source)} is null"); return; } if (dest == null) { Debug.LogError($"{nameof(dest)} is null"); return; } // If the types are the same, then we can use the built-in Unity method. if (source == dest) { EditorUtility.CopySerialized(source, dest); return; } var sourceSo = new SerializedObject(source); var destSo = new SerializedObject(dest); var sourceIterator = sourceSo.GetIterator(); // We should skip the first iteration, since it contains a type of the script that we do not need to overwrite. if (sourceIterator.NextVisible(true)) { while (sourceIterator.NextVisible(true)) { // Trying to find the same property in the destination component. var destProperty = destSo.FindProperty(sourceIterator.propertyPath); if (destProperty == null) { continue; } // If the destination property has different type, then we should ignore it. if (destProperty.propertyType != sourceIterator.propertyType) { continue; } // Finally, copying the property value from the original component. destSo.CopyFromSerializedProperty(sourceIterator); } destSo.ApplyModifiedPropertiesWithoutUndo(); } }