반응형

public 변수로 선언하되 유니티 inspecto 창에는 보이지 않게 하려면 HideInInspector 키워드를 사용 하면 된다



[HideInInspector] Description

변수를 인스펙터 뷰에서 보이지 않도록 설정합니다.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { [HideInInspector] public int p = 5; }

https://docs.unity3d.com/kr/current/ScriptReference/HideInInspector.html







[SerializeField] Description

유니티가 private 필드를 직렬화(Serialization)하도록 설정합니다.

특별한 경우가 아니면 사용하지 않습니다. 유니티가 사용자의 스크립트를 직렬화 하는 경우에, public 필드만 직렬화합니다. 추가로 유니티가 private 필드를 직렬화하도록 설정하고 싶은 경우에, SerializeField 속성(attribute)을 필드에 추가할 수 있습니다.

직렬화를 하게되면 인스펙터에서 해당 필드가 노출됩니다. 유니티는 사용자의 모든 스크립트 컴포넌트를 직렬화 하고, 스크립트 컴포넌트를 직렬화된 버젼으로 다시 로드하고 재생성합니다. .NET의 serialization 기능과는 아무런 영향을 주지 않고, 유니티 내부에서만 동작합니다.

사용되는 직렬화 시스템은 다음을 수행 할 수 있습니다:

- CAN serialize public nonstatic fields (of serializable types)
- CAN serialize nonpublic nonstatic fields marked with the [SerializeField] attribute.
- CANNOT serialize static fields.
- CANNOT serialize properties.


유니티에서 직렬화할 수 있는 타입인 경우에만 사용자의 필드가 직렬화 될 수 있습니다:

직렬화될 수 있는(Serializable) 타입은 다음과 같습니다:

- All classes inheriting from UnityEngine.Object, for example GameObject, Component, MonoBehaviour, Texture2D, AnimationClip.
- All basic data types like int, string, float, bool.
- Some built-in types like Vector2, Vector3, Vector4, Quaternion, Matrix4x4, Color, Rect, LayerMask.
- Arrays of a serializable type
- List of a serializable type)
- Enums
- Structs

요점 정리: List(또는 Array)에 같은 element를 넣은 경우, List가 직렬화 될 때, 새 list를 두번 복사하는 대신 element의 두 사본을 얻게됩니다.

Awake 함수에서 직렬화하지 않는 Dictionary에 연결해서 활용할 수 있습니다. 이 방법은 Dictionary를 수정하고 다시 저장 하고 싶은 경우의 해결방법은 아니지만, 다른 많은 클래스에서 편리한 기능이 될 수 있습니다. 유니티 스크립트 사용자들을 위해: c#의 필드는 UnityScript안의 스크립트 변수이고, [SerializeField]는 @SerializeField가 됩니다. 클래스 상의 [Serializable]는 UnityScript에서 @script Serializable이 됩니다.

For UnityScript users: Fields in c# is a script variable in UnityScript, and [SerializeField] becomes @SerializeField. [Serializable] on a class becomes @script Serializable in a UnityScript.



https://docs.unity3d.com/kr/current/ScriptReference/SerializeField.html





FormerlySerializedAs 의 경우 너무 많이 스게 되면 코드가 복잡해 질 수 수있다


[Unity] 데이터 손실 없이 SerializedField 이름 바꾸기



유니티로 작업하다 보면 데이터를 저장하기 위해 SerializedField와 같은 어트리뷰트를 사용한다. 문제는 개발 도중 이 필드의 이름이 변경되어 기존 저장된 데이터들을 사용하지 못하게 되는 경우가 있다. 이런 경우 FormerlySerializedAs 어트리뷰트를 이용해 기존 버전과의 호환성을 유지해 줄 수 있다.


만일 아래와 같이 m_MyVariable이라는 멤버 변수를 사용하고 있는데 :

class MyClass : MonoBehaviour

{

  [SerializeField]

  private string m_MyVariable;

}


m_MyVariable의 이름이 다른 것으로, 예를 들자면 m_ABetterName와 같이 바뀐다면 기존에 m_MyVariable의 이름으로 저장된 데이터를 읽어 들이지 못하게 된다.

이럴 경우 아래와 같이 작성해주게 된다면 기존  m_MyVariable의 데이터를 m_ABetterName으로 치환해서 읽어 들일수 있다 :


class MyClass : MonoBehaviour

{

  [FormerlySerializedAs("m_MyVariable")]

  [SerializeField]

  private string m_ABetterName;

}


그리고 FormerlySerializedAs 는 여러번 중복해서 사용가능하다. 만일 이름이 여러 번 변경 된다고 하더라도 변경 히스토리에 따라 FormerlySerializedAs 어트리뷰트를 유지해 준다면 여전히 데이터 손실 없이 사용이 가능하다.


class MyClass : MonoBehaviour

{

  [FormerlySerializedAs("m_MyVariable")]

  [FormerlySerializedAs("m_ABetterName")]

  [SerializeField]

  private string m_EvenBetterName;

}


원문 : https://blogs.unity3d.com/2015/02/03/renaming-serialized-fields/



http://kukuta.tistory.com/203







RequireComponent

class in UnityEngine

Description

RequireComponent 속성은 요구되는 의존 컴포넌트를 자동으로 추가해줍니다.!!

RequireComponent를 사용하는 스크립트를 추가하면, 요구되는 컴포너트가 자동으로 해당 게임오브젝트에 추가됩니다. 설정 오류를 피하는 데 유용합니다. 예를 들어, 리지드 바디가 요구되는 스크립트가, 항상 같은 게임오브젝트에 첨부되는 경우. RequireComponent를 사용하면 이 작업이 자동으로 이루어 지기 때문에, 설정에 대한 실수를 할 염려가 없습니다. 

Note that RequireComponent only checks for missing dependencies during the moment the component is added to a GameObject. Existing instances of the component whose GameObject lacks the new dependencies will not have those dependencies automatically added.

using UnityEngine;

// PlayerScript requires the GameObject to have a Rigidbody component [RequireComponent (typeof (Rigidbody))] public class PlayerScript : MonoBehaviour { Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } void FixedUpdate() { rb.AddForce(Vector3.up); } }

Constructors

RequireComponentRequire a single component.


반응형

+ Recent posts