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

UnityでC#を勉強しはじめた私の主張

 UnityでC#を勉強しはじめた私の主張

2015/09/26(土) comm tech festivalでの
「UnityでC#を勉強しはじめた私の主張」

の資料です。
C#erな方、.NETマスターな方、ご意見、ご感想、つっこみをぜひぜひよろしくお願いします。

[Comm Tech Festival](https://comuplus.doorkeeper.jp/events/30403)

RyotaMurohoshi

September 26, 2015
Tweet

More Decks by RyotaMurohoshi

Other Decks in Technology

Transcript

  1. IDragHandlerΛʰී௨ʹʱ࣮૷͢Δྫ using UnityEngine; using UnityEngine.EventSystems; public class DragSample : MonoBehaviour,

    IDragHandler { public void OnDrag(PointerEventData pointerData { Debug.Log ("OnDrag"); } }
  2. IDragHandlerΛʰ໌ࣔతʹʱ࣮૷͢Δྫ using UnityEngine; using UnityEngine.EventSystems; public class DragSample : MonoBehaviour,

    IDragHandler { // public͕ͳ͘ͳͬͯϝιου໊ʹΠϯλʔϑΣʔε໊ʮIDragHandler.ʯ͕͍ͭͨ void IDragHandler.OnDrag(PointerEventData pointerData) { Debug.Log ("OnDrag"); } }
  3. ྫ1 List<int> numList = new List<int> {0, 1, 2, 3};

    // Լه͸ίϯύΠϧΤϥʔ //bool isReadOnly = numList.IsReadOnly; // ICollection<int> ܦ༝Ͱ͸ݺͼग़ͤΔ ICollection<int> numCollection = numList; bool isReadOnly = numCollection.IsReadOnly;
  4. ԼهͷOnDrag͸ɺUnityͷUIΠϕϯτγεςϜ͕ݺΜͰ͘ΕΔ public class DragSample : MonoBehaviour, IDragHandler { void IDragHandler.OnDrag(PointerEventData

    pointerData) { Debug.Log ("OnDrag"); } } ϙΠϯτ͸ɺOnDragϝιουΛΠϕϯτγεςϜҎ֎͔Β ݺ͹͍ͤͨͷ͔Ͳ͏͔ͩͱࢥ͏
  5. using UnityEngine; public class Sample : MonoBehaviour{ void Start ()

    { // Լه͸UnityEngine.Objectܕ Object unityEngineObject; // Լه͸System.Objectܕ object systemObject; } }
  6. using UnityEngine; using System; // <- new! public class Sample

    : MonoBehaviour{ void Start () { // ίϯύΠϧΤϥʔ // Լه͸System.Object͔UnityEngine.Object͔ᐆດ // Object ambiguousObject; // Լه͸System.Objectܕ object systemObject; } }
  7. using UnityEngine; using System; using UnityObject = UnityEngine.Object; // <-

    usingΤΠϦΞεσΟϨΫςΟϒ public class Sample : MonoBehaviour{ void Start () { // Լه͸UnityEngine.Object UnityObject unityEngineObject; // Լه͸System.Objectܕ object systemObject; // ࢒೦ͳ͕ΒɺObject͸ᐆດͳ··ͰίϯύΠϧΤϥʔ // Object ambiguousObject; } }
  8. UnityEngine.RandomʹΤΠϦΞεΛ ͋ΓͬͪΌ͋Γ͔ͳʁ using UnityEngine; using System; using UnityRandom = UnityEngine.Random;

    public class Sample : MonoBehaviour{ void Start () { float randomValue = UnityRandom.value; } }
  9. ࣍ͷίʔυ͸͢΂ͯίϯύΠϧΤϥʔ string str = FindObjectOfType<string> (); // MyClass͸ࣗ࡞ΫϥεɺUnityEngine.ObjectΛܧঝ͍ͯ͠ͳ͍ MyClass myClass

    = FindObjectOfType<MyClass> (); // ISample͸ࣗ࡞ΠϯλʔϑΣʔε ISample sample = FindObjectOfType<ISample> ();
  10. Լه͸OK UnityEngine.ObjectͰܕύϥϝʔλͷ੍໿͕͞Ε͍ͯΔͷͰ Tʹ͸ɺUnityEngine.Object͔ͦͷαϒΫϥε͔͜͠ͳ͍ public class Utility { // TܕͰήʔϜΦϒδΣΫτͷ໊લ͕nameͳ΋ͷΛऔಘ public

    static T[] FindObjectsOfType<T> (string name) where T : UnityEngine.Object { return Object.FindObjectsOfType<T> () .Where (it => it.name == name) .ToArray (); } }
  11. Ґஔ౳Λ͋ΒΘ͢Vector3΍ճసΛද͢QuaternionͰ ԋࢉࢠͷΦʔόʔϩʔυ͕ఆٛ͞Ε͍ͯΔ // Quaternion͸࢛ݩ਺ɻճసɾ࢟੎౳Ͱ࢖͏ Quaternion rotation = Quaternion.Euler (0, 0,

    90.0F); Vector3 vectorA = new Vector3 (1.0F, 1.0F, 0.0F); Vector3 vectorB = new Vector3 (1.0F, -1.0F, 0.0F); Vector3 addedVector = vectorA + vectorB; Vector3 scaledVector = 2.0F * vectorA; Vector3 rotatedVector = rotation * vectorB;
  12. ܕม׵ԋࢉࢠͱ͍͑͹ System.DecimalܕͰ͢ΑͶʁ // ଟ͘ͷ਺஋ܕ͔Β҉໧తܕม׵ decimal num1 = 1; decimal num2

    = 2.0F; decimal num3 = 3.0; // ଟ͘ͷ਺஋ܕ΁ͷ໌ࣔతܕม׵ int numA = (int)num1; float numB = (float)num2; double numC = (double)num3;
  13. Vector3ߏ଄ମ͔ΒVector2ߏ଄ମ΁ͷ҉໧తܕม׵ // z࠲ඪ͸ແࢹ͞ΕΔ Vector2 vector2d = new Vector3 (1.0F, 1.0F,

    0.0F); // z࠲ඪ͸0.0F͕ૠೖ͞ΕΔ Vector3 vector3d = new Vector2 (1.0F, 1.0F); ݁ߏศརʂͩͱࢥ͏ɻࣗ෼͸ɻ
  14. UnityEngine.Object͔Βbool΁ͷܕม׵ // Rigidbody͸ܧঝ֊૚ΛḷΔͱUnityEngine.ObjectʹͨͲΓண͘ Rigidbody rigidbody = GetComponent<Rigidbody>(); // Լه͸if (rigidbody

    != null) { ͱ࣮࣭ಉ͡Β͍͠ if (rigidbody) { rigidbody.AddForce(10.0F * Vector3.forward); } ͜Ε͸ศརͬͪΌศརͩͱࢥ͏͔΋͠Εͳ͍͚ΕͲɺ ͦΜͳʹ͏Ε͘͠ͳ͍ɻͲ͏ࢥ͍·͢ʁ
  15. UnityͰ΋େ׆༂ // ӈลͰRigidbodyܕͬͯॻ͍ͯ͋Δ͡ΌΜ! // Rigidbody rigidbody = GetComponent<Rigidbody> (); var

    rigidbody = GetComponent<Rigidbody> (); // ӈลͰGameManagerܕͬͯॻ͍ͯ͋Δ͡ΌΜ! // GameManager gameManager = FindObjectOfType<GameManager> (); var gameManager = FindObjectOfType<GameManager> ();
  16. దࡐదॴͰvarΛ࢖͏ // ໭Γ஋ܕ͕఻ΘΓζϥ͍ɾࣗ໌Ͱͳ͍ϝιουͰvar࢖͓͏ͱ͸ݴΘͳ͍ // var ambiguousTypeObject = GetAmbiguousTypeObject (): //

    ͜͏͍͏৔߹ɺ͸͖ͬΓܕΛ໌ࣔͨ͠΄͏͕͍͍ͱࢥ͏ SampleType ambiguousTypeObject = GetAmibigaousTypeObject (): // Ͱ΋ӈล͔Βܕ͕ࣗ໌ͳΒvar࢖ͬͨ΄͏͕͖ͬ͢Γ͢Δʂ // Dictionary<string, List<LongLongTypeName>> dictionary // = new Dictionary<string, List<LongLongTypeName>> (); var dictionary = new Dictionary<string, List<LongLongTypeName>> ();
  17. LINQͷCountϝιουɺFunc<TSource,2Boolean>ܕͷσϦήʔτͷҾ਺໊͸ ਖ਼͘͠͸ɺʰpredicateʱͳͷ͚ͩΕͲʂ // Լه͸ίϯύΠϧΤϥʔ // int count = numList.Count(predicate: num

    => num > 90); // Ҿ਺໊͕࢓༷ͱҧ͏ int count = numList.Count(selector: num => num > 90); Unity͕࢖͍ͬͯΔݹ͍mono͕ؒҧ͍ͬͯͨorz
  18. ࣍ͷίʔυ͸໰୊͕͋Δ FirstOrDefault͸ɺҾ਺ͷσϦήʔτ͕ද͢৚݅Λຬͨ͢࠷ॳͷཁૉΛऔಘ͢Δ ʰຬͨ͢ཁૉ͕ͳ͔ͬͨ৔߹ʱ͕͜͜ͰͷϙΠϯτ Vector3 playerPosition = GetPlayerPosition(); List<Vector3> enemyPositions =

    LoadEnemyPositions(); float attakRange = 5.0F; Vector3 targetEnemyPosition = enemyPositions .FirstOrDefault(p => Vector3.Distance(playerPosition, p) <= attakRange);
  19. ৚݅Λຬͨ͢ཁૉ͕ଘࡏ͠ͳ͔ͬͨ৔߹ targetEnemyPosi.on͸new0Vector3(0.0F,00.0F,00.0F)ͱ͍͏݁ՌʹͳΔɻ(nullͰ͸ͳ͍) Vector3 playerPosition = GetPlayerPosition(); List<Vector3> enemyPositions = LoadEnemyPositions();

    float attakRange = 5.0F; Vector3 targetEnemyPosition = enemyPositions .FirstOrDefault(p => Vector3.Distance(playerPosition, p) <= attakRange); ɹʰਖ਼͍͠஋͕ೖ͍ͬͯΔͱࢥͬͨΒɺ࣮͸نఆ஋͕ೖ͍ͬͯͨͨΊόάʂʱʹؾΛΑ͏
  20. μϝͳྫ Player player = GetPlayer(); // ͜Ε͸Ͱ͖ͳ͍͚ΕͲ // player.Items =

    new List<Item>(); // ී௨ʹ֎෦͔ΒList<T>ͷϝιουܦ༝Ͱத਎Λ͍͡ΕΔ player.Items.Clear();
  21. Ҋ1"๷ޚతίϐʔΛ͢Δ public class Player { /*ུ*/ private List<Item> items; public

    List<Item> Items { get { return new List<Item>(items); } } } ಺෦ͷϦετϝϯόͷෳ੡Λ֎෦ʹެ։͢Δ ͚ΕͲɺ͜ΕͩͱʮΫϥε಺ͷΞΠςϜͷϦετͷσʔλ͸͍͡ΕͳΑʯʮ๷ޚతίϐʔͨ͠Αʯ ͬͯ͜ͱ͕ͺͬͱݟͰ఻ΘΒͳ͍ɻPlayerΫϥεͷίʔυ'or'υΩϡϝϯτΛಡ·ͳ͍ͱ͍͚ͳ͍
  22. Ҋ2"ϓϩύςΟΛIEnumerable<T>ܕʹ͢Δ public class Player { /*ུ*/ private List<Item> items; public

    IEnumerable<Item> Items { get { return items; } } } ʮ͍͡Εͳ͍Αʯͱ͍͏͜ͱ͸఻ΘΔ͚ΕͲɺʮ֬ఆ͍ͯ͠ΔσʔλͩΑʯ͕఻ΘΒͳ͍ ʮ஗ԆධՁ͞ΕΔ஋ʁʯʮ಺෦Ͱ͸Ϧετɾ഑ྻͰอ࣋ͤͣDBʹ͍͍ͪͪ໰͍߹ΘͤͯΔʁʯ ͬͯצҧ͍͞ΕΔՄೳੑ͋Γɻ݁ہPlayerΫϥεͷίʔυ͔υΩϡϝϯτΛಡ·ͳ͍ͱ͍͚ͳ͍
  23. Ҋ3"ϓϩύςΟΛIReadOnlyList<T>ܕʹ͢Δ public class Player { /*ུ*/ private List<Item> items; public

    IReadOnlyList<Item> Items { get { return items; } } } ʮ֎෦͔Βͩͱ͍͡Εͳ͍Αʯ΋ʮ஗ԆධՁ͡Όͳͯ֬͘ఆ͍ͯ͠ΔσʔλͩΑʯ΋఻ΘΔ ʲ൵ใʳ͚ͩͲUnityͩͱ࢖͑ͳ͍ʲக໋తʳ .NET%4.5Ͱొ৔ͨ͠ΠϯλʔϑΣʔε͔ͩΒorz
  24. Ҋ4"ϓϩύςΟΛReadOnlyCollec.on<T>ܕʹ͢Δ public class Player { /*ུ*/ private List<Item> items; public

    ReadOnlyCollection<Item> Items { get { return new ReadOnlyCollection<Item>(items); } } } ΋͘͠͸ɺ(ҙຯ͸มΘͬͯ͘Δ͚ΕͲ) public class Player { /*ུ*/ public ReadOnlyCollection<Item> Items { get ; private set; } }