#if UNITY_EDITORusingUnityEngine;usingUnityEditor;////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// publicstaticclassEditorHierarchyContextMenu{//------------------------------------------------------------------------ // note this was written with help from: // https://answers.unity.com/questions/22947/adding-to-the-context-menu-of-the-hierarchy-tab.html//------------------------------------------------------------------------ constintk_iBasePriority=-100;//------------------------------------------------------------------------// resets a transform to its parent origin without affecting its children// // this really helps tidy up scenes which might not have been put together// quite the way you wish they had been.// // I lost track of the number of times I've done this manually (1000s?)// so I wrote this editor menu estension to do it.//// the maths to do this manually isn't that hard, but it's WAY simpler to // just reparent stuff and let the Unity Editor work it out for you.//------------------------------------------------------------------------// this attribute makes it appear on the right click menu in the hierarchy// see the linked post on Unity answers for more information about the priority [MenuItem( "GameObject/Reset Transform To Parent", false, k_iBasePriority )]staticvoidRightClick_ResetTransformToParent(){TransformcResetMeToParent=Selection.activeTransform;Undo.RecordObject(cResetMeToParent,System.Reflection.MethodBase.GetCurrentMethod().Name+"-"+cResetMeToParent.gameObject.name);GameObjectcTEMPParent=newGameObject("TEMPORARY_REPARENTER");TransformcTEMPTransform=cTEMPParent.transform;cTEMPTransform.position=Vector3.zero;cTEMPTransform.rotation=Quaternion.identity;cTEMPTransform.localScale=Vector3.one;cResetMeToParent.ReparentAllChildrenTo(cTEMPTransform);cResetMeToParent.localPosition=Vector3.zero;cResetMeToParent.localRotation=Quaternion.identity;cResetMeToParent.localScale=Vector3.one;cTEMPTransform.ReparentAllChildrenTo(cResetMeToParent);Object.DestroyImmediate(cTEMPParent);}//------------------------------------------------------------------------ // validates the above function can be used & greys out the option if not [MenuItem( "GameObject/Reset Transform To Parent", true )]staticboolValidate_RightClick_ResetTransformToParent(){return((null!=Selection.activeTransform)&&(null!=Selection.activeTransform.parent));}//------------------------------------------------------------------------publicstaticvoidReparentAllChildrenTo(thisTransformcOriginalParent,TransformcNewParent){intiInitialChildcount=cOriginalParent.childCount;for(inti=0;i<iInitialChildcount;++i){// n.b. we take the 0th child iInitialChildcount times to empty the child listcOriginalParent.GetChild(0).SetParent(cNewParent);}}}#endif// #if UNITY_EDITOR
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.