ソースコード
using UnityEngine;
<summary>
</summary>
public static class GameObjectExtensions
{
<summary>
</summary>
public static void SafeSetParent(this GameObject self, GameObject parent)
{
var t = self.transform;
t.parent = parent.transform;
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.identity;
t.localScale = Vector3.one;
self.layer = parent.layer;
}
<summary>
</summary>
public static void SafeSetParent(this GameObject self, Component parent)
{
SafeSetParent(self, parent.gameObject);
}
}
<summary>
</summary>
public static class ComponentExtensions
{
<summary>
</summary>
public static void SafeSetParent(this Component self, GameObject parent)
{
var t = self.transform;
t.parent = parent.transform;
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.identity;
t.localScale = Vector3.one;
self.gameObject.layer = parent.layer;
}
<summary>
</summary>
public static void SafeSetParent(this Component self, Component parent)
{
SafeSetParent(self, parent.gameObject);
}
}
使い方
gameObject.SafeSetParent( parent );
関連記事