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
250
UnixTime is Simple
mattak
0
170
おっさんが停滞しないために
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
770
Unity energy usage
mattak
0
860
Unidux 0.3.1
mattak
1
370
Other Decks in Programming
See All in Programming
CSC509 Lecture 14
javiergs
PRO
0
140
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
Zoneless Testing
rainerhahnekamp
0
120
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
42 best practices for Symfony, a decade later
tucksaun
1
180
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
250
Haze - Real time background blurring
chrisbanes
1
510
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
160
Featured
See All Featured
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
A Tale of Four Properties
chriscoyier
157
23k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Gamification - CAS2011
davidbonilla
80
5.1k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Building an army of robots
kneath
302
44k
Faster Mobile Websites
deanohume
305
30k
Statistics for Hackers
jakevdp
796
220k
For a Future-Friendly Web
brad_frost
175
9.4k
Documentation Writing (for coders)
carmenintech
66
4.5k
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දࣔ