Snippets

Alex Darby ResetTransformToParent

You are viewing an old version of this snippet. View the current version.
Revised by Alex Darby da29c29
#if UNITY_EDITOR

using UnityEngine;
using UnityEditor;

//////////////////////////////////////////////////////////////////////////////	
//////////////////////////////////////////////////////////////////////////////	
public static class EditorHierarchyContextMenu
{
	//------------------------------------------------------------------------	
	// note this was written with help from: 
    // https://answers.unity.com/questions/22947/adding-to-the-context-menu-of-the-hierarchy-tab.html
	//------------------------------------------------------------------------	

	const int k_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 )]
	static void RightClick_ResetTransformToParent()
	{
		Transform cResetMeToParent = Selection.activeTransform;

		Undo.RecordObject( cResetMeToParent, System.Reflection.MethodBase.GetCurrentMethod().Name + "-" + cResetMeToParent.gameObject.name );

		GameObject	cTEMPParent		= new GameObject( "TEMPORARY_REPARENTER" );
		Transform	cTEMPTransform	= 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 )]
	static bool Validate_RightClick_ResetTransformToParent()
	{
		return(		( null != Selection.activeTransform ) 
				&&	( null != Selection.activeTransform.parent ) );
	}

	//------------------------------------------------------------------------
	public static void ReparentAllChildrenTo( this Transform cOriginalParent, Transform cNewParent )
	{
		int iInitialChildcount = cOriginalParent.childCount;

		for( int i = 0; i < iInitialChildcount; ++i )
		{
			// n.b. we take the 0th child iInitialChildcount times to empty the child list
			cOriginalParent.GetChild( 0 ).SetParent( cNewParent );
		}
	}
}
						
#endif// #if UNITY_EDITOR
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.