Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UnityC# for Beginner

mattak
June 21, 2016

UnityC# for Beginner

Presentated in Unity3D study

mattak

June 21, 2016
Tweet

More Decks by mattak

Other Decks in Programming

Transcript

  1.  (FOFSJDT  MBNCEBEFMFHBUF  FWFOU  FOVN  FYUFOTJPO

     OVMMBCMF  WBS  ಗ໊ܕ  OBNFTQBDF 1SPQFSUZ QBSUJBMUZQF ZJFME PVU -*/2 $จ๏
  2. ͳͥ૯শܕ͕͋Δͷ͔ʁ ૯শܕ͕ͳ͍ੈքΛߟ͑Δ List list = new List(); List<object> list =

    new List<object>(); ͭ·Γ͢΂ͯͷཁૉ͕PCKFDUܕͰอଘ͞ΕΔ
  3. ͳͥ૯শܕ͕͋Δͷ͔ʁ int id = (int)list[0];
 string name = (string)list[0]; PCKFDUܕΛDBTUͯ͠औΓग़͢ඞཁ͕͋Δ

    int id = intList[0]; ܕ͕ܾ·͍ͬͯΕ͹ɺΩϟετෆཁͰ҆શʹॻ͚Δ IntList intList = new IntList();
  4. IntList, StringList, HogeList, … Ͱ΋ܕ͝ͱʹΫϥεΛఆٛͨ͘͠ͳ͍ ͳͥ૯শܕ͕͋Δͷ͔ʁ List<T> => List<int>, List<string>,

    … ͦ͜Ͱ۩ମతͳܕΛ໌ࣔͤͣϩδοΫ͕͔͚Δͱ ൚༻ੑ͕͔͍ͨɹ ͜ΕΛ࣮ݱ͢ΔͨΊɺந৅తͳܕهड़͕Ͱ͖Δ (FOFSJDT1SPHSBNNJOH
  5. ͍ΖΜͳؔ਺࣮૷͕ग़དྷΔ public class Person
 {
 void SayHello(string name)
 {
 Debug.Log("Hello,

    " + name);
 } void Start()
 { Hello hello = SayHello; }
 } )FMMPܕͷؔ਺
  6. Hello hello = (string name) =>
 {
 Debug.Log("Hello, " +

    name);
 };
 Hello hello = delegate(string name)
 {
 Debug.Log("Hello, " + name);
 };
 ແ໊EFMFHBUF ແ໊EFMFHBUF লུ
  7. ແ໊EFMFHBUF ߋʹলུ Hello hello = (name) =>
 {
 Debug.Log("Hello, "

    + name);
 }; ແ໊EFMFHBUF ߋʹߋʹলུ Hello hello = name => Debug.Log("Hello, " + name);
  8. public enum Animal : int
 {
 Monkey = 0,
 Lion,


    Cat,
 Dog
 } FOVN஋ʹରͯ͠਺஋Λඥ෇͚ΒΕΔ จࣈྻΛͻ΋෇͚ͨΓɺ ෳ਺ͷ৘ใΛͻ΋෇͚ͨΓ͸Ͱ͖ͳ͍
  9. public static class StringExtension
 {
 public static string MyUpperCase(this string

    str)
 {
 return str.ToUpper();
 }
 }
 TUBUJDDMBTTΛએݴ Ϋϥε໊͸ԿͰ΋  TUBUJDNFUIPEΛએݴ ୈҰҾ਺ʹ֦ு͍ͨ͠ܕͱUIJTΛ͚ͭΔ
  10. int? hoge = null;
 bool? fuga = null; QSJNJUJWFܕʹ͔͔ͭ͑͠ͳ͍ ܕͷલʹ

    Λ͚ͭΔ͜ͱͰOVMM͕୅ೖՄೳʹͳΔ FMWJTPQFSBUPS΋ػೳͱͯ͠ೖͬͯΔ int result = hoge ?? 1; OVMMͩͬͨΒӈͷϦςϥϧ  Λબ୒
  11. ܕΛએݴͤͣɺϑΟʔϧυ஋ʹ҆શʹΞΫηεͰ͖Δ var rem = new { Name = "Rem", Age

    = 17 };
 var ram = new { Name = "Ram", Age = 17 }; Assert.AreEqual(rem.GetType(), ram.GetType()); 'JFME໊͕ҰॹͳΒܕ΋ಉ͡ Debug.Log("Hello, " + rem.Name);
  12. public class Person
 {
 private string _name = string.Empty;
 


    public string Name
 {
 get { return _name; }
 set { _name = value; }
 }
 }
 ܕએݴͷޙʹAHFUAATFUAͱ͍͏༧໿ޠΛ࢖ͬͯ HFUUFSTFUUFSͷΞΫηε੍ޚ͕Ͱ͖Δ
  13. public class Person
 {
 private string _name = string.Empty;
 


    public string Name
 {
 get { return _name; }
 private set { _name = value; }
 }
 }
 TFUUFSΛQSJWBUFʹͯ͠ॻ͖ࠐΊͳͨ͘͠Γɺ HFUTFU͢Δͱ͖ʹԿ͔ॲཧΛهड़ͨ͠Γग़དྷΔ
  14. public class Person
 {
 public string Name { get; private

    set; }
 } ୯ʹHFUTFU͢Δ͚ͩͳΒলུه๏͕ར༻Ͱ͖Δ HFUUFSTFUUFS͕ఆٛͰ͖Δ͜ͱͰ,70͕ग़དྷΔͷ͕ 1SPQFSUZͷಛ௃
  15. // Partial Class
 public partial class SamplePartial : MonoBehaviour
 {


    void Start()
 {
 }
 }
 
 public partial class SamplePartial : MonoBehaviour
 {
 void Update()
 {
 }
 }
 AQBSUJBMAΛ͚ͭΔ͜ͱͰ࣮૷ϑΝΠϧ෼཭͕Ͱ͖Δ
  16. foreach (int i in ListupNumbers())
 {
 Debug.Log("number: " + i);


    } IEnumerable<int> ListupNumbers()
 {
 yield return 0;
 yield return 1;
 yield return 2;
 } -JTUͱ͔"SSBSZΛOFX͠ͳͯ͘΋ ͜Μͳײ͡ͰZJFMESFUVSOͯ͋͛͠Δͱ ̍ͭͮͭཁૉΛऔಘ͢ΔΑ͏ͳ࢓૊Έ͕؆୯ʹ͔͚Δ
  17. ͲͷΑ͏ʹ࣮ߦ͞ΕΔʁ using (IEnumerator<int> it = ListupNumbers().GetEnumerator())
 {
 while (it.MoveNext())
 {


    Debug.Log("number: " + it.Current);
 }
 } .PWF/FYU Ͱ͖Δ͔͗ΓJUFSBUJPO͕·ΘΔ
  18. JUFSBUJPOΛதஅ͢Δʹ͸ʁ IEnumerable<int> ListupNumbers()
 {
 yield return 0;
 yield return 1;


    yield break;
 yield return 2; // ignored
 } ZJFMECSFBL͢Ε͹͍͍
  19. 6OJUZͷ$PSPVUJOFͱͷରԠ public class Sample : MonoBehaviour
 {
 void Start()
 {


    StartCoroutine(ListupNumbers());
 StartCoroutine(ListupNumbers());
 }
 
 void Update()
 {
 Debug.Log("Updated");
 }
 
 IEnumerator<int> ListupNumbers()
 {
 for (int i = 0; i < 5; i++)
 {
 Debug.Log(i.ToString());
 yield return i;
 }
 }
 }
  20. void Increment(out int v)
 {
 v = v + 1;


    } void Set(out int v)
 {
 v = 1;
 } int i = 0;
 Set(out i); PVUΛ͔ͭ͏ͱࢀরΛ౉ͯ͠ม਺΁ͷॻ͖ࠐΈ͕Ͱ͖Δ ͨͩ͠PVU͕͍ͭͨม਺ͷ஋͸औಘͰ͖ͳ͍ ࢖͍ํ ӈลͰWΛར༻ͯ͠ΔͷͰμϝ