/// <summary> /// Initializes a new instance of the /// <see cref="PropertyBackingFieldDrawer+HashableSerializedProperty"/> struct. /// </summary> /// <param name="mod">Property modification.</param> public HashableSerializedProperty(PropertyModification mod) : this(mod.propertyPath, mod.target) { }
SerializedProperty SerializeMod(PropertyModification mod) { SerializedObject ser_obj = new UnityEditor.SerializedObject(mod.target); return(ser_obj.FindProperty(mod.propertyPath)); }
static private void CollectRotationModifications(IAnimationRecordingState state, ref UndoPropertyModification[] modifications, ref Dictionary <object, RotationModification> rotationModifications) { List <UndoPropertyModification> outModifs = new List <UndoPropertyModification>(); foreach (var modification in modifications) { PropertyModification prop = modification.previousValue; if (!(prop.target is Transform)) { outModifs.Add(modification); continue; } EditorCurveBinding binding = new EditorCurveBinding(); AnimationUtility.PropertyModificationToEditorCurveBinding(prop, state.activeRootGameObject, out binding); if (binding.propertyName.StartsWith(kLocalRotation)) { RotationModification rotationModification; if (!rotationModifications.TryGetValue(prop.target, out rotationModification)) { rotationModification = new RotationModification(); rotationModifications[prop.target] = rotationModification; } if (binding.propertyName.EndsWith("x")) { rotationModification.x = modification; } else if (binding.propertyName.EndsWith("y")) { rotationModification.y = modification; } else if (binding.propertyName.EndsWith("z")) { rotationModification.z = modification; } else if (binding.propertyName.EndsWith("w")) { rotationModification.w = modification; } rotationModification.lastQuatModification = modification; } else if (prop.propertyPath.StartsWith(kLocalEulerAnglesHint)) { RotationModification rotationModification; if (!rotationModifications.TryGetValue(prop.target, out rotationModification)) { rotationModification = new RotationModification(); rotationModifications[prop.target] = rotationModification; } rotationModification.useEuler = true; if (prop.propertyPath.EndsWith("x")) { rotationModification.eulerX = modification; } else if (prop.propertyPath.EndsWith("y")) { rotationModification.eulerY = modification; } else if (prop.propertyPath.EndsWith("z")) { rotationModification.eulerZ = modification; } } else { outModifs.Add(modification); } } if (rotationModifications.Count > 0) { modifications = outModifs.ToArray(); } }
private static void AddKey(IAnimationRecordingState state, EditorCurveBinding binding, Type type, PropertyModification modification) { GameObject activeRootGameObject = state.activeRootGameObject; AnimationClip activeAnimationClip = state.activeAnimationClip; if ((activeAnimationClip.hideFlags & HideFlags.NotEditable) != HideFlags.None) { return; } AnimationWindowCurve animationWindowCurve = new AnimationWindowCurve(activeAnimationClip, binding, type); object currentValue = CurveBindingUtility.GetCurrentValue(activeRootGameObject, binding); if (animationWindowCurve.length == 0) { object value = null; if (!AnimationRecording.ValueFromPropertyModification(modification, binding, out value)) { value = currentValue; } if (state.frame != 0) { AnimationWindowUtility.AddKeyframeToCurve(animationWindowCurve, value, type, AnimationKeyTime.Frame(0, activeAnimationClip.frameRate)); } } AnimationWindowUtility.AddKeyframeToCurve(animationWindowCurve, currentValue, type, AnimationKeyTime.Frame(state.frame, activeAnimationClip.frameRate)); state.SaveCurve(animationWindowCurve); }
private static void CollectRotationModifications(IAnimationRecordingState state, ref UndoPropertyModification[] modifications, ref Dictionary <object, AnimationRecording.RotationModification> rotationModifications) { List <UndoPropertyModification> list = new List <UndoPropertyModification>(); UndoPropertyModification[] array = modifications; for (int i = 0; i < array.Length; i++) { UndoPropertyModification undoPropertyModification = array[i]; EditorCurveBinding editorCurveBinding = default(EditorCurveBinding); PropertyModification previousValue = undoPropertyModification.previousValue; AnimationUtility.PropertyModificationToEditorCurveBinding(previousValue, state.activeRootGameObject, out editorCurveBinding); if (editorCurveBinding.propertyName.StartsWith("m_LocalRotation")) { AnimationRecording.RotationModification rotationModification; if (!rotationModifications.TryGetValue(previousValue.target, out rotationModification)) { rotationModification = new AnimationRecording.RotationModification(); rotationModifications[previousValue.target] = rotationModification; } if (editorCurveBinding.propertyName.EndsWith("x")) { rotationModification.x = undoPropertyModification; } else if (editorCurveBinding.propertyName.EndsWith("y")) { rotationModification.y = undoPropertyModification; } else if (editorCurveBinding.propertyName.EndsWith("z")) { rotationModification.z = undoPropertyModification; } else if (editorCurveBinding.propertyName.EndsWith("w")) { rotationModification.w = undoPropertyModification; } rotationModification.lastQuatModification = undoPropertyModification; } else if (previousValue.propertyPath.StartsWith("m_LocalEulerAnglesHint")) { AnimationRecording.RotationModification rotationModification2; if (!rotationModifications.TryGetValue(previousValue.target, out rotationModification2)) { rotationModification2 = new AnimationRecording.RotationModification(); rotationModifications[previousValue.target] = rotationModification2; } rotationModification2.useEuler = true; if (previousValue.propertyPath.EndsWith("x")) { rotationModification2.eulerX = undoPropertyModification; } else if (previousValue.propertyPath.EndsWith("y")) { rotationModification2.eulerY = undoPropertyModification; } else if (previousValue.propertyPath.EndsWith("z")) { rotationModification2.eulerZ = undoPropertyModification; } list.Add(undoPropertyModification); } else { list.Add(undoPropertyModification); } } if (rotationModifications.Count > 0) { modifications = list.ToArray(); } }
public void AddPropertyModification(EditorCurveBinding binding, PropertyModification propertyModification, bool keepPrefabOverride) { AnimationMode.AddPropertyModification(binding, propertyModification, keepPrefabOverride); }
private static void CollectRotationModifications(AnimationWindowState state, ref UndoPropertyModification[] modifications, ref Dictionary <object, AnimationRecording.RotationModification> rotationModifications) { List <UndoPropertyModification> propertyModificationList = new List <UndoPropertyModification>(); foreach (UndoPropertyModification propertyModification in modifications) { EditorCurveBinding binding = new EditorCurveBinding(); PropertyModification previousValue = propertyModification.previousValue; AnimationUtility.PropertyModificationToEditorCurveBinding(previousValue, state.activeRootGameObject, out binding); if (binding.propertyName.StartsWith("m_LocalRotation")) { AnimationRecording.RotationModification rotationModification; if (!rotationModifications.TryGetValue((object)previousValue.target, out rotationModification)) { rotationModification = new AnimationRecording.RotationModification(); rotationModifications[(object)previousValue.target] = rotationModification; } if (binding.propertyName.EndsWith("x")) { rotationModification.x = propertyModification; } else if (binding.propertyName.EndsWith("y")) { rotationModification.y = propertyModification; } else if (binding.propertyName.EndsWith("z")) { rotationModification.z = propertyModification; } else if (binding.propertyName.EndsWith("w")) { rotationModification.w = propertyModification; } rotationModification.lastQuatModification = propertyModification; } else if (previousValue.propertyPath.StartsWith("m_LocalEulerAnglesHint")) { AnimationRecording.RotationModification rotationModification; if (!rotationModifications.TryGetValue((object)previousValue.target, out rotationModification)) { rotationModification = new AnimationRecording.RotationModification(); rotationModifications[(object)previousValue.target] = rotationModification; } if (previousValue.propertyPath.EndsWith("x")) { rotationModification.eulerX = propertyModification; } else if (previousValue.propertyPath.EndsWith("y")) { rotationModification.eulerY = propertyModification; } else if (previousValue.propertyPath.EndsWith("z")) { rotationModification.eulerZ = propertyModification; } } else { propertyModificationList.Add(propertyModification); } } if (propertyModificationList.Count <= 0) { return; } modifications = propertyModificationList.ToArray(); }
static UndoPropertyModification PropertyModificationToUndoPropertyModification(PropertyModification prop) { return(new UndoPropertyModification { previousValue = prop, currentValue = new PropertyModification { objectReference = prop.objectReference, propertyPath = prop.propertyPath, target = prop.target, value = prop.value }, keepPrefabOverride = true }); }
public static string ConvertPrefabPropertyModificationToString(GameObject prefab, PropertyModification propertyModification) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(ObjectUtility.CalculateTransformIndexPathStringReverseOrder(ObjectUtility.FindTransform(propertyModification.target), prefab.transform)) .Append(propertyModification.propertyPath) .Append("_") .Append(propertyModification.value); if (propertyModification.objectReference != null) { stringBuilder.Append("_"); Object reference = propertyModification.objectReference; int instanceId = reference.GetInstanceID(); string assetPath = AssetDatabase.GetAssetPath(reference); if (string.IsNullOrEmpty(assetPath)) { } else { stringBuilder.Append(instanceId); if (AssetDatabase.IsForeignAsset(reference) && !AssetDatabase.IsMainAsset(reference)) { Transform referenceTrans = ObjectUtility.FindTransform(reference); if (referenceTrans == null) { referenceTrans = ObjectUtility.FindTransform(propertyModification.target); } stringBuilder.Append("_") .Append(ObjectUtility.CalculateTransformIndexPathStringReverseOrder(referenceTrans)); } } } return(stringBuilder.ToString()); }
public static void AddInstanceComponentToPrefab(Component obj) { var gameObject = PrefabUtility.FindPrefabRoot(GetGameObject(obj)); var prefab = (GameObject)PrefabUtility.GetPrefabParent(gameObject); foreach (var i in GetRequiredComponents(obj)) { if (i && PrefabUtility.GetPrefabParent(i) == null && prefab.GetComponent(i.GetType()) == null) { AddInstanceComponentToPrefab(i); } } // add the prefab to the target var prefabComp = prefab.AddComponent(obj.GetType()); if (prefabComp) { var mods = new List <PropertyModification>(PrefabUtility.GetPropertyModifications(gameObject)); // copy data EditorUtility.CopySerialized(obj, prefabComp); // fix up any references to their prefabs SerializedObject so = new SerializedObject(obj); SerializedObject prefabCompSo = new SerializedObject(prefabComp); prefabCompSo.Update(); so.Update(); var i = so.GetIterator(); while (i.Next(true)) { if (i.propertyType == SerializedPropertyType.ObjectReference) { if (i.propertyPath == "m_PrefabParentObject" || i.propertyPath == "m_PrefabInternal" || i.propertyPath == "m_GameObject" || i.propertyPath == "m_Script") { continue; } var prefabRef = PrefabUtility.GetPrefabParent(i.objectReferenceValue); var prefabRefRoot = PrefabUtility.GetPrefabParent(PrefabUtility.FindPrefabRoot(PrefabUtilityEx.GetGameObject(i.objectReferenceValue))); var prefabType = i.objectReferenceValue != null?PrefabUtility.GetPrefabType(i.objectReferenceValue) : PrefabType.None; if (prefabType != PrefabType.Prefab && prefabType != PrefabType.ModelPrefab) { // link to an object in the scene. // we must add a modification for this. if (i.objectReferenceValue != null && (prefabRef == null || prefab != prefabRefRoot)) { var propertyMod = new PropertyModification(); propertyMod.objectReference = i.objectReferenceValue; propertyMod.target = prefabComp; propertyMod.propertyPath = i.propertyPath; mods.Add(propertyMod); prefabCompSo.FindProperty(i.propertyPath).objectReferenceValue = null; } else { prefabCompSo.FindProperty(i.propertyPath).objectReferenceValue = prefabRef; } } } } so = null; // save prefabCompSo.ApplyModifiedProperties(); EditorUtility.SetDirty(prefab); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(prefab)); // remove the applied value from our instance. UnityEngine.GameObject.DestroyImmediate(obj); // add property mods var components = gameObject.GetComponents(prefabComp.GetType()); Component newComponent = System.Array.Find(components, ((arg) => PrefabUtility.GetPrefabParent(arg) == prefabComp)); PrefabUtility.SetPropertyModifications(newComponent, mods.ToArray()); } else { EditorUtility.DisplayDialog("Error", "Can't apply this component to the prefab as it cannot own more than one prefab of this type", "Ok"); } }
static void SetupPropertyModification(string name, float value, PropertyModification prop) { prop.propertyPath = kMaterialPrefix + name; prop.value = value.ToString(); }
static bool ComparePropertyModifications(PropertyModification a, PropertyModification b) { return(a.objectReference == b.objectReference && a.propertyPath == b.propertyPath && a.target == b.target && a.value == b.value); }
/* * public static void ApplySpecificChanges (GameObject obj, PropertyModification[] changes) * { * * } */ public static void RevertSpecificChanges(Object obj, PropertyModification[] changes) { var root = PrefabUtility.FindPrefabRoot(GetGameObject(obj)); var modsArray = PrefabUtility.GetPropertyModifications(root); var mods = new List <PropertyModification>(modsArray != null ? modsArray:new PropertyModification[0]); if (obj == root.transform) { var prefabTForm = (Transform)PrefabUtility.GetPrefabParent(root.transform); var baseMods = new PropertyModification[] { new PropertyModification() { propertyPath = "m_LocalPosition.x", target = prefabTForm, value = prefabTForm.localPosition.x.ToString() }, new PropertyModification() { propertyPath = "m_LocalPosition.y", target = prefabTForm, value = prefabTForm.localPosition.y.ToString() }, new PropertyModification() { propertyPath = "m_LocalPosition.z", target = prefabTForm, value = prefabTForm.localPosition.z.ToString() }, new PropertyModification() { propertyPath = "m_LocalRotation.x", target = prefabTForm, value = prefabTForm.localRotation.x.ToString() }, new PropertyModification() { propertyPath = "m_LocalRotation.y", target = prefabTForm, value = prefabTForm.localRotation.y.ToString() }, new PropertyModification() { propertyPath = "m_LocalRotation.z", target = prefabTForm, value = prefabTForm.localRotation.z.ToString() }, new PropertyModification() { propertyPath = "m_LocalRotation.w", target = prefabTForm, value = prefabTForm.localRotation.w.ToString() }, new PropertyModification() { propertyPath = "m_LocalScale.x", target = prefabTForm, value = prefabTForm.localScale.x.ToString() }, new PropertyModification() { propertyPath = "m_LocalScale.y", target = prefabTForm, value = prefabTForm.localScale.y.ToString() }, new PropertyModification() { propertyPath = "m_LocalScale.z", target = prefabTForm, value = prefabTForm.localScale.z.ToString() } }; mods = new List <PropertyModification>(baseMods); } else { foreach (var i in changes) { mods.RemoveAll((a) => ComparePropertyModifications(i, a)); } } var newChanges = mods.ToArray(); PrefabUtility.SetPropertyModifications(obj, newChanges); }
private static void AddKey(AnimationWindowState state, EditorCurveBinding binding, Type type, PropertyModification modification) { GameObject rootGameObject = state.m_RootGameObject; AnimationClip activeAnimationClip = state.m_ActiveAnimationClip; AnimationWindowCurve animationWindowCurve = new AnimationWindowCurve(activeAnimationClip, binding, type); object currentValue = AnimationWindowUtility.GetCurrentValue(rootGameObject, binding); object value = null; if (animationWindowCurve.length == 0 && state.m_Frame != 0) { if (!AnimationRecording.ValueFromPropertyModification(modification, binding, out value)) { value = currentValue; } AnimationWindowUtility.AddKeyframeToCurve(animationWindowCurve, value, type, AnimationKeyTime.Frame(0, activeAnimationClip.frameRate)); } AnimationWindowUtility.AddKeyframeToCurve(animationWindowCurve, currentValue, type, AnimationKeyTime.Frame(state.m_Frame, activeAnimationClip.frameRate)); state.SaveCurve(animationWindowCurve); }
/// <summary> /// Reverts the given property on the instance to the prefab value. /// </summary> /// <param name="instance">The prefab instance to revert the value on.</param> /// <param name="property">The property to revert.</param> public static void RevertValue(object instance, InspectedProperty property) { // We only want top-level components if (instance is MonoBehaviour == false) { return; } // Not a prefab var prefabGameObject = (GameObject)PrefabUtility.GetPrefabParent(((MonoBehaviour)instance).gameObject); if (prefabGameObject == null) { return; } // Get all of the property modifications on the object. If there are no property // modifications, then there is nothing to revert. PropertyModification[] mods = PrefabUtility.GetPropertyModifications((UnityObject)instance); if (mods == null) { return; } ISerializedObject serializedInstance = (ISerializedObject)instance; bool removed = false; for (int i = 0; i < mods.Length; ++i) { PropertyModification mod = mods[i]; // A property modification can take one of two forms. It can either be modifying a // Unity serialized value or a Full Inspector serialized value. // Check to see if it's a Full Inspector serialized value. If it is, then we lookup // the key that the modification is associated with and, if we find said key, and // that the key is equal to the property we are checking for, then we return true. string serializedPropertyName; if (TryExtractPropertyName(serializedInstance, mod, out serializedPropertyName) && serializedPropertyName == property.Name) { removed = true; } // Check to see if it is a Unity serialized value. We have to do a dotted comparison // because the propertyPath may be associated with, ie, an array, which in that case // the path is something like "values.Array._items[0]" while property.Name is just // "values". if (ContainsPropertyName(mod.propertyPath, property.Name)) { removed = true; } if (removed) { ArrayUtility.RemoveAt(ref mods, i); PrefabUtility.SetPropertyModifications((UnityObject)instance, mods); break; } } }
private static void AddKey(AnimationWindowState state, EditorCurveBinding binding, Type type, PropertyModification modification) { GameObject activeRootGameObject = state.activeRootGameObject; AnimationClip activeAnimationClip = state.activeAnimationClip; AnimationWindowCurve curve = new AnimationWindowCurve(activeAnimationClip, binding, type); object currentValue = CurveBindingUtility.GetCurrentValue(activeRootGameObject, binding); if (curve.length == 0) { object outObject = null; if (!ValueFromPropertyModification(modification, binding, out outObject)) { outObject = currentValue; } if (state.frame != 0) { AnimationWindowUtility.AddKeyframeToCurve(curve, outObject, type, AnimationKeyTime.Frame(0, activeAnimationClip.frameRate)); } } AnimationWindowUtility.AddKeyframeToCurve(curve, currentValue, type, AnimationKeyTime.Frame(state.frame, activeAnimationClip.frameRate)); state.SaveCurve(curve); }
public bool DiscardModification(PropertyModification modification) { return(false); }
static void AddKey(IAnimationRecordingState state, EditorCurveBinding binding, Type type, PropertyModification modification) { GameObject root = state.activeRootGameObject; AnimationClip clip = state.activeAnimationClip; if ((clip.hideFlags & HideFlags.NotEditable) != 0) { return; } AnimationWindowCurve curve = new AnimationWindowCurve(clip, binding, type); // Add key at current frame object currentValue = CurveBindingUtility.GetCurrentValue(root, binding); if (state.addZeroFrame) { // Is it a new curve? if (curve.length == 0) { object oldValue = null; if (!ValueFromPropertyModification(modification, binding, out oldValue)) { oldValue = currentValue; } if (state.currentFrame != 0) { AnimationWindowUtility.AddKeyframeToCurve(curve, oldValue, type, AnimationKeyTime.Frame(0, clip.frameRate)); } } } AnimationWindowUtility.AddKeyframeToCurve(curve, currentValue, type, AnimationKeyTime.Frame(state.currentFrame, clip.frameRate)); state.SaveCurve(curve); }
public bool DiscardModification(PropertyModification modification) { return(!AnimationMode.IsPropertyAnimated(modification.target, modification.propertyPath)); }
/// <summary> /// <see cref="m_PrefabOverrideModificationMd5"/> /// </summary> private void CaculatePrefabOverrideModification() { if (!UnityEditor.PrefabUtility.HasPrefabInstanceAnyOverrides(gameObject, false)) { m_PrefabOverrideModificationMd5 = string.Empty; } else { List <string> modificationStrings = null; if (m_OwnerMap != null) { modificationStrings = m_OwnerMap.GetStringsCache(); } else { modificationStrings = new List <string>(); } if (m_Prefab == null) { m_Prefab = UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(gameObject); } PropertyModification[] modifications = UnityEditor.PrefabUtility.GetPropertyModifications(gameObject); for (int iModification = 0; iModification < modifications.Length; iModification++) { PropertyModification iterModification = modifications[iModification]; bool isOverrideModification; if (iterModification.target == m_Prefab) { isOverrideModification = !Constants.PREFAB_ROOT_GAMEOBJECT_IGNORE_MODIFICATION_PROPERTY_PATHS.Contains(iterModification.propertyPath); } else if (iterModification.target == m_Prefab.transform) { isOverrideModification = !Constants.PREFAB_ROOT_TRANSFORM_IGNORE_MODIFICATION_PROPERTY_PATHS.Contains(iterModification.propertyPath); } else if (CheckChildOverride(iterModification)) { isOverrideModification = false; } else { isOverrideModification = IsPrefabOverrideModification(iterModification); } if (isOverrideModification) { Debug.Log("sceneunit:" + gameObject.name); modificationStrings.Add(Leyoutech.Utility.PrefabUtility.ConvertPrefabPropertyModificationToString(m_Prefab, iterModification)); } } if (modificationStrings.Count > 0) { StringBuilder stringBuilder = StringUtility.AllocStringBuilderCache(); modificationStrings.Sort(); for (int iModification = 0; iModification < modificationStrings.Count; iModification++) { stringBuilder.Append(modificationStrings[iModification]); } modificationStrings.Clear(); m_PrefabOverrideModificationMd5 = StringUtility.CalculateMD5Hash(StringUtility.ReleaseStringBuilderCacheAndReturnString()); } else { m_PrefabOverrideModificationMd5 = string.Empty; } } }