라디오 버튼은 <RadioButton>을 사용한다.


<RadioGroup>을 사용하여 라디오 버튼을 그룹화 할 수 있다.


라디오 그룹의 setOnCheckedChangeListener 이벤트를 사용하여 클릭한 라디오 버튼의 ID를 알 수 있다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
 
        <RadioButton
            android:id="@+id/radiobutton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="라디오버튼1" />
 
        <RadioButton
            android:id="@+id/radiobutton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="라디오버튼2" />
 
 
        <RadioButton
            android:id="@+id/radiobutton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="라디오버튼3" />
    </RadioGroup>
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) findViewById(checkedId);
                Toast.makeText(MainActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
cs


'Android' 카테고리의 다른 글

[Android] EditText 사용하기  (0) 2019.03.13
[Android] SeekBar 사용  (0) 2019.03.12
[Android] CheckBox 사용  (0) 2019.03.08
[Android] ToggleButton/Switch 사용  (0) 2019.03.07
[Android] Button 상태에 따라 이미지 변경하기  (0) 2019.03.06

충돌 감지가 필요한 Object에 Add Component를 눌러 Box Collider를 추가한다.


추가된 Box Collider의 Size를 늘려 충돌 영역을 넓혀준다.


실제 충돌을 감지할 Object에는 Rigidbody를 추가한다.


OnTriggerEnter 함수에 충돌하였을때 발생할 이벤트를 작성한다.




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
public class EnemyControl : MonoBehaviour
{
    public float Speed = 50.0f;
    private Transform myTransform = null;
    
    // Start is called before the first frame update
    void Start()
    {
        myTransform = GetComponent<Transform>();
    }
 
    // Update is called once per frame
    void Update()
    {
        Vector3 moveAmount = Speed * Vector3.back * Time.deltaTime;
        myTransform.Translate(moveAmount);
 
        if(myTransform.position.y <= -60.0f)
        {
            myTransform.position = new Vector3(Random.Range(-60.0f, 60.0f), 60.0f, 0.0f);
        }
    }
 
    //충돌 이벤트 함수
    void OnTriggerEnter(Collider other)
    {
        if(other.tag == "Bullet")
        {
            Debug.Log("Bullet Trigger Enter");
        }
    }
}
cs


'유니티' 카테고리의 다른 글

[유니티] 사운드 삽입  (0) 2019.03.13
[유니티] 폭발 이펙트 추가  (0) 2019.03.12
[유니티] Random 위치에 Object 이동  (0) 2019.03.07
[유니티] Object 생성  (0) 2019.03.05
[유니티]Object 삭제  (0) 2019.03.04

체크박스는 <CheckBox>를 사용한다.


On/Off 상태를 구할 땐 setOnCheckedChangeListener를 사용한다.


1
2
3
4
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox);
 
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(MainActivity.this"체크상태 : " + isChecked, Toast.LENGTH_SHORT).show();
            }
        });
    }
cs




ToggleButton과 Switch는 On/Off 상태를 표현할 수 있다.


On/Off 상태 변화 감지는 setOnCheckedChageListener에서 할수 있다.


On일때 isChecked의 값은 True, Off일때 isChecked의 값이 False가 된다.


1
2
3
4
5
6
7
8
9
    <ToggleButton
        android:id="@+id/ToggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <Switch
        android:id="@+id/Switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        ToggleButton tb = (ToggleButton) findViewById(R.id.ToggleButton);
 
        Switch sw = (Switch) findViewById(R.id.Switch);
 
        tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(MainActivity.this"tb : "+isChecked, Toast.LENGTH_SHORT).show();
            }
        });
 
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(MainActivity.this"sw : "+isChecked, Toast.LENGTH_SHORT).show();
            }
        });
    }
cs


Transform 컴포넌트 위치에 Vector3(x좌표, y좌표, z좌표)를 입력하면 이동시킬 수 있다.


Random 위치는 Random.Range(시작, 끝) Random한 값을 가져올 범위를 입력해주면 된다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class EnemyControl : MonoBehaviour
{
    public float Speed = 50.0f;
    private Transform myTransform = null;
    
    // Start is called before the first frame update
    void Start()
    {
        myTransform = GetComponent<Transform>();
    }
 
    // Update is called once per frame
    void Update()
    {
        Vector3 moveAmount = Speed * Vector3.back * Time.deltaTime;
        myTransform.Translate(moveAmount);
 
        if(myTransform.position.y <= -60.0f)
        {
            myTransform.position = new Vector3(Random.Range(-60.0f, 60.0f), 60.0f, 0.0f);
        }
    }
}
cs


'유니티' 카테고리의 다른 글

[유니티] 폭발 이펙트 추가  (0) 2019.03.12
[유니티] Object 충돌  (0) 2019.03.08
[유니티] Object 생성  (0) 2019.03.05
[유니티]Object 삭제  (0) 2019.03.04
[유니티] Object 좌우 이동  (0) 2019.03.02

우선 상태에 따라 drawable 리소스를 만든다.


상태별 이미지를 지정한 selector 리소스를 정의한다. 


정의한 selector 리소스를 Button의 Background로 지정한다.


bg_button_nomal.xml

1
2
3
4
5
6
7
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="270"
        android:endColor="#FF2A68"
        android:startColor="#FF5E3A" />
    <corners android:radius="10dp" />
</shape>
cs


bg_button_pressed.xml

1
2
3
4
5
6
7
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="270"
        android:endColor="#1D62F0"
        android:startColor="#1AD6FD" />
    <corners android:radius="10dp" />
</shape>
cs


selector_button.xml

1
2
3
4
5
6
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bg_button_normal"
        android:state_pressed="false" />
    <item android:drawable="@drawable/bg_button_pressed"
        android:state_pressed="true" />
</selector>
cs


main.xml

1
2
3
4
5
6
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_button"
        android:text="button"/>
cs


'Android' 카테고리의 다른 글

[Android] CheckBox 사용  (0) 2019.03.08
[Android] ToggleButton/Switch 사용  (0) 2019.03.07
[Android] Button 추가하기  (0) 2019.03.05
[Android] ProgressBar Customize  (0) 2019.03.04
[Android] ProgressBar 표시하기  (0) 2019.03.02

1. GameObject 타입의 변수를 추가해준다. 이 변수에 추가한 Object가  생성하게 될 Object가 된다.


2. Instantiate(생성할 Object, 생성할 위치값, 생성할 회전값)으로 Object가 생성된다.


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
public class PlayerControl : MonoBehaviour
{
    public float speed = 15.0f;
    private Transform myTransform = null;
    // 생성하게 될 Object
    public GameObject BulletPrefab = null;
 
 
    // Start is called before the first frame update
    void Start()
    {
        myTransform = GetComponent<Transform>();
    }
 
    // Update is called once per frame
    void Update()
    {   
        float axis = Input.GetAxis("Horizontal");
 
        Vector3 moveAmout = axis * speed * -Vector3.right * Time.deltaTime;
 
        myTransform.Translate(moveAmout);
 
        if(Input.GetKeyDown(KeyCode.Space) == true)
        {
            //인스턴싱.
            Instantiate(BulletPrefab, myTransform.position, Quaternion.identity);
        }
    }
}
cs


'유니티' 카테고리의 다른 글

[유니티] Object 충돌  (0) 2019.03.08
[유니티] Random 위치에 Object 이동  (0) 2019.03.07
[유니티]Object 삭제  (0) 2019.03.04
[유니티] Object 좌우 이동  (0) 2019.03.02
[유니티] Object에 Script연결  (0) 2019.03.01

layout에 Button을 추가한다.


1
2
3
4
5
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button"/>
cs


Button의 클릭 이벤트는 setOnClickListener를 이용한다.


layout에서 onClick속성을 이용하는 방법도 있지만 추천하는 방법은 아니므로 setOnClickListener에 구현하는 것이 좋다. 


1
2
3
4
5
6
7
8
9
10
11
12
13
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this"버튼이 클릭되었습니다.", Toast.LENGTH_SHORT).show();
            }
        });
    }
cs


'Android' 카테고리의 다른 글

[Android] ToggleButton/Switch 사용  (0) 2019.03.07
[Android] Button 상태에 따라 이미지 변경하기  (0) 2019.03.06
[Android] ProgressBar Customize  (0) 2019.03.04
[Android] ProgressBar 표시하기  (0) 2019.03.02
[Android] gravity 속성  (0) 2019.03.01

+ Recent posts