Unityで神になる本 第4章 信号機


このエントリーをはてなブックマークに追加

Unityで神になる本を読んでる

scriptsがJavaScriptなのでC#で書いてみる

信号機みたいなものを作る

  • C#で書き換えたら下記になった
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Signal : MonoBehaviour
{
public GameObject RedCube;
public GameObject BlueCube;
public Material MRed;
public Material MRedDark;
public Material MBlue;
public Material MBlueDark;
int changeCount = 0;
// Start is called before the first frame update
void Start()
{
RedCube.GetComponent<Renderer>().material = MRed;
BlueCube.GetComponent<Renderer>().material = MBlueDark;
InvokeRepeating("ChangeSignal", 3f, 3f);
}
// Update is called once per frame
void Update()
{
}
void ChangeSignal () {
changeCount++;
if(changeCount % 2 == 0) {
RedCube.GetComponent<Renderer>().material = MRedDark;
BlueCube.GetComponent<Renderer>().material = MBlue;
} else {
RedCube.GetComponent<Renderer>().material = MRed;
BlueCube.GetComponent<Renderer>().material = MBlueDark;
}
}



  • とりあえず泥臭い書き方はできた
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Signal : MonoBehaviour
{
public GameObject RedCube;
public GameObject BlueCube;
public Material MRed;
public Material MRedDark;
public Material MBlue;
public Material MBlueDark;
int changeCount = 0;
//public float interval = 0.2f;
// Start is called before the first frame update
void Start()
{
//RedCube.GetComponent<Renderer>().material = MRed;
//BlueCube.GetComponent<Renderer>().material = MBlueDark;
//InvokeRepeating("ChangeSignal", 3f, 3f);
StartCoroutine("BlinkSignal");
; }
// Update is called once per frame
void Update()
{
}
void ChangeSignal () {
changeCount++;
if(changeCount % 2 == 0) {
RedCube.GetComponent<Renderer>().material = MRedDark;
BlueCube.GetComponent<Renderer>().material = MBlue;
} else {
RedCube.GetComponent<Renderer>().material = MRed;
BlueCube.GetComponent<Renderer>().material = MBlueDark;
}
}
IEnumerator BlinkSignal() {
WaitForSeconds blinkSignal = new WaitForSeconds(0.3f);
var redRenderComponent = RedCube.GetComponent<Renderer>();
var blueRenderComponent = BlueCube.GetComponent<Renderer>();
while (true) {
redRenderComponent.material = MRed;
blueRenderComponent.material = MBlueDark;
yield return new WaitForSeconds(2.0f);
redRenderComponent.material = MRedDark;
yield return blinkSignal;
redRenderComponent.material = MRed;
yield return blinkSignal;
redRenderComponent.material = MRedDark;
yield return blinkSignal;
redRenderComponent.material = MRed;
yield return blinkSignal;
redRenderComponent.material = MRedDark;
yield return blinkSignal;
redRenderComponent.material = MRed;
yield return blinkSignal;
redRenderComponent.material = MRedDark;
yield return blinkSignal;
redRenderComponent.material = MRed;
yield return blinkSignal;
redRenderComponent.material = MRedDark;
redRenderComponent.material = MRedDark;
blueRenderComponent.material = MBlue;
yield return new WaitForSeconds(2.0f);
blueRenderComponent.material = MBlueDark;
yield return blinkSignal;
blueRenderComponent.material = MBlue;
yield return blinkSignal;
blueRenderComponent.material = MBlueDark;
yield return blinkSignal;
blueRenderComponent.material = MBlue;
yield return blinkSignal;
blueRenderComponent.material = MBlueDark;
yield return blinkSignal;
blueRenderComponent.material = MBlue;
yield return blinkSignal;
blueRenderComponent.material = MBlueDark;
yield return blinkSignal;
blueRenderComponent.material = MBlue;
yield return blinkSignal;
blueRenderComponent.material = MBlueDark;
}
}
}

下記みたいになったら成功

もうちょっとループ的な書き方本当はできるんだろうけどひとまず


このエントリーをはてなブックマークに追加