示例#1
0
        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);
        }