Enable If Attribute:用于任何屬性,并且可以在檢查器中啟用或禁用該屬性。相關(guān)屬性時,使用此選項可啟用屬性编丘。
這個特性的效果主要是當指定條件滿足時,啟用對應(yīng)的屬性彤悔,默認傳入的參數(shù)為對應(yīng)屬性的名稱嘉抓,如果為True或者不為null時,啟用對應(yīng)屬性
[EnableIf("IsToggled")]
public int EnableIfToggled;
[EnableIf("SomeObject")]
public Vector3 EnabledWhenHasReference;
還以指定一個選項值晕窑,當指定的屬性與這個值拼配時抑片,顯示屬性
[EnableIf("SomeEnum", InfoMessageType.Info)]
public Vector2 Info;
[EnableIf("SomeEnum", InfoMessageType.Error)]
public Vector2 Error;
[EnableIf("SomeEnum", InfoMessageType.Warning)]
public Vector2 Warning;
可以使用@特殊符號寫入表達式,其表達式的值作為實參
[EnableIf("@this.IsToggled && this.SomeObject != null || this.SomeEnum == InfoMessageType.Error")]
public int EnableWithExpression;
完整示例代碼
using Sirenix.OdinInspector;
using UnityEngine;
public class EnableIfAttributeExample : MonoBehaviour
{
public UnityEngine.Object SomeObject;
[EnumToggleButtons]
public InfoMessageType SomeEnum;
public bool IsToggled;
[EnableIf("SomeEnum", InfoMessageType.Info)]
public Vector2 Info;
[EnableIf("SomeEnum", InfoMessageType.Error)]
public Vector2 Error;
[EnableIf("SomeEnum", InfoMessageType.Warning)]
public Vector2 Warning;
[EnableIf("IsToggled")]
public int EnableIfToggled;
[EnableIf("SomeObject")]
public Vector3 EnabledWhenHasReference;
[EnableIf("@this.IsToggled && this.SomeObject != null || this.SomeEnum == InfoMessageType.Error")]
public int EnableWithExpression;
}