Created
October 3, 2013 12:16
-
-
Save Suzeep/6808893 to your computer and use it in GitHub Desktop.
アタッチしたオブジェクトのTransformを全取得するスクリプト。
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; | |
using System; | |
using System.Collections.Generic; | |
//---------------------------------------------------------------------- | |
// | |
// アタッチしたオブジェクト以下のTransformを全取得するスクリプト | |
// 主にキャラクターの関節取得用 | |
// | |
// ※ | |
// RootにしたいTransformを指定して、それ以下のを取得も可能. | |
// スクリプトやインスペクタでRoot名が設定されていればそのような挙動になります. | |
// スクリプトで設定する場合は, このコンポーネントのAwakeが呼ばれる前に行う必要があります. | |
// | |
//---------------------------------------------------------------------- | |
public class TransformList : MonoBehaviour | |
{ | |
//------------------------------------------------------------------ | |
// Awake | |
//------------------------------------------------------------------ | |
void Awake() | |
{ | |
m_Owner = this.gameObject; | |
Transform trans = m_Owner.transform; | |
if( m_RootName == "" ) | |
{ | |
m_Root = trans; | |
m_RootName = trans.name; | |
GetChild( trans ); | |
} | |
else // ルートの指定がある場合 | |
{ | |
// Rootを見つける | |
while( trans.GetChildCount() > 0 ) | |
{ | |
trans = trans.GetChild( 0 ); | |
if( trans.name == m_RootName ){ | |
m_Root = trans; | |
break; | |
} | |
} | |
if( m_Root ){ | |
GetChild( m_Root ); | |
}else{ | |
Debug.LogError( "[" + m_RootName + "] is not exist. please check transform name." ); | |
} | |
} | |
} | |
//------------------------------------------------------------------ | |
// 子Transformを探してを全部格納(自分含む) | |
//------------------------------------------------------------------ | |
private void GetChild( Transform check_root ) | |
{ | |
int cnt = check_root.GetChildCount(); | |
m_DictList.Add( check_root.name, check_root ); | |
#if UNITY_EDITOR | |
m_List.Add(check_root); | |
#endif | |
for( int i=0; i < cnt; ++i ){ | |
GetChild( check_root.GetChild(i) ); | |
} | |
} | |
//------------------------------------------------------------------ | |
// 指定した名前のTransformを取得 | |
//------------------------------------------------------------------ | |
public Transform GetJoint(string name) | |
{ | |
if( m_DictList.ContainsKey(name) ) | |
return m_DictList[ name ]; | |
else | |
return null; | |
} | |
//------------------------------------------------------------------ | |
// property | |
//------------------------------------------------------------------ | |
// ルート名 | |
public string RootName { | |
get { | |
return m_RootName; | |
} | |
set { | |
m_RootName = value; | |
} | |
} | |
//------------------------------------------------------------------ | |
// member | |
//------------------------------------------------------------------ | |
private GameObject m_Owner; // アタッチ対象オブジェクト | |
// Jointリスト | |
public Dictionary<string, Transform> m_DictList = new Dictionary<string, Transform>(); | |
#if UNITY_EDITOR | |
public List<Transform> m_List = new List<Transform>(); | |
#endif | |
[SerializeField] | |
private string m_RootName; // ルート名 | |
[SerializeField] | |
private Transform m_Root; // ルート | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment