Last active
April 16, 2022 13:23
-
-
Save sfider/114a062f317e81de8ffb132ba56c1687 to your computer and use it in GitHub Desktop.
Unity fix for positioning newly created objects in hierarchy below selected object, not at the end of the hierarchy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2022 Marcin Swiderski | |
* | |
* The MIT Licence (MIT) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
using UnityEditor; | |
using UnityEngine; | |
public static class CreateGameObjectHierarchyPositionFix | |
{ | |
private static GameObject _lastActiveGameObject; | |
[InitializeOnLoadMethod] | |
private static void InitializeOnLoad() | |
{ | |
Selection.selectionChanged += OnSelectionChanged; | |
ObjectChangeEvents.changesPublished += OnObjectChange; | |
} | |
private static void OnSelectionChanged() | |
{ | |
_lastActiveGameObject = Selection.activeGameObject; | |
} | |
private static void OnObjectChange(ref ObjectChangeEventStream stream) | |
{ | |
for (int eventIdx = 0; eventIdx < stream.length; ++eventIdx) { | |
if (stream.GetEventType(eventIdx) == ObjectChangeKind.CreateGameObjectHierarchy) { | |
stream.GetCreateGameObjectHierarchyEvent(eventIdx, out CreateGameObjectHierarchyEventArgs eventArgs); | |
GameObject gameObject = EditorUtility.InstanceIDToObject(eventArgs.instanceId) as GameObject; | |
if (gameObject) { | |
if (_lastActiveGameObject != null && gameObject.transform.parent == _lastActiveGameObject.transform.parent) { | |
gameObject.transform.SetSiblingIndex(_lastActiveGameObject.transform.GetSiblingIndex() + 1); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just add this to editor scripts in your project and you're set :)