内容へ移動
ユーザ用ツール
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
現在位置:
Home
»
wiki
»
Unity
»
Tips
»
098 セーフティーなAddComponent /GetComponent
トレース:
wiki:unity:tips:098
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== 098 セーフティーなAddComponent /GetComponent ====== ===== 概要 ===== よく使う機能は安全性の為にもラップしておくと良いと思います。 <code csharp> //------------------------------------------------------------- //! コンポーネントの追加(存在チェック版). //------------------------------------------------------------- static public T AddSafeComponent<T>(GameObject _go) where T : Component { T component = _go.GetComponent<T>(); if (component == null) { // 無ければ足す。 return _go.AddComponent<T>(); } // あればそのまま返す。 return component; } //------------------------------------------------------------- //! コンポーネントの取得(存在チェック版). //------------------------------------------------------------- public static T GetSafeComponent<T>(GameObject _go) where T : Component { T component = _go.GetComponent<T>(); if(component == null) { Debug.LogError("Expected to find component of type " + typeof(T) + " but found none.", _go); } return component; } </code>
Permalink
wiki/unity/tips/098.txt
· 最終更新: 2015/02/14 12:28 by
step
ページ用ツール
ログイン
文書の表示
文書の先頭へ
印刷
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
oeffentlich