Esempio n. 1
0
	public void DrawFieldInInspector(OCPropertyField propertyField, GUIContent label, GUILayoutOption[] emptyOptions, OCFloatSliderAttribute floatSlider, OCIntSliderAttribute intSlider)
	{
		if(floatSlider != null)
		{
//			var currentTarget = _instance;
//			MemberInfo[] memberInfo = currentTarget.GetType().GetMember(propertyField.PrivateName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
			//Tests if the field is not a float, if so it will display an error
//			if
//			(		memberInfo == null
//			|| ((memberInfo[0] as FieldInfo) == null && (memberInfo[0] as PropertyInfo) == null)
//			|| ((memberInfo[0] as FieldInfo).FieldType != typeof(float) && (memberInfo[0] as PropertyInfo).PropertyType != typeof(float))
//			)
//			{
//				Debug.LogError("The '[FloatSliderInInspector(" + floatSlider.MinValue + " ," + floatSlider.MaxValue + ")]' failed. FloatSliderInInspector does not work with the type '" + memberInfo[0].MemberType + "', it only works with float. The attribute is attached to the field '" + propertyField.Name + "' in '" + _instance + "'.");
//				return;
//			}
			propertyField.SetValue(EditorGUILayout.Slider(label, (float)propertyField.GetValue(), floatSlider.MinValue, floatSlider.MaxValue));

		}
		else
		if(intSlider != null)
		{
//			var currentTarget = _instance;
//			MemberInfo[] memberInfo = currentTarget.GetType().GetMember(propertyField.PrivateName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
			//Tests if the field is not a int, if so it will display an error
//			if
//			(		memberInfo == null
//			|| ((memberInfo[0] as FieldInfo) == null && (memberInfo[0] as PropertyInfo) == null)
//			|| ((memberInfo[0] as FieldInfo).FieldType != typeof(int) && (memberInfo[0] as PropertyInfo).PropertyType != typeof(int))
//			)
//			{
//				Debug.LogError("The '[IntSliderInInspector(" + intSlider.MinValue + " ," + intSlider.MaxValue + ")]' failed. IntSliderInInspector does not work with the type '" + memberInfo[0].MemberType + "', it only works with int. The attribute is attached to the field '" + propertyField.Name + "' in '" + _instance + "'.");
//				return;
//			}
			propertyField.SetValue(EditorGUILayout.IntSlider(label, (int)propertyField.GetValue(), intSlider.MinValue, intSlider.MaxValue));
		}
		else
		if(propertyField.UnityPropertyField != null && propertyField.UnityPropertyField.editable)
		{
			// VVVV DRAWS THE STANDARD FIELD  VVVV
			EditorGUILayout.PropertyField(propertyField.UnityPropertyField, label, true);
			// ^^^^^  DRAWS THE STANDARD FIELD  ^^^^^
		}
		else
		{
			if(propertyField.UnityPropertyField != null)
			{
				switch(propertyField.UnityType)
				{
				case SerializedPropertyType.Integer:
					propertyField.SetValue(EditorGUILayout.IntField(label, (int)propertyField.GetValue(), emptyOptions));
					break;
		
				case SerializedPropertyType.Float:
					propertyField.SetValue(EditorGUILayout.FloatField(label, (float)propertyField.GetValue(), emptyOptions));
					break;

				case SerializedPropertyType.Boolean:
					propertyField.SetValue(EditorGUILayout.Toggle(label, (bool)propertyField.GetValue(), emptyOptions));
					break;

				case SerializedPropertyType.String:
					propertyField.SetValue(EditorGUILayout.TextField(label, (string)propertyField.GetValue(), emptyOptions));
					break;
		
				case SerializedPropertyType.Vector2:
					propertyField.SetValue(EditorGUILayout.Vector2Field(propertyField.PublicName, (Vector2)propertyField.GetValue(), emptyOptions));
					break;
		
				case SerializedPropertyType.Vector3:
					propertyField.SetValue(EditorGUILayout.Vector3Field(propertyField.PublicName, (Vector3)propertyField.GetValue(), emptyOptions));
					break;
		
				case SerializedPropertyType.Enum:
					propertyField.SetValue(EditorGUILayout.EnumPopup(label, (Enum)propertyField.GetValue(), emptyOptions));
					break;
		
				case SerializedPropertyType.Generic:
				case SerializedPropertyType.ObjectReference:

					if(propertyField.PublicName == "Animation")
						Debug.Log("Drawing Animation: " + propertyField.CSType);

					if(propertyField.CSType.IsSerializable)
					{
						List<OCPropertyField> nestedPropertiesAndFields = new List<OCPropertyField>();

						nestedPropertiesAndFields = OCPropertyField.GetAllPropertiesAndFields
						( propertyField.Instance
						, propertyField.UnityPropertyField
						, OCExposePropertyFieldsAttribute.OCExposure.PropertiesAndFields
						);

						Debug.Log("In OCEditor.DrawFieldInInspector, propertyField type is Serializable.");

						Debug.Log("In OCEditor.DrawFieldInInspector, nested Properties and Fields: " + nestedPropertiesAndFields.Select(p => p.PublicName).Aggregate((a, b) => a + ", " + b));

						DrawSerializedProperties(nestedPropertiesAndFields);

					}
					break;
		
				default:
		
					break;

				}
			}
			else if(propertyField.MemberInfo != null)
			{
				//Debug.Log(propertyField.PublicName + ", " + propertyField.Instance);
				switch(Type.GetTypeCode(propertyField.CSType))
				{
					case TypeCode.Boolean:
						propertyField.SetValue(EditorGUILayout.Toggle(label, (bool)propertyField.GetValue(), emptyOptions));
						break;
			
					case TypeCode.Decimal:
					case TypeCode.Double:
					case TypeCode.Single:
						propertyField.SetValue(EditorGUILayout.FloatField(label, (float)propertyField.GetValue(), emptyOptions));
						break;
			
					case TypeCode.Int16:
					case TypeCode.Int32:
					case TypeCode.Int64:
					case TypeCode.UInt16:
					case TypeCode.UInt32:
					case TypeCode.UInt64:
						propertyField.SetValue(EditorGUILayout.IntField(label, (int)propertyField.GetValue(), emptyOptions));
						break;
			
					case TypeCode.String:
						propertyField.SetValue(EditorGUILayout.TextField(label, (string)propertyField.GetValue(), emptyOptions));
						break;

					case TypeCode.Object:
						//Debug.Log("Drawing: " + propertyField.CSType + ", " + propertyField.CSType.IsSerializable);

						if(propertyField.CSType.IsSerializable && (propertyField.Instance != null || (propertyField.UnityPropertyField != null && propertyField.UnityPropertyField.objectReferenceValue != null)))
						{
							//	DrawDefaultInspector();
							List<OCPropertyField> nestedPropertiesAndFields = new List<OCPropertyField>();

							nestedPropertiesAndFields = OCPropertyField.GetAllPropertiesAndFields
							( propertyField.GetValue()
							, propertyField.UnityPropertyField
							, OCExposePropertyFieldsAttribute.OCExposure.PropertiesAndFields
							);

							//EditorGUILayout.Separator();

							//GUILayout.Space(-14);



							if(_foldedState.ContainsKey(propertyField.PublicName))
							{
								EditorGUI.indentLevel--;
								_foldedState[propertyField.PublicName] = EditorGUILayout.Foldout(_foldedState[propertyField.PublicName], label);
								EditorGUI.indentLevel++;
							}
							else
							{
								_foldedState.Add(propertyField.PublicName, true);
							}

							if(_foldedState[propertyField.PublicName])
							{

								//EditorGUILayout.BeginVertical();
								//GUILayout.Space(15);
								//EditorGUILayout.BeginHorizontal();
								//GUILayout.Space(-25);
//								Debug.Log("In OCEditor.DrawFieldInInspector, propertyField type is Serializable.");
//
//								Debug.Log("In OCEditor.DrawFieldInInspector, nested Properties and Fields: " + nestedPropertiesAndFields.Select(p => p.Instance != null ? p.Instance.ToString() : "Null").Aggregate((a, b) => a + ", " + b));

								EditorGUI.indentLevel++;

								DrawSerializedProperties(nestedPropertiesAndFields);

								EditorGUI.indentLevel--;

								//EditorGUILayout.EndHorizontal();
								//EditorGUILayout.EndVertical();

							}
						}
						else if(propertyField.CSType.IsSubclassOf(typeof(UnityEngine.Object)))
						{
							propertyField.SetValue(EditorGUILayout.ObjectField(label, (UnityEngine.Object)propertyField.GetValue(), propertyField.CSType, true, emptyOptions));
						}
						else if(propertyField.CSType.IsEnum)
						{
							propertyField.SetValue(EditorGUILayout.EnumPopup(label, (Enum)propertyField.GetValue(), emptyOptions));
						}
						break;

					default:
						break;
				}
				if(propertyField.CSType.IsEnum)
					propertyField.SetValue(EditorGUILayout.EnumPopup(label, (Enum)propertyField.GetValue(), emptyOptions));
			}
		}
	}
Esempio n. 2
0
	public void DrawSerializedProperties(List< OCPropertyField > allPropertiesAndFields)
	{
		GUIContent label = new GUIContent();
		GUILayoutOption[] emptyOptions = new GUILayoutOption[0];

		//EditorGUILayout.BeginVertical(emptyOptions);

		//Loops through all visible fields
		foreach(OCPropertyField propertyField in allPropertiesAndFields)
		{
			//Debug.Log(propertyField.PublicName + ", " + propertyField.UnityType);

			//if(propertyField.GetValue() == null)
			//	continue;

			//EditorGUILayout.BeginHorizontal(emptyOptions);

			//Finds the bool Condition, enum Condition and tooltip if they exist (They are null otherwise).
			OCBoolPropertyToggleAttribute boolCondition = propertyField.GetAttribute<OCBoolPropertyToggleAttribute>();
			OCEnumPropertyToggleAttribute enumCondition = propertyField.GetAttribute<OCEnumPropertyToggleAttribute>();
			OCDrawMethodAttribute drawMethod = propertyField.GetAttribute<OCDrawMethodAttribute>();
			OCTooltipAttribute tooltip = propertyField.GetAttribute<OCTooltipAttribute>();
			OCFloatSliderAttribute floatSlider = propertyField.GetAttribute<OCFloatSliderAttribute>();
			OCIntSliderAttribute intSlider = propertyField.GetAttribute<OCIntSliderAttribute>();

			//Evaluates the enum and bool conditions
			bool allowedVisibleForBoolCondition = propertyField.IsVisibleForBoolCondition(boolCondition);
			bool allowedVisibleForEnumCondition = propertyField.IsVisibleForEnumCondition(enumCondition);

			//Tests is the field is visible
			if(allowedVisibleForBoolCondition && allowedVisibleForEnumCondition && drawMethod == null)
			{

				label.text = propertyField.PublicName;

				//Sets the tooltip if avaiable
				if(tooltip != null)
				{
					label.tooltip = tooltip.Description;
				}

				DrawFieldInInspector(propertyField, label, emptyOptions, floatSlider, intSlider);

			}
			else
			if(drawMethod != null)
			{
				// If the user wants to draw the field himself.
				MethodInfo drawMethodInfo = this.GetType().GetMethod(drawMethod.DrawMethod);
				if(drawMethodInfo == null)
				{
					Debug.LogError("The '[CustomDrawMethod(" + drawMethod.DrawMethod + "" + drawMethod.ParametersToString() + ")]' failed. Could not find the method '" + drawMethod.DrawMethod + "' in the " + this.ToString() + ". The attribute is attached to the field '" + propertyField.PublicName + "' in '" + propertyField.UnityPropertyField.serializedObject.targetObject + "'.");
					continue;
				}
				ParameterInfo[] parametersInfo = drawMethodInfo.GetParameters();
				if(parametersInfo.Length != (drawMethod.Parameters as object[]).Length)
				{
					Debug.LogError("The '[CustomDrawMethod(" + drawMethod.DrawMethod + "" + drawMethod.ParametersToString() + ")]' failed. The number of parameters in the attribute, did not match the number of parameters in the actual method. The attribute is attached to the field '" + propertyField.PublicName + "' in '" + propertyField.UnityPropertyField.serializedObject.targetObject + "'.");
					continue;
				}

				bool _error = false;
				for(int i = 0; i < parametersInfo.Length; i++)
				{
					//Makes sure the parameter of the actual method is equal to the given parameters
					if(!Type.Equals(parametersInfo[i].ParameterType, drawMethod.Parameters[i].GetType()))
					{
						_error = true;
						Debug.LogError("The '[CustomDrawMethod(" + drawMethod.DrawMethod + "" + drawMethod.ParametersToString() + ")]' failed. The parameter type ('" + drawMethod.Parameters[i].GetType() + "') in the attribute, did not match the the parameter type ('" + parametersInfo[i].ParameterType + "') of the actual method, parameter index: '" + i + "'. The attribute is attached to the field '" + propertyField.PublicName + "' in '" + propertyField.UnityPropertyField.serializedObject.targetObject + "'.");
						continue;
					}
				}
				if(_error)
				{
					continue;
				}

				// VVVVV Calls the users own method  VVVVV
				drawMethodInfo.Invoke(this, drawMethod.Parameters);
				// ^^^^^ Calls the users own method ^^^^^
			}
			else
			{
				//Debug.Log("In OCEditor.DrawSerializedProperties, nothing to draw! " + allowedVisibleForBoolCondition + ", " + allowedVisibleForEnumCondition + ", " + drawMethod);
			}

			//EditorGUILayout.EndHorizontal();

		}

		//EditorGUILayout.EndVertical();
	}