コード例 #1
0
        public bool DrawField(string label, Func <object> getter, Action <object> setter, Type type)
        {
            //Primitive?
            var primitiveDrawer = MessagePackPrimitivePropertyDrawer.GetDrawer(type);

            if (primitiveDrawer != null)
            {
                return(primitiveDrawer.DrawField(label, getter, setter, type));
            }

            //Array?
            if (type.IsArray)
            {
                if (_arrayDrawer == null)
                {
                    _arrayDrawer = new ArrayMessagePackPropertyDrawer(_useFoldout);
                }

                return(_arrayDrawer.DrawField(label, getter, setter, type));
            }

            //[MessagePackObject]でもないし[Union]でもないなら処理不可
            if (!MessagePackTypeCache.Instance.IsCached(type))
            {
                return(false);
            }

            bool edit = false;

            //折り畳み処理
            if (_useFoldout)
            {
                _foldoutOpen = EditorGUILayout.Foldout(_foldoutOpen, label);
                if (!_foldoutOpen)
                {
                    return(false);
                }
            }

            var concreteType = type;

            //すでにinstanceがあるか、取得しておく
            var obj = getter();

            //Union対象かを一度だけチェックする
            if (!_unionChecked)
            {
                _unions = MessagePackTypeCache.Instance.GetUnions(type);
                if (_unions != null && _unions.Length > 0)
                {
                    _unionPopup = _unions.Select(x => new GUIContent(x.SubType.FullName)).ToArray();
                }
                _unionChecked = true;
            }

            if (_unionPopup != null)
            {
                //初回だけ、objに合わせたindexに調整する
                if (_unionSelectedIndex == -1)
                {
                    if (obj != null)
                    {
                        _unionSelectedIndex = Array.FindIndex(_unions, union => union.SubType == obj.GetType());
                    }

                    if (_unionSelectedIndex == -1)
                    {
                        _unionSelectedIndex = 0;
                    }
                }

                var newIndex = EditorGUILayout.Popup(UnionLabel, _unionSelectedIndex, _unionPopup);
                if (newIndex != _unionSelectedIndex)
                {
                    _unionSelectedIndex = newIndex;
                    //Unionの選択が変わったら再生成するためにobjをnullにしておく
                    obj = null;
                    setter(null);
                    edit = true;
                }
                concreteType = _unions[_unionSelectedIndex].SubType;
            }

            //デフォルトで作成
            if (obj == null)
            {
                if (_useFoldout && GUILayout.Button("new", GUILayout.Width(100f)))
                {
                    obj = Activator.CreateInstance(concreteType);
                    setter(obj);
                    edit = true;
                }
            }
            else
            {
                if (_useFoldout && GUILayout.Button("set null", GUILayout.Width(100f)))
                {
                    setter(null);
                    obj  = null;
                    edit = true;
                }
            }

            if (obj == null)
            {
                return(edit);
            }

            var props = MessagePackTypeCache.Instance.GetProperties(concreteType);

            foreach (var prop in props)
            {
                DefaultMessagePackPropertyDrawer drawer = null;
                if (!_childDrawers.TryGetValue(prop.Name, out drawer))
                {
                    drawer = new DefaultMessagePackPropertyDrawer(); //子はFoldout使う
                    _childDrawers.Add(prop.Name, drawer);
                }

                edit |= drawer.DrawField(prop.Name, () => prop.GetValue(obj, null), (newValue) => prop.SetValue(obj, newValue, null), prop.PropertyType);
            }

            return(edit);
        }
コード例 #2
0
 private void OnEnable()
 {
     _drawer = new DefaultMessagePackPropertyDrawer(useFoldout: false);
 }