Attributes
Unity에서 제공하는 attribute들
AddComponentMenu
기본적으로 스크립트는 유니티의 Component->Scripts 메뉴에 자동추가 된다.
[AddComponentMenu("Transform/Follow Transform")] public class FollowTransform : MonoBehaviour { } |
ContextMenu, ContextMenuItem
스크립트를 우클릭시 뜨는 context menu에 커맨드를 추가할 수 있다. Item은 오른쪽 버튼을 누르면 나타는 메뉴를 말한다.
public class ContextTesting : MonoBehaviour { /// Add a context menu named "Do Something" in the inspector /// of the attached script. [ContextMenu ("Do Something")] void DoSomething () { Debug.Log ("Perform operation"); } } |
이렇게 하면 컨텍스트 메뉴에 Do Something이 나타나고, 선택하면
ExecuteInEditMode
기본적으로 play mode일 때만 스크립트가 실행되나, 이 attribute를 사용하면 edit mode일 때도 스크립트가 실행되게 한다. (Update, FixedUpdate, and OnGUI functions)
using UnityEngine; using System.Collections; [ExecuteInEditMode] public class example : MonoBehaviour { public Transform target; void Update() { if (target) transform.LookAt(target); } } |
Header
inspector에서 설명을 추가한다
public class HeadAttrinuteTest : MonoBehaviour { [Header("- Health Point")] public int max_hp; public int current_hp; [Header("- Magic Point")] public int max_mp; public int current_mp; } |
HideInInspector
inspector에서 안보이게 한다.
using UnityEngine; using System.Collections; public class example : MonoBehaviour { [HideInInspector] public int p = 5; } |
Multiline(value)
장문의 문자열을 출력하게 할수 있다.
using UnityEngine; using System.Collections; public class MultilineAttributeTest : MonoBehaviour { [Multiline(3)] public string test_string = ""; } |
NonSerialized
앞서 HideInInspector는 값을 유지하지만, 이건 그냥 리셋하여 디폴트값으로 바꾼다.
class Test { // p will not be shown in the inspector or serialized [System.NonSerialized] public int p = 5; } |
Range(min, max)
인스펙터에서 변수값을 변경할때 범위값을 지정해 슬라이드로 조절할 수 있게 한다.
using UnityEngine; using System.Collections; public class RangeAttributeTest : MonoBehaviour { [Range(0, 10)] public int range_value_int = 0; [Range(0.0f, 10.0f)] public float range_value_float = 0; } |
RequireComponent
함께 필요한 다른 컴포넌트가 자동으로 추가된다.
[RequireComponent (typeof (Rigidbody))] public class PlayerScript : MonoBehaviour { void FixedUpdate() { rigidbody.AddForce(Vector3.up); } } |
이렇게 하면 작성하면, PlayerScript추가할 때 Rigidbody도 같이 추가된다.
System.Serializable
[System.Serializable] class Test { public int p = 5; public Color c = Color.white; } |
SerializeField
private필드를 강제로 serialize한다. 그러므로 inspector에서도 편집이 가능해진다.
using UnityEngine; public class SomePerson : MonoBehaviour { //This field gets serialized because it is public. public string name = "John"; //This field does not get serialized because it is private. private int age = 40; //This field gets serialized even though it is private //because it has the SerializeField attribute applied. [SerializeField] private bool hasHealthPotion = true; void Update () { } } |
Space( value )
변수들의 간격을 조절 할 수 있다
using UnityEngine; using System.Collections; public class SpaceAttributeTest : MonoBehaviour { public int max_hp; public int current_hp; [Space(20)] public int max_mp; public int current_mp; } |
System.Nonserialized
System.Nonserialized는 HideInInspector 처럼 public으로 선언된 변수들을 인스펙터에서 숨겨주는 역활을 한다. 다만 HideInInspector와는 달리 이전에 인스펙터에서 값을 수정했다고 하더라도 값을 유지하지 않고 스크립트에서 설정한 초기값이 적용된다.
using UnityEngine; using System.Collections; public class SystemNonSerializedTest : MonoBehaviour { public int visible_value = 0; [System.NonSerialized] public int hide_value = 5; } |
TestArea(min, max)
multiline과 비슷하게 텍스트 입력을 위한 공간을 지정한다. 최소와 최대 크기를 지정해서 사용
using UnityEngine; using System.Collections; public class TextAreaAttributeTest : MonoBehaviour { [TextArea(3, 10)] public string test_string = ""; } |
Tooltip
변수에 마우스를 롤오버 했을때 팁이 표시되게 한다.
using UnityEngine; using System.Collections; public class TooltipAttributeTest : MonoBehaviour { [Tooltip("Tooltip Attribute Test")] public int value = 0; } |
'Technical Report > Unity' 카테고리의 다른 글
| ColorSuite.cs by Keijiro (0) | 2016.08.29 |
|---|---|
| Unity sprite texture shader (0) | 2016.08.09 |
| UE4 25 tips for Unreal Engine 4(2) (0) | 2016.07.10 |
| UE4 25 tips for Unreal Engine 4(1) (2) | 2016.07.09 |
| Unity GLSL base shader (0) | 2016.06.03 |