Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Unity C# for beginner - unity part -
Search
mattak
June 28, 2016
Programming
0
280
Unity C# for beginner - unity part -
mattak
June 28, 2016
Tweet
Share
More Decks by mattak
See All by mattak
最近やった作業環境改善施策10個
mattak
0
240
UnixTime is Simple
mattak
0
160
おっさんが停滞しないために
mattak
32
18k
tech invest
mattak
1
300
Nodux - node base redux framework
mattak
0
1.4k
what_is_technical_investment
mattak
0
120
unitypackage distribution
mattak
0
760
Unity energy usage
mattak
0
840
Unidux 0.3.1
mattak
1
360
Other Decks in Programming
See All in Programming
リリース8年目のサービスの1800個のERBファイルをViewComponentに移行した方法とその結果
katty0324
5
3.6k
offers_20241022_imakiire.pdf
imakurusu
2
360
Golang と Erlang
taiyow
8
1.9k
Android 15 でアクションバー表示時にステータスバーが白くなってしまう問題
tonionagauzzi
0
140
[PyCon Korea 2024 Keynote] 커뮤니티와 파이썬, 그리고 우리
beomi
0
110
Vue3の一歩踏み込んだパフォーマンスチューニング2024
hal_spidernight
3
3.1k
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
480
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
0
150
役立つログに取り組もう
irof
26
8.6k
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
430
Why Spring Matters to Jakarta EE - and Vice Versa
ivargrimstad
0
970
約9000個の自動テストの 時間を50分->10分に短縮 Flakyテストを1%以下に抑えた話
hatsu38
23
11k
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
790
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
9
680
The Cost Of JavaScript in 2023
addyosmani
45
6.6k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Why Our Code Smells
bkeepers
PRO
334
57k
A Tale of Four Properties
chriscoyier
156
23k
Teambox: Starting and Learning
jrom
132
8.7k
It's Worth the Effort
3n
183
27k
Done Done
chrislema
181
16k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
41
2.1k
Code Review Best Practice
trishagee
64
17k
Building an army of robots
kneath
302
42k
Transcript
Unity C# ⼊⾨ - Unity編 - 6OJUZษڧձ !NBUUBL
今⽇のメニュー!
C# Unity (BNF0CKFDU .POP#FIBWJPVS 3FTPVSDFT *OTUBOUJBUF
%FTUSPZ $PNQPOFOU *OQVU %FCVH 1SPpMF
1. GameObject
%্ۭؒʹஔ͞ΕΔͯ͢ͷ (BNF0CKFDUΫϥεͱͯ͠දݱ͞ΕΔ (BNF0CKFDU͕࣮ߦ࣌ͷؔͷΤϯτϦʔϙΠϯτʹͳΔɻ (BNF0CKFDUʹεΫϦϓτΛ͚ͭΔ͜ͱͰ ࡞ͨؔ͠ͷ࣮ߦ͕ՄೳʹͳΔɻ
.POP#FIBWJPVSʹHBNF0CKFDU͕ੜ͍͑ͯΔ public class SampleBehaviour : MonoBehaviour { void Start() {
this.gameObject; // ࢀরՄೳ } }
ͯ͢ͷ(BNF0CKFDUԼهΛ࣋ͭ this.gameObject.transform; // زԿมΛߦ͏ object this.gameObject.name; // Inspector্Ͱදࣔ͞ΕΔ໊લ this.gameObject.tag; //
Inspector্Ͱઃఆ͞ΕΔλά໊ this.gameObject.activeSelf; // ͜ͷGameObject͕༗ޮԽͲ͏͔ʁ this.gameObject.isStatic; // GameObject͕੩తମԽͲ͏͔? this.gameObject.layer; // GameObject͕ॴଐ͢ΔϨΠϠʔ໊ ࠲ඪΛॴ༗͠ͳ͍(BNF0CKFDUଘࡏ͠ͳ͍
2. MonoBehaviour
.POP#FIBWJPVSͱʁ (BNF0CKFDUʹ"UUBDI͢Δ4DSJQUͷجఈΫϥε ಛ -JGF$ZDMFΛ࣋ͭ 4UBSU 6QEBUF "XBLF 0O&OBCMFʜ (BNF0CKFDUͷγϣʔτΧοτΛ࣋ͭ
$PSPVUJOFΛ։࢝Ͱ͖Δ https://docs.unity3d.com/ScriptReference/MonoBehaviour.html
-JGF$ZDMF Πϕϯτͷ࣮ߦॱং http://docs.unity3d.com/ja/current/Manual/ExecutionOrder.html ήʔϜͷॳظԽ "XBLF(BNF0CKFDU͕࡞ΒΕͨॳΊͷΈ 0O&OBCMF(BNF0CKFDU͕༗ޮʹͳΔͨͼʹݺΕΔ 4UBSU0O&OBCMF͞Εͨޙ
-JGF$ZDMF Πϕϯτͷ࣮ߦॱং http://docs.unity3d.com/ja/current/Manual/ExecutionOrder.html ཧఆ 0O$PMMJTJPO&OUFS $PMMJTJPODPMMJTJPO ͕͋ͨͬͨ࣌ 0O$PMMJTJPO&YJU $PMMJTJPODPMMJTJPO ʹ͋ͨͬͨޙ
0O5SJHHFS&OUFS $PMMJEFSDPMMJEFS ྖҬʹೖͬͨ࣌ 0O5SJHHFS&YJU $PMMJEFSDPMMJEFS ྖҬ͔Βग़ͨ࣌
-JGF$ZDMF Πϕϯτͷ࣮ߦॱং http://docs.unity3d.com/ja/current/Manual/ExecutionOrder.html ήʔϜϧʔϓ 6QEBUFຖϑϨʔϜݺΕΔ -BTU6QEBUF6QEBUFͯ͠$PSPVUJOFͷ࣮ߦऴྃޙ
-JGF$ZDMF Πϕϯτͷ࣮ߦॱং http://docs.unity3d.com/ja/current/Manual/ExecutionOrder.html ऴྃ࣌ 0O%JTBCMF(BNF0CKFDU͕"DUJWFͰͳ͘ͳͬͨλΠϛϯά 0O%FTUSPZ(BNF0CKFDU͕ফࣦ͢Δ࠷ޙͷλΠϛϯά
֤छϓϩύςΟ (BNF0CKFDUͷγϣʔτΧοτ࣋ͭ this.enabled; // εΫϦϓτ͕༗ޮԽͲ͏͔ this.isActiveAndEnabled; // εΫϦϓτ͕༗ޮͰGameObject͕༗ޮ͔ʁ this.gameObject;
// GameObjectͷࢀর this.tag; // λά this.transform; // زԿมܗΛߦ͏ object this.hideFlags; // this.name; // Inspector্Ͱදࣔ͞ΕΔ໊લ
$PSPVUJOFͷ։࢝Ͱ͖Δ public class SampleBehaviour : MonoBehaviour { void Start() {
this.StartCoroutine(Hoge()); } IEnumerator Hoge() { yield return null; } }
3. Resources
3FTPVSDFTͰ0CKFDUΛಡΈࠐΉ͜ͱ͕Ͱ͖Δ Sprite sprite = Resources.Load<Sprite>("image"); 6OJUZͰA3FTPVSDFTA͕ಛघϑΥϧμʹͳ͍ͬͯΔ 3FTPVSDFTҎԼʹ͋Δͷಡࠐ͕Մೳ ্l3FTPVSDFTJNBHFQOHzΛಡΈࠐΜͰ͍Δ
3FTPVSDFTͰ1SFGBCΛࢀর͢ΔࣄͰ͖Δ l3FTPVSDFT*UFNzΛࢀর GameObject _gameObject = Resources.Load<GameObject>("Item"); ಡΈࠐΈϑΥϧμɺ3FTPVSDFTͱ͍͏ϑΥϧμ໊Ͱ͋Εɺ Ͳͷ֊ʹ͍͍͋ͬͯ
4. Instantiate
(BNF0CKFDU*OTUBOUJBUFͰ 1SFGBCΛݩʹΦϒδΣΫτͷੜ͕Ͱ͖Δ GameObject _prefab = Resources.Load<GameObject>("CheckItem"); GameObject _object = Instantiate<GameObject>(_prefab);
ՃͰҾࢦఆ͕Ͱ͖Δ ୈೋҾ*OTUBOUJBUF͢Δ%࠲ඪ ୈࡾҾճసํ GameObject _object = Instantiate(_prefab, Vector3.zero, Quaternion.identity) as
GameObject;
Ճͨ͠(BNF0CKFDUͷ4FU1BSFOUͰࢦఆ _object.transform.SetParent(this.transform); _object.transform.SetParent(this.transform, false); ୈೋҾͰXPSME࠲ඪͷҐஔɾεέʔϧͰஔ͢Δ͔ʁ GBMTFࢦఆͨ͠ʹैଐɺ࣮ࡍͷҐஔɺεέʔϧ͕มΘΔ USVFXPSME࠲ඪ্ͷҐஔεέʔϧ͕มΘΒͳ͍Α͏ ࣗͷҐஔɺεέʔϧΛʹج͍ͮͯΛ࠶ܭࢉɻ
5. Destroy
4DFOFʹొͨ͠(BNF0CKFDUΛআ͢Δ͜ͱ͕Ͱ͖Δ // ҾʹGameObjectΛ͢͜ͱͰࣗΛআͰ͖Δ GameObject.Destroy(this.gameObject); // 1ඵޙʹআ͢Δ GameObject.Destroy(this.gameObject, 1.0f); ඵΛࢦఆͯ͠ɺͦͷޙʹআͰ͖Δ
6. Component
SampleBehaviour sample = this.gameObject.AddComponent<SampleBehaviour>(); (BNF0CKFDUʹଐ͢Δ$PNQPOFOUಈతʹՃͰ͖Δ SampleBehaviour sample = this.gameObject.GetComponent<SampleBehaviour>(); ·ͨऔಘͰ͖Δ
(FU$PNQPOFOU.POP#FIBWJPVSʹੜ͍͑ͯΔ (BNF0CKFDUʹ͍͍ͯΔ$PNQPOFOUͳΒͳΜͰऔಘՄೳ var text = this.GetComponent<Text>(); var trans = this.GetComponent<Transform>();
var collider = this.GetComponent<BoxCollider>();
ෳಉ͡$PNQPOFOU͕͋Δ߹ SampleBehaviour[] components = this.GetComponents<SampleBehaviour>(); SampleBehaviour component = this.GetComponentInChildren<SampleBehaviour>(); SampleBehaviour
component = this.GetComponentInParent<SampleBehaviour>(); ࢠཁૉ͔ΒऔಘͰ͖Δ ཁૉ͔ΒऔಘͰ͖Δ
ଞͷ$PNQPOFOUΛऔಘ͢Δํ๏ ࣗͷ(BNF0CKFDUʹ͚ͭͯࢀর ҉తಉҰγʔϯ QVCMJDͰઃఆͯ͠ࢀর ໌ࣔతಉҰγʔϯ
'JOEͰ୳ͯ͠ࢀর ҉తಉҰγʔϯ 4JOHMFUPOʹͯ͠ࢀর ҉తάϩʔόϧγʔϯ
'JOEͰ୳͢ GameObject obj1 = GameObject.Find("Sample"); GameObject obj2 = GameObject.FindGameObjectWithTag("Player"); GameObject
obj3 = GameObject.FindObjectOfType<GUITexture>(); جຊతʹUBHΛ͚ͭͯ୳͢ͷ͕ૣͯ͘ྑ͍ ͦͦ୳͞ͳ͍ͰࡁΉઃܭ͕Α͍
7. Input
public class SampleBehaviour : MonoBehaviour { void Update() { if
(Input.GetKey(KeyCode.Space)) { // εϖʔεΛԡͯ͠Δؒtrue } if (Input.GetKeyDown(KeyCode.Space)) { // εϖʔεΛԡԼͨ͠ॠؒͷΈtrue } if (Input.GetKeyUp(KeyCode.Space)) { // εϖʔεΛԡԼͯͨ͠͠ॠؒͷΈtrue } } } (FU,FZϘλϯೖྗΛऔಘ
(FU,FZϘλϯೖྗΛऔಘ Input.GetMouseButton(0); // ࠨΫϦοΫ Input.GetMouseButtonDown(1); // ӈΫϦοΫ Input.GetMouseButtonUp(2); // ਅΜதΫϦοΫ
// λονΠϕϯτͷॲཧ if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{ } TouchPhase.Began; // ։࢝ TouchPhase.Ended; // ऴྃ TouchPhase.Moved; // Ҡಈத TouchPhase.Stationary; // λονͨ͠··ఀࢭத TouchPhase.Canceled; // γεςϜଆͰΩϟϯηϧ͞Εͨ (FU5PVDIλονΠϕϯτͷऔಘ
Input.acceleration; // Ճ Input.location; // Ґஔใ Input.compass; // ίϯύε Input.deviceOrientation;
// σόΠεͷํ ͦͷଞऔಘͰ͖Δͷ
8. Debug
%FCVH-PH999Ͱϩά͕͚Δ Debug.Log("debugϩά"); Debug.LogWarning("warningϩά"); Debug.LogError("errorϩά");
Debug.Break(); Ұ࣌ఀࢭΛϓϩάϥϜଆ͔Β͢Δ͜ͱ͕ग़དྷΔ ͜Ε͕ԡ͞Εͨঢ়ଶ
Debug.isDebugBuild; // debugϏϧυͰ͋Εtrue %FCVHϏϧυ͔Ͳ͏͔ఆͰ͖Δ
$MFBSϩάΛআ͢Δ $PMMBQTFಉ͡ϝοηʔδͷϩάΛ·ͱΊΔ $MFBSPO1MBZ։࢝ϘλϯΛԡ͢ͱϩάΛΫϦΞ͢Δ &SSPSPO1BVTFΤϥʔ͕ى͖ͨΒϙʔζ͢Δ ͦͷଞ$POTPMFͷ͍ํ
9. Profile
(BNFϏϡʔͷ4UBUTΛԡ͢ͱ؆қతͳใ͕ݟΕΔ
8JOEPX1SPpMFSͰ$16/FUXPSL.FNPSZදࣔ