Exemplo n.º 1
0
 static void SetValue(ComponentEcs component, List <FieldInfo> fields, FieldInfo field, object newValue)
 {
     if (newValue != field.GetValue(component))
     {
         field.SetValue(component, newValue);
         component._InternalOnValueChange((ushort)fields.IndexOf(field));
     }
 }
Exemplo n.º 2
0
        void DrawField(ComponentEcs component, List <FieldInfo> fields, FieldInfo field)
        {
            var fieldValue = field.GetValue(component);
            var fieldType  = field.FieldType;

            var strVal = fieldValue != null?string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}", fieldValue) : "null";

            if (strVal.Length > MaxFieldToStringLength)
            {
                strVal = strVal.Substring(0, MaxFieldToStringLength);
            }

            //EditorGUILayout.LabelField(field.Name, GUILayout.MaxWidth(EditorGUIUtility.labelWidth - 16));

            object newValue    = null;
            var    maySetValue = true;

            EditorGUI.BeginChangeCheck();

            if (fieldType == typeof(bool))
            {
                newValue = EditorGUILayout.Toggle(field.Name, (bool)fieldValue);
            }
            else if (fieldType == typeof(int))
            {
                newValue = EditorGUILayout.IntField(field.Name, (int)fieldValue);
            }
            else if (fieldType == typeof(float))
            {
                newValue = EditorGUILayout.FloatField(field.Name, (float)fieldValue);
            }
            else if (fieldType == typeof(string))
            {
                newValue = EditorGUILayout.DelayedTextField(field.Name, (string)fieldValue);
            }
            else if (fieldType == typeof(Vector3))
            {
                newValue = EditorGUILayout.Vector3Field(field.Name, (Vector3)fieldValue);
            }

            else if (fieldType == typeof(Vector2))
            {
                newValue = EditorGUILayout.Vector2Field(field.Name, (Vector2)fieldValue);
            }
            else if (fieldType == typeof(Vector2Int))
            {
                newValue = EditorGUILayout.Vector2IntField(field.Name, (Vector2Int)fieldValue);
            }
            else if (fieldType == typeof(Color))
            {
                newValue = EditorGUILayout.ColorField(field.Name, (Color)fieldValue);
            }
            else if (fieldType.IsEnum)
            {
                newValue = (Enum)EditorGUILayout.EnumPopup(field.Name, (Enum)fieldValue);
            }
            else if (fieldType == typeof(UnityEngine.Object) || fieldType.IsSubclassOf(typeof(UnityEngine.Object)) || (fieldType.IsInterface && fieldValue is MonoBehaviour))
            {
                newValue = EditorGUILayout.ObjectField(field.Name, (UnityEngine.Object)fieldValue, fieldType, true);
            }
            else if (fieldType.IsSubclassOf(typeof(Entity)) && fieldValue != null)
            {
                var p = observer.transform.parent;

                var entity = fieldValue as Entity;
                if (entity.DebugEntityObserver != null)
                {
                    newValue    = EditorGUILayout.ObjectField(field.Name, entity.DebugEntityObserver.gameObject, fieldType, true);
                    maySetValue = false;
                }
            }
            else if (fieldType.GetInterfaces().Contains(typeof(IEnumerable)))
            {
                maySetValue = false;
                int   itemsCount = 0;
                float minHeight  = EditorGUIUtility.singleLineHeight;



                foreach (var item in (IEnumerable)fieldValue)
                {
                    itemsCount++;
                }

                GUILayout.BeginScrollView(EditorGUILayout.GetControlRect().position, GUILayout.Height(itemsCount * minHeight + minHeight));
                GUILayout.BeginVertical();
                foreach (var item in (IEnumerable)fieldValue)
                {
                    if (item.GetType().IsSubclassOf(typeof(Entity)))
                    {
                        var p = observer.transform.parent;

                        var entity = item as Entity;
                        newValue = EditorGUILayout.ObjectField("Entity_" + entity.ID, entity.DebugEntityObserver.gameObject, fieldType, true);
                    }
                    else
                    {
                        EditorGUILayout.SelectableLabel(item.ToString(), (GUILayout.MaxHeight(EditorGUIUtility.singleLineHeight)));
                    }
                }
                GUILayout.EndVertical();
                GUILayout.EndScrollView();
            }
            else
            {
                EditorGUILayout.SelectableLabel(strVal, GUILayout.MaxHeight(EditorGUIUtility.singleLineHeight));
            }

            if (EditorGUI.EndChangeCheck())
            {
                if (maySetValue)
                {
                    SetValue(component, fields, field, newValue);
                }
            }
        }