유니티

SerializeField로 Inspector에서 변수 값 조정

Nhahan 2022. 6. 8. 18:01
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Driver : MonoBehaviour
{
    float steerSpeed = 0.1f;
    [SerializeField] float moveSpeeed = 0.01f;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(0, 0, steerSpeed);
        transform.Translate(0, moveSpeeed, 0);
    }
}

 

원하는 변수 선언 시 앞에 [SerializeField] 를 추가하면

 

실수로 Speeed라고 했네

Inspector에서 값을 조정할 수 있다.

 

 


 

 

public을 붙여도 되긴 한데, 둘의 차이점은

public은 외부에서 접근 가능하고, SerializeField는 외부에서 접근 불가하다.

 

가장 좋은건 public을 쓰지 않고 SerializeField를 써주고, Getter로 접근하게끔 하는 것이 정석이라고 본다.

 

 

 

 

728x90