Skip to content

Create my tagged component

Nectunia edited this page Oct 27, 2020 · 13 revisions

It's pretty simple to create a tagged component :

  1. Create you own class wich inherite from MonoBehaviourTagged.
  2. Make sure that your component can find Property components or find MonoBehaviourTagged components it needs to interact with.
  3. Script the mechanic you want to implement.
  4. (Optional) Make a custom Editor if need.

As an advanced example you can have a look at the PropertyTriggerHandler component available in this asset. It can be find in the Unity editor Project tab : "Assets/Plugins/Nectunia/PropertyInterface/Package/Scripts/Components/"

When inherite from MonoBehaviourTagged, you can catch the triggers onValuesTypeChanged() and OnTagIDChanged(). Below an example class :

using Nectunia.PropertyInterface;
using System;
using UnityEngine;

/// <summary>
/// My custom tagged component
/// </summary>
public class MyCustomTaggedComponent : MonoBehaviourTagged{
    public string _tagTypeName;
    [SerializeField]
    private Property _attachedProperty;

    public void Update () {
        // Property component have only 1 value => it's name "A"
        // Here we log its value in the console
        Debug.Log("The value of the attached Property is : " + this._attachedProperty?.A.Value.ToString());
    }

    /// <summary>
    /// Event triggered when this.TagID changed.
    /// </summary>
    protected override void OnTagIDChanged () {
        // Get in the current GameObject the first Property component wich refer the same TagID
        this._attachedProperty = this.gameObject.GetProperty(this.TagID);
    }

    /// <summary>
    /// Event triggered when this.ValuesType changed.
    /// </summary>
    protected override void OnValuesTypeChanged () {
        // Get the Type name of the refered Tag's ValueType
        this._tagTypeName = this.ValuesType.GetSystemType().Name;

        // Another solution
        //this._tagTypeName = Enum.GetName(typeof(Tag.ValueType), this.ValuesType);
    }
}

Default Editor

MonoBehaviourTagged provide a default Editor wich draw its fields and those of its children.

Here the default Editor for the above custom tagged component example :
MyCustomTaggedComponent_Editor

Clone this wiki locally