Created
July 11, 2015 03:53
-
-
Save valbeat/128b0fb184b44c523116 to your computer and use it in GitHub Desktop.
Generics.cs
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
// ジェネリックにしたいメソッド | |
Object FindObjectOf(Type type) { | |
return FindObjectOfType(type); | |
} | |
// 型パワメータの名前をTとするのは慣習 | |
T FindObjectOf<T>() where T: Component { | |
// 型パラメータに制限を課すことができる | |
return (T)FindObjectOfType(typeof(T)); | |
} | |
// ジェネリックメソッドの呼び出し方 | |
var renderer = FindObjectOf<Renderer>(); | |
// intはコンポーネントを継承していないのでエラー | |
int i = FindObjectOf<int>(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment