public void DrawInspector <OCNewType>(OCNewType target) where OCNewType : new() { if (target == null) { GUILayout.Label("Object is Null"); return; } Type exposePropertiesType = typeof(OCExposePropertyFieldsAttribute); OCExposePropertyFieldsAttribute.OCExposure exposure = OCExposePropertyFieldsAttribute.OCExposure.None; if (exposePropertiesType != null) { object[] attributes = target.GetType().GetCustomAttributes(exposePropertiesType, true); if (attributes != null && attributes.Length > 0) { exposure = (attributes[0] as OCExposePropertyFieldsAttribute).Exposure; } } SerializedProperty unityPropertyFieldIterator = serializedObject.GetIterator(); List <OCPropertyField> allPropertyFields = OCPropertyField.GetAllPropertiesAndFields ( target , unityPropertyFieldIterator , exposure ); DrawSerializedProperties(allPropertyFields); FindMissingScripts(ref allPropertyFields); // OCPropertyField.SetAllPropertiesAndFields // ( // target // , allPropertyFields // ); }
/// <summary> /// Gets all properties and fields from a given C# Type object and/or Unity /// Type property field. Returns true if we found any. /// </summary> /// <returns> /// All properties and fields exposed (does not overwrite existing list). /// </returns> /// <param name='obj'> /// The optional C# Type object. /// </param> /// <param name='unityPropertyField'> /// The optional Unity Type property field. /// </param> /// <param name='allPropertiesAndFields'> /// If set to <c>true</c>, all the properties and fields. /// </param> public static List <OCPropertyField> GetAllPropertiesAndFields (object obj = null , SerializedProperty unityPropertyField = null , OCExposePropertyFieldsAttribute.OCExposure exposure = OCExposePropertyFieldsAttribute.OCExposure.None ) { List <OCPropertyField> allPropertyFields = new List <OCPropertyField>(); Type type = obj.GetType(); if ( type == null && unityPropertyField == null ) { return(null); } Stack <OCPropertyField> candidates = new Stack <OCPropertyField>(); if (unityPropertyField != null) { while (unityPropertyField.NextVisible(true)) { if (unityPropertyField.editable && !unityPropertyField.DuplicateCommand()) { string publicName = ObjectNames.NicifyVariableName(unityPropertyField.name); if (ExcludedPropertyFieldPublicNames.Contains(publicName)) { continue; } candidates.Push ( new OCPropertyField(obj, unityPropertyField.Copy()) ); } } unityPropertyField.Reset(); } if (type != null) { // Debug.Log("We found a type!"); MemberInfo[] memberInfos = type.GetMembers(OCBindingFlags); foreach (MemberInfo info in memberInfos) { if ( (info.MemberType == MemberTypes.Field && !(info as FieldInfo).IsSpecialName ) || (info.MemberType == MemberTypes.Property && !(info as PropertyInfo).IsSpecialName && (info as PropertyInfo).CanRead && (info as PropertyInfo).CanWrite ) ) { // Debug.Log("We found a good member info! :" + info.Name); string publicName = ObjectNames.NicifyVariableName(info.Name); if (ExcludedPropertyFieldPublicNames.Contains(publicName)) { continue; } // Debug.Log("We found a good candidate! :" + info.Name); candidates.Push(new OCPropertyField(obj, info)); } } } while (candidates.Count > 0) { OCPropertyField candidate = candidates.Pop(); OCPropertyField match = allPropertyFields.Find(p => p.PublicName == candidate.PublicName); if (match != null && match != default(OCPropertyField) && (match.UnityPropertyField != null || match.Instance != null)) { match = candidate; } else { allPropertyFields.Insert(0, candidate); } } if (allPropertyFields.Count > 0) { return(allPropertyFields); } else { return(null); } }