public static MountResult MountAccessory( Outfit outfit, Accessory accessory, bool singleUndo = true, string undoLabel = null) { if (AssetDatabase.Contains(outfit)) { Debug.LogError("Can't modify an outfit asset. Outfit must be in the scene.", outfit); return(MountResult.FailedOnError); } if (singleUndo) { Undo.IncrementCurrentGroup(); } undoLabel = string.IsNullOrEmpty(undoLabel) ? "Add Accessory" : undoLabel; Undo.RecordObjects(Outfit.UnsafeGetUndoObjects(outfit).ToArray(), undoLabel); bool isNew = AssetDatabase.Contains(accessory); if (isNew) { var name = accessory.name; accessory = accessory.Instantiate(); accessory.name = name; // Record undo later. } else { Undo.RecordObjects(Accessory.UnsafeGetUndoObjects(accessory).ToArray(), undoLabel); } var origParent = accessory.transform.parent; var result = outfit.Mount(accessory); if (result.IsFailed()) { Debug.LogWarningFormat(outfit, "Accessory mount failed: {0}: {1}", accessory.name, result); if (isNew) { accessory.gameObject.SafeDestroy(); } accessory = null; } else { if (isNew) { Undo.RegisterCreatedObjectUndo(accessory.gameObject, undoLabel); } var parent = accessory.transform.parent; accessory.transform.parent = origParent; Undo.SetTransformParent(accessory.transform, parent, undoLabel); } if (singleUndo) { Undo.CollapseUndoOperations(Undo.GetCurrentGroup()); } return(result); }
/// <summary> /// Properly adds the accessory to the body's while in editor mode. (Includes undo.) /// </summary> /// <remarks> /// <para> /// <paramref name="singleUndo"/> will create a single undo action. If calling this method is part of /// a larger operation that needs a single undo, then set <paramref name="singleUndo"/> to false. Behavior /// is undefined if <paramref name="singleUndo"/> is false and the caller does not properly group this method's /// actions. /// </para> /// </remarks> /// <param name="body">The body. (Required. Must be a scene object.)</param> /// <param name="outfit">The accessory. (Required)</param> /// <param name="singleUndo"> /// If true, will group all actions into a single undo. Otherwise the caller must properly group the undos. /// </param> /// <param name="undoLabel">The label to use for the undo, or null to use the default label.</param> /// <returns>True if the outfit was successfully set.</returns> public static bool AddAccessory( Body body, Accessory accessory, bool singleUndo = true, string undoLabel = null) { if (AssetDatabase.Contains(body)) { Debug.LogError("Can't modify a body asset. Body must be in the scene.", body); return(false); } if (singleUndo) { Undo.IncrementCurrentGroup(); } undoLabel = string.IsNullOrEmpty(undoLabel) ? "Add Accessory" : undoLabel; Undo.RecordObjects(Body.UnsafeGetUndoObjects(body).ToArray(), undoLabel); bool isNew = AssetDatabase.Contains(accessory); if (isNew) { var name = accessory.name; accessory = accessory.Instantiate(); accessory.name = name; // Record undo later. } else { Undo.RecordObjects(Accessory.UnsafeGetUndoObjects(accessory).ToArray(), undoLabel); } var origParent = accessory.transform.parent; bool isDirty = false; if (body.Accessories.Add(accessory).IsFailed()) { if (isNew) { accessory.gameObject.SafeDestroy(); } accessory = null; } else { if (isNew) { Undo.RegisterCreatedObjectUndo(accessory.gameObject, undoLabel); } var parent = accessory.transform.parent; accessory.transform.parent = origParent; Undo.SetTransformParent(accessory.transform, parent, undoLabel); isDirty = true; } if (singleUndo) { Undo.CollapseUndoOperations(Undo.GetCurrentGroup()); } return(isDirty); }