Created
January 25, 2023 14:13
-
-
Save takumifukasawa/8bdb517192a1070f30a9ab4869d793f9 to your computer and use it in GitHub Desktop.
unity: transform utilities
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
using UnityEngine; | |
namespace Utilities | |
{ | |
// ---------------------------------------------------------------------------------------- | |
// refs: | |
// https://stackoverflow.com/questions/33437244/find-children-of-children-of-a-gameobject | |
// ---------------------------------------------------------------------------------------- | |
public static class TransformUtilities | |
{ | |
public static Transform RecursiveFindChild(Transform parent, string childName) | |
{ | |
foreach (Transform child in parent) | |
{ | |
if (child.name == childName) | |
{ | |
return child; | |
} | |
else | |
{ | |
Transform found = RecursiveFindChild(child, childName); | |
if (found != null) | |
{ | |
return found; | |
} | |
} | |
} | |
return null; | |
} | |
public static void SetLossyScale(Transform target, Vector3 scale) | |
{ | |
target.localScale = new Vector3( | |
target.localScale.x / target.lossyScale.x * scale.x, | |
target.localScale.y / target.lossyScale.y * scale.y, | |
target.localScale.z / target.lossyScale.z * scale.z | |
); | |
} | |
public static void SetLocalOrigin(Transform target, Transform parent) | |
{ | |
target.parent = parent; | |
target.localPosition = Vector3.zero; | |
target.localRotation = Quaternion.identity; | |
target.localScale = Vector3.one; | |
} | |
public static void DestroyChildren(Transform target) | |
{ | |
foreach (Transform child in target) | |
{ | |
Object.Destroy(child.gameObject); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment