内容へ移動
ユーザ用ツール
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
現在位置:
Home
»
wiki
»
Unity
»
Tips
»
121 GameObjectを検索する拡張機能(5.5.0f3)
トレース:
wiki:unity:tips:121
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== 121 GameObjectを検索する拡張機能(5.5.0f3) ====== Unityの標準のGameObject.Find を使いやすい形にラップしたFindDeepクラスを提供します。 <code csharp GameObjectExtensions.cs> using System.Collections.Generic; using UnityEngine; using System.Linq; public static class GameObjectExtensions { /// <summary> /// 指定した名前のGameObjectが見つかるまで探します。 /// </summary> public static GameObject FindDeep( this GameObject self, string name, bool includeInactive = true, bool parent = false) { Transform[] children = null; if (parent) { children = self.GetComponentsInParent<Transform>(includeInactive); } else { children = self.GetComponentsInChildren<Transform>(includeInactive); } foreach (var transform in children) { if (transform.name == name) { return transform.gameObject; } } return null; } /// <summary> /// 指定した名前のGameObjectが見つかるまでPath指定で探します。 /// </summary> public static GameObject FindDeepPath( this GameObject self, string name, bool includeInactive = true, bool parent = false) { GameObject target = self; string[] nameList = name.Split('/'); foreach (var findName in nameList) { GameObject result = target.FindDeep(findName, includeInactive, parent); if(result != null) { target = result; } else { return null; } } return target; } } </code> <code csharp 使い方> GameObject result; void Start() { // 検索。 result = this.gameObject.FindDeep("HogeHoge"); // スラッシュ区切りで、検索。 result = this.gameObject.FindDeepPath("Header/Button_01"); } </code>
Permalink
wiki/unity/tips/121.txt
· 最終更新: 2017/03/18 09:36 by
step
ページ用ツール
ログイン
文書の表示
文書の先頭へ
印刷
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
oeffentlich