TylerTemp / SaintsField

A Unity Inspector extension tool focusing on script fields inspector enhancement
MIT License
154 stars 9 forks source link

when use [showif] not work on array #5

Closed ZeroUltra closed 6 months ago

ZeroUltra commented 6 months ago

Hi !!! code like this:

public class NewMonoBehaviour : MonoBehaviour
{
    public bool useTargetIds;
    [ShowIf(nameof(useTargetIds))]
    public int[] targetIds;

    public TestAllowClass linkgo;

    [System.Serializable]
    public class TestAllowClass
    {
        public bool useTargetIds;
        [ShowIf(nameof(useTargetIds))]
        public int[] targetIds;
    }
}

image

ZeroUltra commented 6 months ago

Unity2021.3.33 Unity2022.3.16

TylerTemp commented 6 months ago

Thanks for reporting.

This is because:

Directly using on list/array will apply to every direct element of the list, this is a limit from Unity.

The solution is to write one attribute for SaintsEditor.

This will be implemented in the future. But please note, SaintsEditor is not a core value of this project (it just works), so it‘s low priority and might take some time (versions) to see this happens.

ZeroUltra commented 6 months ago

Thank you for your reply. I look forward to this feature.

ZeroUltra commented 6 months ago

If someone desperately needs it, I have another plan, though not perfect.

  1. [System.Serializable]
    public class NestedArray<T>
    {
    public T[] Array;
    }
  2. 
    using System;
    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif

[AttributeUsage(AttributeTargets.Field, Inherited = true)] public class HideNestedArrayAttribute : PropertyAttribute {

}

if UNITY_EDITOR

[CustomPropertyDrawer(typeof(HideNestedArrayAttribute))] public class HideNestedArrayDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var array = property.FindPropertyRelative("Array"); EditorGUI.PropertyField(position, array, new GUIContent(property.displayName)); } public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { var array = property.FindPropertyRelative("Array"); // These numbers are tried out. I don't know how to get it. if (array.isExpanded) { return 70 + Mathf.Clamp((array.arraySize-1) * 20, 0, float.MaxValue); } else return 20; } }

endif

ok, test it

using UnityEngine; using SaintsField;

public class NewMonoBehaviour : MonoBehaviour { public bool useIds; [ShowIf("useIds"), HideNestedArray] public NestedArray ids;

public TestArray testArray;
public TestArray[] testArrays;

[System.Serializable]
public class TestArray
{
    public bool useIds;
    [ShowIf("useIds"), HideNestedArray]
    public NestedArray<int> ids;
}

}


![image](https://github.com/TylerTemp/SaintsField/assets/38253805/119e3702-4b10-4902-913f-62917d327fc2)
TylerTemp commented 6 months ago

Please update 2.1.12 to use PlayaShowIf/PlayaHideIf for array. Note: you need to enable SaintsEditor to make it work. See Set Up SaintsEditor in document. And note:

Please note, any Editor level component can not work together with each other (it will not cause trouble, but only one will actually work). Which means, OdinInspector, NaughtyAttributes, MarkupAttributes, SaintsEditor can not work together.

If the attributes have any errors, please open a new issue )))