using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using NUnit.Framework; using TMPro; using Unity.AI.Navigation; using UnityFusion.CLRBinding.Adapter; using UnityEditor; using UnityEngine; using UnityEngine.AI; using UnityEngine.Animations; using UnityEngine.Playables; using UnityEngine.Rendering; using UnityEngine.U2D; using UnityEngine.UI; using UnityEngine.Video; namespace Phantom.XRMOD.UnityFusion.Editor { [CustomEditor(typeof(XRMODBehaviourAdapter.Adapter), true)] public class MonoBehaviourAdapterEditor : UnityEditor.Editor { private Dictionary foldouts = new(); private XRMODBehaviourAdapter.Adapter adapter; private void OnEnable() { adapter = target as XRMODBehaviourAdapter.Adapter; if (adapter != null) { Editor.ComponentName.Set(adapter, adapter.ILInstance.Type.Name, Editor.ModifyOptions.Defaults); } } public override void OnInspectorGUI() { serializedObject.Update(); if (adapter != null) { var tmp_Instance = adapter.ILInstance; if (tmp_Instance != null) { var tmp_Fields = tmp_Instance.Type.ReflectionType .GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); List tmp_FieldInfos = new(); foreach (FieldInfo tmp_Field in tmp_Fields) { if (tmp_Field.IsPublic) { tmp_FieldInfos.Add(tmp_Field); } else { var tmp_FieldsOfSerializeField = tmp_Field.GetCustomAttributes(typeof(SerializeField), true); if (tmp_FieldsOfSerializeField.Length > 0) tmp_FieldInfos.Add(tmp_Field); } } //All fields foreach (var tmp_I in tmp_FieldInfos) { var tmp_FieldName = tmp_I.Name; var tmp_GotValue = tmp_I.GetValue(tmp_Instance); if (tmp_GotValue == null) { EditorGUILayout.LabelField(tmp_FieldName, "*NULL"); continue; } var tmp_CType = tmp_GotValue.GetType(); if (tmp_CType == typeof(string)) { var tmp_Value = EditorGUILayout.TextField(tmp_FieldName, (string) tmp_GotValue); tmp_I.SetValue(tmp_Instance, tmp_Value); } else if (tmp_CType.IsPrimitive) { if (tmp_CType == typeof(float)) { var tmp_Value = EditorGUILayout.FloatField(tmp_FieldName, (float) tmp_GotValue); tmp_I.SetValue(tmp_Instance, tmp_Value); } else if (tmp_CType == typeof(int)) { var tmp_Value = EditorGUILayout.IntField(tmp_FieldName, (int) tmp_GotValue); tmp_I.SetValue(tmp_Instance, tmp_Value); } else if (tmp_CType == typeof(bool)) { var tmp_Value = EditorGUILayout.Toggle(tmp_FieldName, (bool) tmp_GotValue); tmp_I.SetValue(tmp_Instance, tmp_Value); } } else if (tmp_CType.IsArray || tmp_CType.IsGenericType) { var tmp_ElementType = tmp_CType.IsGenericType ? tmp_CType.GenericTypeArguments[0] : tmp_CType.GetElementType(); if (tmp_ElementType == typeof(ScriptableObject)) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(AudioClip)) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(SpriteAtlas)) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(Mesh)) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(Sprite)) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(AnimationClip)) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(Texture)) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(Material)) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(Shader)) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(float)) { DrawFloatList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(int)) { DrawIntList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(string)) { DrawStrList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(Transform)) { DrawList(tmp_GotValue, tmp_FieldName); } // else if (tmp_ElementType.IsSubclassOf(typeof(Component))) // { // DrawList(tmp_GotValue, tmp_FieldName); // } else if (tmp_ElementType == (typeof(MeshFilter))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(MeshRenderer))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(SkinnedMeshRenderer))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(TextMesh))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(TextMeshPro))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(ParticleSystem))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(TrailRenderer))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(LineRenderer))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Projector))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(LensFlare))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(ParticleSystemForceField))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(WindZone))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Rigidbody))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(ConstantForce))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(CharacterController))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Cloth))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(BoxCollider))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(SphereCollider))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(CapsuleCollider))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(MeshCollider))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(TerrainCollider))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(WheelCollider))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(FixedJoint))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(SpringJoint))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(HingeJoint))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(CharacterJoint))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(ConfigurableJoint))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(NavMeshAgent))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(NavMeshObstacle))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(NavMeshSurface))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(NavMeshModifier))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(NavMeshModifierVolume))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(NavMeshLink))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(AudioSource))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(AudioListener))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(AudioReverbZone))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(AudioReverbFilter))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(AudioLowPassFilter))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(AudioHighPassFilter))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(AudioDistortionFilter))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(AudioEchoFilter))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(AudioChorusFilter))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(VideoPlayer))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Camera))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Light))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(LightProbeProxyVolume))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(LightProbes))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(LightProbeGroup))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(ReflectionProbe))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(OcclusionArea))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(LODGroup))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(CanvasRenderer))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(SpriteRenderer))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(SortingGroup))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Skybox))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(FlareLayer))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(StreamingController))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(RectTransform))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(CanvasRenderer))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Canvas))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(CanvasScaler))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(VerticalLayoutGroup))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(HorizontalLayoutGroup))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(GridLayoutGroup))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(LayoutElement))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(ContentSizeFitter))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(AspectRatioFitter))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Text))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(TextMeshProUGUI))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(InputField))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(TMP_InputField))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Dropdown))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(TMP_Dropdown))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Slider))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Scrollbar))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(ScrollRect))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Toggle))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(ToggleGroup))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Selectable))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Shadow))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Outline))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(RectMask2D))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Mask))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(PositionAsUV1))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(PlayableDirector))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Animator))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Animation))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Transform))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Terrain))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Volume))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(BillboardRenderer))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(SpriteShapeRenderer))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(SpriteMask))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(Grid))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(PositionConstraint))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(RotationConstraint))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(ScaleConstraint))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(ParentConstraint))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(AimConstraint))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(LookAtConstraint))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == (typeof(SpriteMask))) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(GameObject)) { DrawList(tmp_GotValue, tmp_FieldName); } else if (tmp_ElementType == typeof(MonoBehaviour)) { DrawList(tmp_GotValue, tmp_FieldName); } } else if (tmp_CType.IsEnum) { var tmp_EnumValue = EditorGUILayout.TextField($"[Enum]{tmp_FieldName}", (string) tmp_GotValue); tmp_I.SetValue(tmp_Instance, tmp_EnumValue); } else { if (typeof(UnityEngine.Object).IsAssignableFrom(tmp_CType)) { //Unity's type var tmp_Res = EditorGUILayout.ObjectField(tmp_FieldName, tmp_GotValue as UnityEngine.Object, tmp_CType, true); tmp_I.SetValue(tmp_Instance, tmp_Res); } else { //other EditorGUILayout.LabelField(tmp_FieldName, tmp_GotValue.ToString()); } } } } } serializedObject.ApplyModifiedProperties(); } private void DrawList(object _value, string _fieldName) where T : UnityEngine.Object { try { foldouts.TryAdd(_fieldName, false); if (_value == null) return; if (_value is IList tmp_List) { foldouts[_fieldName] = EditorGUILayout.BeginFoldoutHeaderGroup(foldouts[_fieldName], _fieldName); if (foldouts[_fieldName]) { for (int tmp_Idx = 0; tmp_Idx < tmp_List.Count; tmp_Idx++) { tmp_List[tmp_Idx] = (T) EditorGUILayout.ObjectField( new GUIContent(tmp_Idx.ToString()), tmp_List[tmp_Idx], typeof(T), false); } } } else { Debug.LogError($"{_value.GetType()},{typeof(List)},{_value.GetType() == typeof(List)}"); } } catch (Exception tmp_Exception) { Debug.LogError(tmp_Exception); } EditorGUILayout.EndFoldoutHeaderGroup(); } private void DrawFloatList(object _dataIndex, string _fieldName) { if (!foldouts.ContainsKey(_fieldName)) foldouts.Add(_fieldName, false); var tmp_InstanceGOList = (IList) _dataIndex; foldouts[_fieldName] = EditorGUILayout.BeginFoldoutHeaderGroup(foldouts[_fieldName], _fieldName); if (foldouts[_fieldName]) { for (int tmp_Idx = 0; tmp_Idx < tmp_InstanceGOList.Count; tmp_Idx++) { tmp_InstanceGOList[tmp_Idx] = EditorGUILayout.FloatField(new GUIContent(tmp_Idx.ToString()), tmp_InstanceGOList[tmp_Idx]); } } EditorGUILayout.EndFoldoutHeaderGroup(); } private void DrawIntList(object _dataIndex, string _fieldName) { if (!foldouts.ContainsKey(_fieldName)) foldouts.Add(_fieldName, false); var tmp_InstanceGOList = (IList) _dataIndex; foldouts[_fieldName] = EditorGUILayout.BeginFoldoutHeaderGroup(foldouts[_fieldName], _fieldName); if (foldouts[_fieldName]) { for (int tmp_Idx = 0; tmp_Idx < tmp_InstanceGOList.Count; tmp_Idx++) { tmp_InstanceGOList[tmp_Idx] = EditorGUILayout.IntField(new GUIContent(tmp_Idx.ToString()), tmp_InstanceGOList[tmp_Idx]); } } EditorGUILayout.EndFoldoutHeaderGroup(); } private void DrawStrList(object _dataIndex, string _fieldName) { if (!foldouts.ContainsKey(_fieldName)) foldouts.Add(_fieldName, false); var tmp_InstanceGoList = (IList) _dataIndex; foldouts[_fieldName] = EditorGUILayout.BeginFoldoutHeaderGroup(foldouts[_fieldName], _fieldName); if (foldouts[_fieldName]) { for (int tmp_Idx = 0; tmp_Idx < tmp_InstanceGoList.Count; tmp_Idx++) { tmp_InstanceGoList[tmp_Idx] = EditorGUILayout.TextField(new GUIContent(tmp_Idx.ToString()), tmp_InstanceGoList[tmp_Idx]); } } EditorGUILayout.EndFoldoutHeaderGroup(); } public static Dictionary GetInternalInspectorTitlesCache() { Type inspectorTitlesType = typeof(ObjectNames).GetNestedType("InspectorTitles", BindingFlags.Static | BindingFlags.NonPublic); var inspectorTitlesField = inspectorTitlesType.GetField("s_InspectorTitles", BindingFlags.Static | BindingFlags.NonPublic); return (Dictionary) inspectorTitlesField.GetValue(null); } } }