2016. 2. 19. 13:58





AddComponentMenu : 유니티 메뉴 추가.

1
2
3
4
5
// C# example:
[AddComponentMenu("Transform/Follow Transform")]
public class FollowTransform : MonoBehaviour
{
}
s

ContextMenu : 우클릭 메뉴 추가.

1
2
3
4
5
6
7
8
9
10
11
12
// C# example:
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");    
    }
}
 
cs


ExecuteInEditMode : 에디트 모드에서 스크립트 실행.

1
2
3
4
5
6
7
8
9
10
11
12
13
 
using UnityEngine;
using System.Collections;
 [ExecuteInEditMode]
public class example : MonoBehaviour 
{
    public Transform target;
    void Update() 
    {        
        if (target)            
        transform.LookAt(target);
    }
}
cs

 

HideInInspector : 인스펙터에서 속성 감추기, 이전 세팅값은 유지.

1
2
3
4
5
6
7
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour 
{    
    [HideInInspector]    
    public int p = 5;
}
cs

NonSerialized : 인스펙터에서 속성 감추기, 이전 세팅값은 무시.

1
2
3
4
5
6
// C# Example
class Test {    
    // p will not be shown in the inspector or serialized    
    [System.NonSerialized]    
    public int p = 5;
}
cs

RPC : 원격지 호출 함수로 지정, 보내는 쪽과 받는 쪽 모두 다 존재해야 함.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using UnityEngine;
using System.Collections; 
public class example : MonoBehaviour {
    public Transform cubePrefab;
    void OnGUI() {
        if(GUILayout.Button("SpawnBox")) {      
        NetworkViewID viewID = Network.AllocateViewID();           
        networkView.RPC("SpawnBox", RPCMode.AllBuffered, viewID,transform.position);
        }
    }
    [RPC]
    void SpawnBox(NetworkViewID viewID,Vector3 location) 
    {        
        Transform clone;
        clone = Instantiate(cubePrefab, location, Quaternion.identity) as Transform as Transform;
        NetworkView nView;
        nView = clone.GetComponent<NetworkView>();
        nView.viewID = viewID;
    }
}
cs

 

RequireComponent : 컴포넌트 자동 추가.

1
2
3
4
5
6
7
[RequireComponent (typeof (Rigidbody))]
public class PlayerScript : MonoBehaviour {
 
    void FixedUpdate()  {     
        rigidbody.AddForce(Vector3.up);
    }
}

cs

Serializable : 인스펙터에 인스턴스의 하위 속성 노출.

1
2
3
4
5
6
7
8
9
10
11
12
// C# Example
[System.Serializable]
class Test
{
    public int p = 5;
    public Color c = Color.white;
}
 
class Sample : MonoBehaviour
{
    public Test serializableObj; // 인스펙터에 p, c가 노출된다.
}
cs

SerializeField : 인스펙터에 비공개 멤버 노출.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//C# example
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 () {
    }
}
cs





Posted by 시리시안