This will update your component into the visual area of Unity.
Some public variables will be fashioned and change with your selected attributes.
The attributes can be used into programming using the [].
The class can have on top this attribute and is followed by the code source.
this will add a URL
1 | [HelpURL("http://free-tutorials.org")] |
this will block drag and drop your component to just once.
1 | [DisallowMultipleComonent] |
this will block remove the component depend on TEST.
1 | [RequireComponent(typeof(TEST))] |
You can remove step by step , but when you add this also add both this component and the TEST component.
Many attributes use sections used to separate and show info, like:
show word Details
1 | [Headers("Details")] |
make a space into Unity Inspector
1 | [Space] |
show the header like Stats
1 | [Header("Stats")] |
this tell is multiline for the text area
1 | [Multiline] |
renders a flexible textbox with a scrollbar.
1 | [TextArea] |
create a slider from 0.0 to 360.0 for variable circle_angle
1 | [Range(0.0, 360.0)] [Tooltip("select angle!")] public float circle_angle; |
will hide the public variables
1 | [HideInInspector] |
this will execute the code, for example, you can execute the camera follow even you don’t use play mode.
1 | [ExecuteInEditMode] |
this allows running code GiveObject by creating a menu on
Tools – Give object – Do it
1 | [UnityEditor.MenuItem("Tools/Give object/Do it")] |
then when play and used it this will run GiveObject
1 2 3 4 | public static void GiveObject() { ... } |
this gets serialized even though it is private
1 | [SerializeField] |
This can be a good example when you use sprites and colliders.
Example:
1 2 | [SerializeField] MeshRenderer target; SpriteRenderer srend; |
Then you need to use them and need to tell Unity something about your code
1 | [System.Obsolete("This one is old")] |
… and will show on development like a warning message “This one is old”
This part of the tutorial is more complex and is about events:
This attribute tells Unity can serializable something ( like using events system).
1 | [System.Serializable] |
Example with one serializable event:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //use Events using UnityEngine.Events; // add the attribute System.Serializable [System.Serializable] // create a class MyFloatEvent from UnityEvent and "save it like" serializable. public class MyFloatEvent : UnityEvent {} // your C# script named Something_with_event public class Something_with_event:Monobehavior { // the event will send to variable to read public float read_event = 0; // use the class event public MyFloatEvent on_event_Change; // changes over Update will send to the void Update(){ // this will update the variable to read read_event += Time.deltaTime *0.1f; // this will read the variable to read on_event_Change.Invoke(read_event) } } |
I named the script, class, and variable for the event just to understand how this works.
You can use anything else for the event : read_event_player, read_heat, read
You can change the name of the script or the class for the event.
You can add Listener into your script and use them
NOTE: You can add new listeners to the event, but you cannot see them in the Unity Inspector.
Example: use Start area of code to add this:
1 | on_event_Change.AddListener(on_event_speak); |
then create this to use it:
1 2 3 4 | public void on_event_speak(float read_event) { Debug.Log ("I spek only here" + read_event); } |
If you take a look on Unity Inspector you don’t see this, just the print of Debug.Log
The part of This event is old but is often used by users.
You can use C# events and not Unity events.
The Unity events are super powerful for Unity development and are easy to use them.
If not you need to have some custom code for this type of tasks.
You can use something else into events (but not more 4 args) :
1 | public class MyFloatEvent : UnityEvent<float, Vector3, Transform> {} |
and use it
1 | on_event_Change.Invoke(read_event, transform.position, Transform wow) |
This is a little tricky but you can pass anything you want with events system.