public static void PrintObjectInfo(System.Object pObject) { System.Type type = pObject.GetType(); Debug.Log("PrintObjectInfo " + type.Name); //Debug.Log("lenght = " + type.GetMembers().Length); Debug.Log("lenght = " + type.GetFields().Length); System.Reflection.FieldInfo[] fields = type.GetFields(); string tempvalue = ""; foreach (System.Reflection.FieldInfo field in fields) { if (field.FieldType.IsArray) { tempvalue += field.Name + " = ["; Array array = (Array)field.GetValue(pObject); foreach (System.Object obj in array) { tempvalue += obj.ToString() + ","; } tempvalue += " ]\n"; } else { tempvalue += field.Name + " = " + field.GetValue(pObject) + "\n"; //Debug.Log("Name:" + field.Name + " Value:" + field.GetValue(pObject));// field.Name, field.ToString()) } } Debug.Log(tempvalue); }
public static void Invoke(this System.Object obj, string methodName, params object[] args) { var method = obj.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (method != null) { method.Invoke(obj, args); } }
public static MethodInfo GetMethodFromField(SerializedProperty prop, string methodName, out System.Object targetObject) { GetReflectedFieldInfoRecursively(prop, out targetObject); if (targetObject != null) { return(targetObject.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, null, new System.Type[0], null)); } return(null); }
public ChangeStringValueTool(System.Object data, string newValue, string getMethodName, string setMethodName, bool updateTree, bool updatePanel) { this.data = data; this.newValue = newValue; this.updatePanel = updatePanel; this.updateTree = updateTree; set = data.GetType().GetMethod(setMethodName); get = data.GetType().GetMethod(getMethodName); this.getName = getMethodName; this.setName = setMethodName; if (get.ReturnType != typeof(string)) { get = set = null; getName = setName = null; //ReportDialog.GenerateErrorReport(new Exception("Get method must return bool value"), false, // Language.GetText("Error.Title")); } }
public static string getMembersOf(System.Object t) { Type obj = t.GetType(); string log = "MEMBERS OF : " + obj.Name; MemberInfo[] member_info = obj.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (MemberInfo member in member_info) { log += "\nMember :" + member.Name; } return(log); }
public static PropertyField[] GetProperties(System.Object obj) { List <PropertyField> fields = new List <PropertyField>(); PropertyInfo[] infos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo info in infos) { if (!(info.CanRead && info.CanWrite)) { continue; } object[] attributes = info.GetCustomAttributes(true); bool isExposed = false; foreach (object o in attributes) { if (o.GetType() == typeof(ExposePropertyAttribute)) { isExposed = true; break; } } if (!isExposed) { continue; } SerializedPropertyType type = SerializedPropertyType.Integer; if (PropertyField.GetPropertyType(info, out type)) { PropertyField field = new PropertyField(obj, info, type); fields.Add(field); } } return(fields.ToArray()); }
private void OnPostprocessGameObjectWithUserProperties(GameObject go, string[] propNames, System.Object[] values) { Debug.Log("OnPostprocessGameObjectWithUserProperties"); for (int i = 0; i < propNames.Length; i++) { string propName = propNames[i]; System.Object value1 = values[i]; Debug.Log("GO: " + go.name + "--Propname: " + propName + "--value1: " + value1.GetType()); if (value1 is string) { Debug.Log("string: " + (string)value1); } if (value1 is Vector4) { Debug.Log("Vector4: " + (Vector4)value1); } if (value1 is Color) { Debug.Log("Color: " + (Color)value1); } if (value1 is bool) { Debug.Log("bool: " + (bool)value1); } if (value1 is int) { Debug.Log("int: " + (int)value1); } if (value1 is float) { Debug.Log("float: " + (float)value1); } } }
private static bool CheckAssetAlive(System.Object asset) { if (null == asset) { return(false); } if (asset is Object) { Object UnityObject = asset as Object; if (null == UnityObject || !UnityObject) { return(false); } } else { throw new Exception(string.Format("InVaild Asset Type:{0}", asset.GetType())); } return(true); }
private static FieldData[] GetFields(System.Object obj) { List <FieldData> fields = new List <FieldData>(); FieldInfo[] infos = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); foreach (FieldInfo info in infos) { Attribute[] attributes = Attribute.GetCustomAttributes(info, typeof(ShowInInspectorAttribute)); if (!(attributes.Length > 0)) { continue; } FieldData data = new FieldData(obj, info); fields.Add(data); } return(fields.ToArray()); }
private static PropertyData[] GetProperties(System.Object obj) { List <PropertyData> properties = new List <PropertyData>(); PropertyInfo[] typeInfos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); foreach (PropertyInfo info in typeInfos) { Attribute[] attributes = Attribute.GetCustomAttributes(info, typeof(ShowInInspectorAttribute)); if (!(attributes.Length > 0)) { continue; } PropertyData data = new PropertyData(obj, info); properties.Add(data); } return(properties.ToArray()); }
public static string getMethodsOn(System.Object t) { Type obj = t.GetType(); string log = "METHODS FOR : " + obj.Name; MethodInfo[] method_info = obj.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (MethodInfo method in method_info) { string parameters = ""; ParameterInfo[] param_info = method.GetParameters(); if (0 < param_info.Length) { for (int i = 0; i < param_info.Length; i++) { parameters += param_info[i].ParameterType.Name; parameters += (i < (param_info.Length - 1)) ? ", " : ""; } } log += "\nFunction :" + method.Name + "(" + parameters + ")"; } return(log); }
protected void OnComponentGUI(Rect position, SerializedProperty prop, GUIContent label, string fieldName, string[] requiredValues, string defaultObject, bool isPrefab, int rightOffset, System.Type overrideComponentType = null) { if (prop.propertyType != SerializedPropertyType.ObjectReference) { WrongVariableTypeWarning("ComponentSelection", "object references"); return; } int originalIndentLevel = EditorGUI.indentLevel; GUIContent tempLabel = new GUIContent(label); tempLabel.tooltip = "1st Line : GameObject Reference. The GameObject on which to select a Component. This is not Serialized.\n\n2nd Line : The Selected Component. This is your actual Serialized variable.\n\n(GameObject reference is displayed in YELLOW color when the selected component is null, WHITE whe the selected component is on the same GameObject as this script and CYAN when the selected component is on another GameObject.)"; position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), tempLabel); EditorGUI.indentLevel = 0; if (prop.hasMultipleDifferentValues) { position.height = GetPropertyHeight(prop, label); #if !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2 if (fieldInfo.FieldType.IsArray || fieldInfo.FieldType.IsGenericType) { position.x += 15; } #endif EditorGUI.HelpBox(position, "Multi object editing is not supported for references with different values.", MessageType.Warning); return; } bool firstInit = false; if (!targetObjects.ContainsKey(prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath)) { firstInit = true; targetObjects.Add(prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath, null); } // Set back the target game object if the drawed object has been deselected. if (prop.objectReferenceValue != null && (targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] == null || targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] != (prop.objectReferenceValue as Component).gameObject)) { targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] = (prop.objectReferenceValue as Component).gameObject; } if (firstInit && targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] == null) { firstInit = false; try { targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] = string.IsNullOrEmpty(defaultObject) ? (prop.serializedObject.targetObject as MonoBehaviour).gameObject : isPrefab?Resources.Load(defaultObject) as GameObject : GameObject.Find(defaultObject); } catch { targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] = string.IsNullOrEmpty(defaultObject) ? null : isPrefab?Resources.Load(defaultObject) as GameObject : null; } } if ((prop.serializedObject.targetObject as ScriptableObject) != null) { if (targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] != null && !AssetDatabase.Contains(targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath])) { targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] = null; } } position.height /= 2; position.width -= rightOffset; EditorGUI.BeginChangeCheck(); Color tempColor = GUI.color; try{ GUI.color = targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] == (prop.serializedObject.targetObject as MonoBehaviour).gameObject ? objectSelfColor : objectOtherColor; } catch { GUI.color = objectOtherColor; } if (targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] == null) { GUI.color = objectNullColor; } targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] = EditorGUI.ObjectField(position, targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath], typeof(GameObject), true) as GameObject; GUI.color = tempColor; if (targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] == null) { prop.objectReferenceValue = null; return; } position.width += rightOffset; System.Type componentType; List <Component> components; if (fieldInfo.FieldType.IsArray) { componentType = fieldInfo.FieldType.GetElementType(); } else if (fieldInfo.FieldType.IsGenericType) { componentType = fieldInfo.FieldType.GetGenericArguments()[0]; } else { componentType = fieldInfo.FieldType; } if (overrideComponentType != null) { componentType = overrideComponentType; } try { components = targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath].GetComponents(componentType).ToList(); } catch { components = targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath].gameObject.GetComponents(componentType).ToList(); } if (EditorGUI.EndChangeCheck()) { Undo.RecordObjects(prop.serializedObject.targetObjects, "Change Target Object"); prop.objectReferenceValue = null; foreach (Object obj in prop.serializedObject.targetObjects) { EditorUtility.SetDirty(obj); } } if (components.Contains(prop.serializedObject.targetObject as Component)) { components.Remove(prop.serializedObject.targetObject as Component); } List <string> componentsNames = new List <string>(); Dictionary <System.Type, int> componentsNumbers = new Dictionary <System.Type, int>(); Dictionary <string, int> namedMonoBehavioursNumbers = new Dictionary <string, int>(); List <Component> markedForDeletion = new List <Component>(); foreach (Component component in components) { if (component == null) { continue; } if (!componentsNumbers.ContainsKey(component.GetType())) { componentsNumbers.Add(component.GetType(), 1); } if (component is NamedMonoBehaviour) { if (string.IsNullOrEmpty((component as NamedMonoBehaviour).scriptName)) { if (string.IsNullOrEmpty(fieldName)) { componentsNames.Add(component.GetType().ToString().Replace("UnityEngine.", "") + " " + componentsNumbers[component.GetType()]++.ToString()); } else { System.Object val = GetFieldOrPropertyValue(component, fieldName); if (requiredValues != null && requiredValues.Length > 0) { if (requiredValues.Contains(val.ToString().Replace(" (" + val.GetType().ToString() + ")", ""))) { componentsNames.Add(component.GetType().ToString().Replace("UnityEngine.", "") + " " + componentsNumbers[component.GetType()]++.ToString() + " (" + (val == null ? "null" : val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")) + ")"); } else { markedForDeletion.Add(component); } } else { componentsNames.Add(component.GetType().ToString().Replace("UnityEngine.", "") + " " + componentsNumbers[component.GetType()]++.ToString() + " (" + (val == null ? "null" : val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")) + ")"); } } } else { if (namedMonoBehavioursNumbers.ContainsKey((component as NamedMonoBehaviour).scriptName)) { (component as NamedMonoBehaviour).scriptName += (" " + namedMonoBehavioursNumbers[(component as NamedMonoBehaviour).scriptName]++); } else { namedMonoBehavioursNumbers.Add((component as NamedMonoBehaviour).scriptName, 2); } if (string.IsNullOrEmpty(fieldName)) { componentsNames.Add((component as NamedMonoBehaviour).scriptName); } else { System.Object val = GetFieldOrPropertyValue(component, fieldName); if (requiredValues != null && requiredValues.Length > 0) { if (requiredValues.Contains(val.ToString().Replace(" (" + val.GetType().ToString() + ")", ""))) { componentsNames.Add((component as NamedMonoBehaviour).scriptName + " (" + (val == null ? "null" : val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")) + ")"); } else { markedForDeletion.Add(component); } } else { componentsNames.Add((component as NamedMonoBehaviour).scriptName + " (" + (val == null ? "null" : val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")) + ")"); } } } } else { if (string.IsNullOrEmpty(fieldName)) { componentsNames.Add(component.GetType().ToString().Replace("UnityEngine.", "") + " " + componentsNumbers[component.GetType()]++.ToString()); } else { System.Object val = GetFieldOrPropertyValue(component, fieldName); if (requiredValues != null && requiredValues.Length > 0) { if (requiredValues.Contains(val.ToString().Replace(" (" + val.GetType().ToString() + ")", ""))) { componentsNames.Add(component.GetType().ToString().Replace("UnityEngine.", "") + " " + componentsNumbers[component.GetType()]++.ToString() + " (" + (val == null ? "null" : val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")) + ")"); } else { markedForDeletion.Add(component); } } else { componentsNames.Add(component.GetType().ToString().Replace("UnityEngine.", "") + " " + componentsNumbers[component.GetType()]++.ToString() + " (" + (val == null ? "null" : val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")) + ")"); } } } } foreach (Component component in markedForDeletion) { components.Remove(component); } if (components.Count == 0) { targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] = null; prop.objectReferenceValue = null; return; } components.Insert(0, null); componentsNames.Insert(0, "None (" + componentType.ToString().Replace("UnityEngine.", "") + ")"); position.y += position.height; int index = 0; try { index = components.IndexOf(prop.objectReferenceValue as Component); } catch { prop.objectReferenceValue = null; } try { if (index != 0 && typeof(NamedMonoBehaviour).IsAssignableFrom(components[index].GetType())) { GUI.backgroundColor = (components[index] as NamedMonoBehaviour).scriptNameColor; } prop.objectReferenceValue = components[EditorGUI.Popup(position, index, componentsNames.ToArray())]; GUI.backgroundColor = Color.white; } catch { } EditorGUI.indentLevel = originalIndentLevel; }
/// <summary> /// Writes an Object to a Configuration Node, using it's parser targets /// </summary> /// <returns></returns> public static ConfigNode WriteObjectToConfigNode <T>(string name, ref ConfigNode node, T target) { // Start ConfigNode config = node.AddNode(name); // Crawl it's member infos foreach (MemberInfo member in target.GetType().GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) { // Get the first parser target ParserTarget[] targets = member.GetCustomAttributes(typeof(ParserTarget), false) as ParserTarget[]; if (targets.Length == 0) { continue; } // Is this a node or a value? RequireConfigType[] configTypes = (member.MemberType == MemberTypes.Field ? (member as FieldInfo).FieldType : (member as PropertyInfo).PropertyType).GetCustomAttributes(typeof(RequireConfigType), false) as RequireConfigType[]; // Write System.Object memberValue = member.GetValue(target); Type memberType = member.GetMemberType(); if (memberValue == null) { continue; } // Type ConfigType configType = configTypes.Length == 1 ? configTypes[0].Type : memberType.Name.StartsWith("MapSOParser_") || memberType == typeof(string) ? ConfigType.Value : ConfigType.Node; // Convert if (memberType != typeof(string) && (configType == ConfigType.Value || memberType == typeof(FloatCurveParser))) { memberValue = memberType == typeof(PhysicsMaterialParser) ? memberType.GetProperty("Value").GetValue(memberValue, null) : memberType == typeof(FloatCurveParser) ? memberType.GetProperty("Value").GetValue(memberValue, null) : memberType.GetProperty("Value").GetValue(memberValue); if (memberValue == null || memberType == typeof(StockMaterialParser)) { continue; } if (memberValue.GetType().GetInterface("IEnumerable") != null && memberType != typeof(string)) { memberValue = String.Join(memberType == typeof(StringCollectionParser) ? "," : " ", (memberValue as IEnumerable).Cast <System.Object>().Select(o => o.ToString()).ToArray()); } } // Format Unity types if (writeableTypes.Contains(memberType)) { if (memberValue is Vector2) { memberValue = ConfigNode.WriteVector((Vector2)memberValue); } if (memberValue is Vector3) { memberValue = ConfigNode.WriteVector((Vector3)memberValue); } if (memberValue is Vector3d) { memberValue = ConfigNode.WriteVector((Vector3d)memberValue); } if (memberValue is Vector4) { memberValue = ConfigNode.WriteVector((Vector4)memberValue); } if (memberValue is Quaternion) { memberValue = ConfigNode.WriteQuaternion((Quaternion)memberValue); } if (memberValue is QuaternionD) { memberValue = ConfigNode.WriteQuaternion((QuaternionD)memberValue); } if (memberValue is Color) { memberValue = ConfigNode.WriteColor((Color)memberValue); } } // Texture Type[] textureTypes = { typeof(Mesh), typeof(Texture2D), typeof(Texture), typeof(MapSO), typeof(CBAttributeMapSO) }; if (textureTypes.Contains(memberValue.GetType()) || textureTypes.Contains(memberValue.GetType().BaseType)) { memberValue = Format(memberValue as Object); } // Write if (configType == ConfigType.Value && memberValue.GetType() != typeof(FloatCurve) && memberValue.GetType() != typeof(AnimationCurve)) { config.AddValue(targets[0].FieldName, memberValue); } else if (memberValue.GetType() == typeof(FloatCurve)) { (memberValue as FloatCurve).Save(config.AddNode(targets[0].FieldName)); } else if (memberValue is AnimationCurve) { new FloatCurve((memberValue as AnimationCurve).keys).Save(config.AddNode(targets[0].FieldName)); } } return(config); }