Spawns the specified actor to the game (with undo). The actor. The parent actor. Set null as default. The order under the parent to put the spawned actor. True if automatically select the spawned actor, otherwise false.
(Actor actor, Actor parent = null, int orderInParent = -1, bool autoSelect = true)
| 330 | /// <param name="orderInParent">The order under the parent to put the spawned actor.</param> |
| 331 | /// <param name="autoSelect">True if automatically select the spawned actor, otherwise false.</param> |
| 332 | public void Spawn(Actor actor, Actor parent = null, int orderInParent = -1, bool autoSelect = true) |
| 333 | { |
| 334 | bool isPlayMode = Editor.StateMachine.IsPlayMode; |
| 335 | |
| 336 | if (Level.IsAnySceneLoaded == false) |
| 337 | throw new InvalidOperationException("Cannot spawn actor when no scene is loaded."); |
| 338 | |
| 339 | SpawnBegin?.Invoke(); |
| 340 | |
| 341 | // During play in editor mode spawned actors should be dynamic (user can move them) |
| 342 | if (isPlayMode) |
| 343 | actor.StaticFlags = StaticFlags.None; |
| 344 | |
| 345 | // Add it |
| 346 | Level.SpawnActor(actor, parent); |
| 347 | |
| 348 | // Set order if given |
| 349 | if (orderInParent != -1) |
| 350 | actor.OrderInParent = orderInParent; |
| 351 | |
| 352 | // Peek spawned node |
| 353 | var actorNode = Editor.Instance.Scene.GetActorNode(actor); |
| 354 | if (actorNode == null) |
| 355 | throw new InvalidOperationException("Failed to create scene node for the spawned actor."); |
| 356 | |
| 357 | // Call post spawn action (can possibly setup custom default values) |
| 358 | actorNode.PostSpawn(); |
| 359 | |
| 360 | // Create undo action |
| 361 | IUndoAction action = new DeleteActorsAction(actorNode, true); |
| 362 | if (autoSelect) |
| 363 | { |
| 364 | var before = Selection.ToArray(); |
| 365 | Selection.Clear(); |
| 366 | Selection.Add(actorNode); |
| 367 | OnSelectionChanged(); |
| 368 | action = new MultiUndoAction(action, new SelectionChangeAction(before, Selection.ToArray(), OnSelectionUndo)); |
| 369 | } |
| 370 | Undo.AddAction(action); |
| 371 | |
| 372 | // Mark scene as dirty |
| 373 | Editor.Scene.MarkSceneEdited(actor.Scene); |
| 374 | |
| 375 | SpawnEnd?.Invoke(); |
| 376 | |
| 377 | OnDirty(actorNode); |
| 378 | } |
| 379 | |
| 380 | /// <summary> |
| 381 | /// Converts the selected actor to another type. |
no test coverage detected