GameObjectUndo.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. using bs;
  7. namespace bs.Editor
  8. {
  9. /** @addtogroup Utility-Editor
  10. * @{
  11. */
  12. /// <summary>
  13. /// Handles undo & redo operations for changes made on game objects. Game objects can be recorded just before a change
  14. /// is made and the system will calculate the difference between that state and the state at the end of current frame.
  15. /// This difference will then be recorded as a undo/redo operation. The undo/redo operation will also take care of
  16. /// selecting the object & field it is acting upon.
  17. /// </summary>
  18. internal class GameObjectUndo
  19. {
  20. /// <summary>
  21. /// Contains information about a component that needs its diff recorded.
  22. /// </summary>
  23. private struct ComponentToRecord
  24. {
  25. private Component obj;
  26. private string path;
  27. private SerializedObject orgState;
  28. /// <summary>
  29. /// Creates a new object instance, recording the current state of the component.
  30. /// </summary>
  31. /// <param name="obj">Component to record the state of.</param>
  32. /// <param name="path">
  33. /// Path to the field which should be focused when performing the undo/redo operation. This should be the path
  34. /// as provided by <see cref="InspectableField"/>.
  35. /// </param>
  36. internal ComponentToRecord(Component obj, string path)
  37. {
  38. this.obj = obj;
  39. this.path = path;
  40. orgState = SerializedObject.Create(obj);
  41. }
  42. /// <summary>
  43. /// Generates the diff from the previously recorded state and the current state. If there is a difference
  44. /// an undo command is recorded.
  45. /// </summary>
  46. internal void RecordCommand()
  47. {
  48. if (obj.IsDestroyed)
  49. return;
  50. SerializedObject newState = SerializedObject.Create(obj);
  51. SerializedDiff oldToNew = SerializedDiff.Create(orgState, newState);
  52. if (oldToNew == null || oldToNew.IsEmpty)
  53. return;
  54. SerializedDiff newToOld = SerializedDiff.Create(newState, orgState);
  55. UndoRedo.Global.RegisterCommand(new RecordComponentUndo(obj, path, oldToNew, newToOld));
  56. }
  57. }
  58. /// <summary>
  59. /// Contains information about scene objects that needs their diff recorded. Note this will not record the entire
  60. /// scene object, but rather just its name, transform, active state and potentially other similar properties.
  61. /// It's components as well as hierarchy state are ignored.
  62. /// </summary>
  63. private struct SceneObjectHeaderToRecord
  64. {
  65. private SceneObject[] objs;
  66. private string path;
  67. private SceneObjectState[] orgStates;
  68. /// <summary>
  69. /// Creates a new object instance, recording the current state of the scene object header.
  70. /// </summary>
  71. /// <param name="obj">Scene object to record the state of.</param>
  72. /// <param name="path">
  73. /// Path to the field which should be focused when performing the undo/redo operation.
  74. /// </param>
  75. internal SceneObjectHeaderToRecord(SceneObject obj, string path)
  76. {
  77. this.objs = new [] { obj };
  78. this.path = path;
  79. orgStates = new [] { SceneObjectState.Create(obj) };
  80. }
  81. /// <summary>
  82. /// Creates a new object instance, recording the current state of the scene object header for multiple scene
  83. /// objects.
  84. /// </summary>
  85. /// <param name="objs">Scene objects to record the state of.</param>
  86. /// <param name="path">
  87. /// Path to the field which should be focused when performing the undo/redo operation.
  88. /// </param>
  89. internal SceneObjectHeaderToRecord(SceneObject[] objs, string path)
  90. {
  91. this.objs = objs;
  92. this.path = path;
  93. orgStates = new SceneObjectState[objs.Length];
  94. for(int i = 0; i < orgStates.Length; i++)
  95. orgStates[i] = SceneObjectState.Create(objs[i]);
  96. }
  97. /// <summary>
  98. /// Generates the diff from the previously recorded state and the current state. If there is a difference
  99. /// an undo command is recorded.
  100. /// </summary>
  101. internal void RecordCommand()
  102. {
  103. if (objs == null)
  104. return;
  105. List<SceneObjectHeaderUndo> headers = new List<SceneObjectHeaderUndo>();
  106. for (int i = 0; i < objs.Length; i++)
  107. {
  108. SceneObject obj = objs[i];
  109. SceneObjectState orgState = orgStates[i];
  110. if (obj.IsDestroyed)
  111. continue;
  112. SceneObjectDiff oldToNew = SceneObjectDiff.Create(orgState, SceneObjectState.Create(obj));
  113. if (oldToNew.flags == 0)
  114. continue;
  115. SceneObjectDiff newToOld = SceneObjectDiff.Create(SceneObjectState.Create(obj), orgState);
  116. headers.Add(new SceneObjectHeaderUndo(obj, newToOld, oldToNew));
  117. }
  118. if (headers.Count > 0)
  119. UndoRedo.Global.RegisterCommand(new RecordSceneObjectHeaderUndo(headers, path));
  120. }
  121. }
  122. /// <summary>
  123. /// Contains information about a scene object that needs its diff recorded. Unlike
  124. /// <see cref="SceneObjectHeaderToRecord"/> this will record the entire scene object,
  125. /// including its components and optionally the child hierarchy.
  126. /// </summary>
  127. private struct SceneObjectToRecord
  128. {
  129. private SceneObject obj;
  130. private string description;
  131. private SerializedSceneObject orgState;
  132. /// <summary>
  133. /// Creates a new object instance, recording the current state of the scene object.
  134. /// </summary>
  135. /// <param name="obj">Scene object to record the state of.</param>
  136. /// <param name="hierarchy">If true, the child scene objects will be recorded as well.</param>
  137. /// <param name="description">
  138. /// Optional description that describes the change that is happening.
  139. /// </param>
  140. internal SceneObjectToRecord(SceneObject obj, bool hierarchy, string description)
  141. {
  142. this.obj = obj;
  143. this.description = description;
  144. orgState = new SerializedSceneObject(obj, hierarchy);
  145. }
  146. /// <summary>
  147. /// Generates the diff from the previously recorded state and the current state. If there is a difference
  148. /// an undo command is recorded.
  149. /// </summary>
  150. internal void RecordCommand()
  151. {
  152. if (obj.IsDestroyed)
  153. return;
  154. var newState = new SerializedSceneObject(obj);
  155. UndoRedo.Global.RegisterCommand(new RecordSceneObjectUndo(obj, orgState, newState, description));
  156. }
  157. }
  158. private static List<ComponentToRecord> components = new List<ComponentToRecord>();
  159. private static List<SceneObjectHeaderToRecord> sceneObjectHeaders = new List<SceneObjectHeaderToRecord>();
  160. private static List<SceneObjectToRecord> sceneObjects = new List<SceneObjectToRecord>();
  161. /// <summary>
  162. /// Records the current state of the provided component, and generates a diff with the next state at the end of the
  163. /// frame. If change is detected an undo operation will be recorded. Generally you want to call this just before
  164. /// you are about to make a change to the component.
  165. /// </summary>
  166. /// <param name="obj">Component to record the state of.</param>
  167. /// <param name="fieldPath">
  168. /// Path to the field which should be focused when performing the undo/redo operation. This should be the path
  169. /// as provided by <see cref="InspectableField"/>.
  170. /// </param>
  171. public static void RecordComponent(Component obj, string fieldPath)
  172. {
  173. ComponentToRecord cmp = new ComponentToRecord(obj, fieldPath);
  174. components.Add(cmp);
  175. }
  176. /// <summary>
  177. /// Records the current state of the provided scene object header, and generates a diff with the next state at the
  178. /// end of the frame. If change is detected an undo operation will be recorded. Generally you want to call this
  179. /// just before you are about to make a change to the scene object header.
  180. ///
  181. /// Note this will not record the entire scene object, but rather just its name, transform, active state and
  182. /// potentially other similar properties. It's components as well as hierarchy state are ignored.
  183. /// </summary>
  184. /// <param name="obj">Scene object to record the state of.</param>
  185. /// <param name="fieldName">
  186. /// Name to the field which should be focused when performing the undo/redo operation.
  187. /// </param>
  188. public static void RecordSceneObjectHeader(SceneObject obj, string fieldName)
  189. {
  190. SceneObjectHeaderToRecord so = new SceneObjectHeaderToRecord(obj, fieldName);
  191. sceneObjectHeaders.Add(so);
  192. }
  193. /// <summary>
  194. /// Records the current state of the provided scene object header, and generates a diff with the next state at the
  195. /// end of the frame. If change is detected an undo operation will be recorded. Generally you want to call this
  196. /// just before you are about to make a change to the scene object header.
  197. ///
  198. /// Note this will not record the entire scene object, but rather just its name, transform, active state and
  199. /// potentially other similar properties. It's components as well as hierarchy state are ignored.
  200. /// </summary>
  201. /// <param name="objs">Scene objects to record the state of.</param>
  202. public static void RecordSceneObjectHeader(SceneObject[] objs)
  203. {
  204. SceneObjectHeaderToRecord so = new SceneObjectHeaderToRecord(objs, null);
  205. sceneObjectHeaders.Add(so);
  206. }
  207. /// <summary>
  208. /// Records the current state of the provided scene object header, and generates a diff with the next state at the
  209. /// end of the frame. If change is detected an undo operation will be recorded. Generally you want to call this
  210. /// just before you are about to make a change to the scene object.
  211. ///
  212. /// Note this records the complete state of a scene object, including its header, its components and optionally its
  213. /// child hierarchy.
  214. /// </summary>
  215. /// <param name="obj">Scene object to record.</param>
  216. /// <param name="hierarchy">
  217. /// If true the child objects will be recorded as well, otherwise just the provided object.
  218. /// </param>
  219. /// <param name="description">Optional description specifying the type of changes about to be made.</param>
  220. public static void RecordSceneObject(SceneObject obj, bool hierarchy, string description)
  221. {
  222. SceneObjectToRecord so = new SceneObjectToRecord(obj, hierarchy, description);
  223. sceneObjects.Add(so);
  224. }
  225. /// <summary>
  226. /// Generates diffs for any objects that were previously recorded using any of the Record* methods. The diff is
  227. /// generated by comparing the state at the time Record* was called, compared to the current object state.
  228. /// </summary>
  229. public static void ResolveDiffs()
  230. {
  231. foreach (var entry in components)
  232. entry.RecordCommand();
  233. foreach (var entry in sceneObjectHeaders)
  234. entry.RecordCommand();
  235. foreach (var entry in sceneObjects)
  236. entry.RecordCommand();
  237. components.Clear();
  238. sceneObjectHeaders.Clear();
  239. sceneObjects.Clear();
  240. }
  241. }
  242. /// <summary>
  243. /// Contains information about scene object state, excluding information about its components and hierarchy.
  244. /// </summary>
  245. internal struct SceneObjectState
  246. {
  247. internal string name;
  248. internal Vector3 position;
  249. internal Quaternion rotation;
  250. internal Vector3 scale;
  251. internal bool active;
  252. /// <summary>
  253. /// Initializes the state from a scene object.
  254. /// </summary>
  255. /// <param name="so">Scene object to initialize the state from.</param>
  256. /// <returns>New state object.</returns>
  257. internal static SceneObjectState Create(SceneObject so)
  258. {
  259. SceneObjectState state = new SceneObjectState();
  260. state.name = so.Name;
  261. state.position = so.LocalPosition;
  262. state.rotation = so.LocalRotation;
  263. state.scale = so.LocalScale;
  264. state.active = so.Active;
  265. return state;
  266. }
  267. }
  268. /// <summary>
  269. /// Contains the difference between two <see cref="SceneObjectState"/> objects and allows the changes to be applied to
  270. /// a <see cref="SceneObject"/>. The value of the different fields is stored as its own state, while the flags field
  271. /// specified which of the properties is actually different.
  272. /// </summary>
  273. internal struct SceneObjectDiff
  274. {
  275. internal SceneObjectState state;
  276. internal SceneObjectDiffFlags flags;
  277. /// <summary>
  278. /// Creates a diff object storing the difference between two <see cref="SceneObjectState"/> objects.
  279. /// </summary>
  280. /// <param name="oldState">State of the scene object to compare from.</param>
  281. /// <param name="newState">State of the scene object to compare to.</param>
  282. /// <returns>Difference between the two scene object states.</returns>
  283. internal static SceneObjectDiff Create(SceneObjectState oldState, SceneObjectState newState)
  284. {
  285. SceneObjectDiff diff = new SceneObjectDiff();
  286. diff.state = new SceneObjectState();
  287. if (oldState.name != newState.name)
  288. {
  289. diff.state.name = newState.name;
  290. diff.flags |= SceneObjectDiffFlags.Name;
  291. }
  292. if (oldState.position != newState.position)
  293. {
  294. diff.state.position = newState.position;
  295. diff.flags |= SceneObjectDiffFlags.Position;
  296. }
  297. if (oldState.rotation != newState.rotation)
  298. {
  299. diff.state.rotation = newState.rotation;
  300. diff.flags |= SceneObjectDiffFlags.Rotation;
  301. }
  302. if (oldState.scale != newState.scale)
  303. {
  304. diff.state.scale = newState.scale;
  305. diff.flags |= SceneObjectDiffFlags.Scale;
  306. }
  307. if (oldState.active != newState.active)
  308. {
  309. diff.state.active = newState.active;
  310. diff.flags |= SceneObjectDiffFlags.Active;
  311. }
  312. return diff;
  313. }
  314. /// <summary>
  315. /// Applies the diff to an actual scene object.
  316. /// </summary>
  317. /// <param name="sceneObject">Scene object to apply the diff to.</param>
  318. internal void Apply(SceneObject sceneObject)
  319. {
  320. if (flags.HasFlag(SceneObjectDiffFlags.Name))
  321. sceneObject.Name = state.name;
  322. if (flags.HasFlag(SceneObjectDiffFlags.Position))
  323. sceneObject.LocalPosition = state.position;
  324. if (flags.HasFlag(SceneObjectDiffFlags.Rotation))
  325. sceneObject.LocalRotation = state.rotation;
  326. if (flags.HasFlag(SceneObjectDiffFlags.Scale))
  327. sceneObject.LocalScale = state.scale;
  328. if (flags.HasFlag(SceneObjectDiffFlags.Active))
  329. sceneObject.Active = state.active;
  330. }
  331. }
  332. /// <summary>
  333. /// Contains information about two separate states of a scene object header.
  334. /// </summary>
  335. internal struct SceneObjectHeaderUndo
  336. {
  337. internal SceneObject obj;
  338. internal SceneObjectDiff newToOld;
  339. internal SceneObjectDiff oldToNew;
  340. internal SceneObjectHeaderUndo(SceneObject obj, SceneObjectDiff newToOld, SceneObjectDiff oldToNew)
  341. {
  342. this.obj = obj;
  343. this.newToOld = newToOld;
  344. this.oldToNew = oldToNew;
  345. }
  346. }
  347. /// <summary>
  348. /// Stores the field changes in a <see cref="SceneObject"/> as a difference between two states. Allows those changes to
  349. /// be reverted and re-applied. Does not record changes to scene object components or hierarchy, but just the fields
  350. /// considered its header (such as name, local transform and active state).
  351. /// </summary>
  352. [SerializeObject]
  353. internal class RecordSceneObjectHeaderUndo : UndoableCommand
  354. {
  355. private string fieldPath;
  356. private List<SceneObjectHeaderUndo> headers;
  357. private RecordSceneObjectHeaderUndo() { }
  358. /// <summary>
  359. /// Creates the new scene object header undo command.
  360. /// </summary>
  361. /// <param name="headers">Information about recorded states of scene object headers.</param>
  362. /// <param name="fieldPath">
  363. /// Optional path that controls which is the field being modified and should receive input focus when the command
  364. /// is executed. Note that the diffs applied have no restriction on how many fields they can modify at once, but
  365. /// only one field will receive focus.</param>
  366. public RecordSceneObjectHeaderUndo(List<SceneObjectHeaderUndo> headers, string fieldPath)
  367. {
  368. this.headers = headers;
  369. this.fieldPath = fieldPath;
  370. }
  371. /// <inheritdoc/>
  372. protected override void Commit()
  373. {
  374. if (headers == null)
  375. return;
  376. foreach (var header in headers)
  377. {
  378. if (header.obj == null)
  379. continue;
  380. if (header.obj.IsDestroyed)
  381. {
  382. Debug.LogWarning("Attempting to commit state on a destroyed game-object.");
  383. continue;
  384. }
  385. header.oldToNew.Apply(header.obj);
  386. }
  387. FocusOnField();
  388. RefreshInspector();
  389. }
  390. /// <inheritdoc/>
  391. protected override void Revert()
  392. {
  393. if (headers == null)
  394. return;
  395. foreach (var header in headers)
  396. {
  397. if (header.obj == null)
  398. continue;
  399. if (header.obj.IsDestroyed)
  400. {
  401. Debug.LogWarning("Attempting to revert state on a destroyed game-object.");
  402. continue;
  403. }
  404. header.newToOld.Apply(header.obj);
  405. }
  406. FocusOnField();
  407. RefreshInspector();
  408. }
  409. /// <summary>
  410. /// Selects the component's scene object and focuses on the specific field in the inspector, if the inspector
  411. /// window is open.
  412. /// </summary>
  413. private void FocusOnField()
  414. {
  415. if (headers != null && headers.Count > 0)
  416. {
  417. if (headers.Count == 1)
  418. {
  419. if (Selection.SceneObject != headers[0].obj)
  420. Selection.SceneObject = headers[0].obj;
  421. }
  422. else
  423. {
  424. List<SceneObject> objs = new List<SceneObject>();
  425. foreach (var header in headers)
  426. {
  427. if(header.obj != null)
  428. objs.Add(header.obj);
  429. }
  430. Selection.SceneObjects = objs.ToArray();
  431. }
  432. if (!string.IsNullOrEmpty(fieldPath))
  433. {
  434. InspectorWindow inspectorWindow = EditorWindow.GetWindow<InspectorWindow>();
  435. inspectorWindow?.FocusOnField(headers[0].obj.UUID, fieldPath);
  436. }
  437. }
  438. }
  439. /// <summary>
  440. /// Updates the values of the fields displayed in the inspector window.
  441. /// </summary>
  442. private void RefreshInspector()
  443. {
  444. InspectorWindow inspectorWindow = EditorWindow.GetWindow<InspectorWindow>();
  445. inspectorWindow?.RefreshSceneObjectFields(true);
  446. }
  447. }
  448. /// <summary>
  449. /// Stores the field changes in a <see cref="SceneObject"/> as a difference between two states. Allows those changes to
  450. /// be reverted and re-applied. Unlike <see cref="bs.Editor.RecordSceneObjectHeaderUndo"/> this command records the
  451. /// full scene object state, including changes to its components and optionally any child objects.
  452. /// </summary>
  453. [SerializeObject]
  454. internal class RecordSceneObjectUndo : UndoableCommand
  455. {
  456. private SceneObject obj;
  457. private SerializedSceneObject oldState;
  458. private SerializedSceneObject newState;
  459. private RecordSceneObjectUndo() { }
  460. /// <summary>
  461. /// Creates the new scene object undo command.
  462. /// </summary>
  463. /// <param name="obj">Scene object on which to apply the performed changes.</param>
  464. /// <param name="oldState">Recorded original state of the scene object(s).</param>
  465. /// <param name="newState">Recorded new state of the scene object(s).</param>
  466. /// <param name="description">
  467. /// Optional description of the changes made to the scene object between the two states.
  468. /// </param>
  469. public RecordSceneObjectUndo(SceneObject obj, SerializedSceneObject oldState, SerializedSceneObject newState,
  470. string description)
  471. {
  472. this.obj = obj;
  473. this.oldState = oldState;
  474. this.newState = newState;
  475. }
  476. /// <inheritdoc/>
  477. protected override void Commit()
  478. {
  479. newState?.Restore();
  480. FocusOnObject();
  481. RefreshInspector();
  482. }
  483. /// <inheritdoc/>
  484. protected override void Revert()
  485. {
  486. oldState?.Restore();
  487. FocusOnObject();
  488. RefreshInspector();
  489. }
  490. /// <summary>
  491. /// Selects the scene object if not already selected.
  492. /// </summary>
  493. private void FocusOnObject()
  494. {
  495. if (obj != null)
  496. {
  497. if (Selection.SceneObject != obj)
  498. Selection.SceneObject = obj;
  499. }
  500. }
  501. /// <summary>
  502. /// Updates the values of the fields displayed in the inspector window.
  503. /// </summary>
  504. private void RefreshInspector()
  505. {
  506. InspectorWindow inspectorWindow = EditorWindow.GetWindow<InspectorWindow>();
  507. inspectorWindow?.RefreshSceneObjectFields(true);
  508. }
  509. }
  510. /// <summary>
  511. /// Stores the field changes in a <see cref="Component"/> as a difference between two states. Allows those changes to
  512. /// be reverted and re-applied.
  513. /// </summary>
  514. [SerializeObject]
  515. internal class RecordComponentUndo : UndoableCommand
  516. {
  517. private Component obj;
  518. private string fieldPath;
  519. private SerializedDiff newToOld;
  520. private SerializedDiff oldToNew;
  521. private RecordComponentUndo() { }
  522. /// <summary>
  523. /// Creates the new component undo command.
  524. /// </summary>
  525. /// <param name="obj">Component on which to apply the performed changes.</param>
  526. /// <param name="fieldPath">
  527. /// Optional path that controls which is the field being modified and should receive input focus when the command
  528. /// is executed. Note that the diffs applied have no restriction on how many fields they can modify at once, but
  529. /// only one field will receive focus.</param>
  530. /// <param name="oldToNew">
  531. /// Difference that can be applied to the old object in order to get the new object state.
  532. /// </param>
  533. /// <param name="newToOld">
  534. /// Difference that can be applied to the new object in order to get the old object state.
  535. /// </param>
  536. public RecordComponentUndo(Component obj, string fieldPath, SerializedDiff oldToNew, SerializedDiff newToOld)
  537. {
  538. this.obj = obj;
  539. this.fieldPath = fieldPath;
  540. this.oldToNew = oldToNew;
  541. this.newToOld = newToOld;
  542. }
  543. /// <inheritdoc/>
  544. protected override void Commit()
  545. {
  546. if (oldToNew == null || obj == null)
  547. return;
  548. if (obj.IsDestroyed)
  549. {
  550. Debug.LogWarning("Attempting to commit state on a destroyed game-object.");
  551. return;
  552. }
  553. oldToNew.Apply(obj);
  554. FocusOnField();
  555. }
  556. /// <inheritdoc/>
  557. protected override void Revert()
  558. {
  559. if (newToOld == null || obj == null)
  560. return;
  561. if (obj.IsDestroyed)
  562. {
  563. Debug.LogWarning("Attempting to revert state on a destroyed game-object.");
  564. return;
  565. }
  566. newToOld.Apply(obj);
  567. FocusOnField();
  568. }
  569. /// <summary>
  570. /// Selects the component's scene object and focuses on the specific field in the inspector, if the inspector
  571. /// window is open.
  572. /// </summary>
  573. private void FocusOnField()
  574. {
  575. SceneObject so = obj.SceneObject;
  576. if (so != null)
  577. {
  578. if (Selection.SceneObject != so)
  579. Selection.SceneObject = so;
  580. if (!string.IsNullOrEmpty(fieldPath))
  581. {
  582. InspectorWindow inspectorWindow = EditorWindow.GetWindow<InspectorWindow>();
  583. inspectorWindow?.FocusOnField(obj.UUID, fieldPath);
  584. }
  585. }
  586. }
  587. }
  588. /** @} */
  589. }