コード例 #1
0
        protected virtual void DrawImpl(Rect position, GUIContent label)
        {
            var rowHeight = GUI.skin.font.lineHeight;

            rowHeight += GUI.skin.label.margin.vertical + GUI.skin.label.padding.vertical;
            var pos = new GUILayoutPosition(position, rowHeight);

            if (Target.CurrentType == SerializedTarget.Type.SerializedProperty)
            {
                using (var scope = new EditorGUI.ChangeCheckScope())
                {
                    var foldoutPos = pos.GetSplitPos(3f, 0);
                    _foldout = EditorGUI.Foldout(foldoutPos, _foldout, label);

                    var objFieldPos = pos.GetSplitPos(3f, 1, 2);
                    var newObj      = EditorGUI.ObjectField(objFieldPos, Target.ObjectReference, typeof(TDictionary), false);
                    if (scope.changed)
                    {
                        Target.ObjectReference = newObj;
                    }
                }
                if (!_foldout || Target.ObjectReference == null)
                {
                    return;
                }
            }
            else
            {
                EditorGUI.LabelField(pos.Pos, label);
            }

            var assetInstance = Dictionary;

            if (assetInstance == null)
            {
                pos.IncrementRow();
                var labelPos = pos.Pos;
                EditorGUI.LabelField(labelPos, "Don't Found Reference...");
                return;
            }

            var SO = new SerializedObject(assetInstance);

            var(doApply, nextPos) = OnDrawElementsBefore(pos, SO, rowHeight);
            pos = nextPos;
            if (doApply)
            {
                return;
            }

            using (var indent1 = new EditorGUI.IndentLevelScope())
            {
                var indentPos = pos.Indent(true);
                DrawElements(indentPos, SO, rowHeight);
            }
        }
コード例 #2
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var divideCount = 6;
            var pos         = new GUILayoutPosition(position);
            var labelPos    = pos.GetSplitPos(divideCount, 0, 2);

            EditorGUI.LabelField(labelPos, label);

            var popUpPos            = pos.GetSplitPos(divideCount, 2, 4);
            var selectingSceneIndex = FindIndex(property.stringValue);
            var index = EditorGUI.Popup(popUpPos, selectingSceneIndex, ScenePathList);

            if (selectingSceneIndex != index)
            {
                property.stringValue = index >= 0
                    ? EditorBuildSettings.scenes[index].path
                    : "";
            }
        }
コード例 #3
0
        /// <summary>
        /// Targetと引数SOが参照しているものは同じですが、場合によってTargetがポインタ型の場合があるためプロパティを変更したい場合はSOの方を使用してください。
        /// </summary>
        /// <param name="SO"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        bool DrawTypePopUp(SerializedObject SO, GUILayoutPosition position, bool enableUpdate)
        {
            bool doApply   = false;
            var  typeCache = GetOrCreateTypeCahce(SO, out var isNew);

            if (isNew)
            {
                doApply = true;
            }

            var newAsmIndex  = EditorGUI.Popup(position.GetSplitPos(layoutPosDivideCount, 1), typeCache.AssemblyIndex, typeCache.AssemblyNameList);
            var newTypeIndex = EditorGUI.Popup(position.GetSplitPos(layoutPosDivideCount, 2), typeCache.TypeIndex, typeCache.TypeNameList);

            doApply |= newAsmIndex != typeCache.AssemblyIndex;
            doApply |= newTypeIndex != typeCache.TypeIndex;
            if (doApply && enableUpdate)
            {
                typeCache.AssemblyIndex = newAsmIndex;
                typeCache.TypeIndex     = newTypeIndex;
            }
            return(doApply);
        }
コード例 #4
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var textTemplateEngine = GetTargetTextTemplateEngine(property);
            var keywordProp        = property.FindPropertyRelative("keyword");
            var valueProp          = property.FindPropertyRelative("value");

            int selectedKeywordIndex = textTemplateEngine.Keywords
                                       .Zip(Enumerable.Range(0, textTemplateEngine.Keywords.Count()), (_k, _i) => (_k, _i))
                                       .Where(_t => _t._k.key == keywordProp.stringValue)
                                       .Select(_t => _t._i).FirstOrDefault();

            int selectedValueIndex = 0;

            if (textTemplateEngine.Keywords.Any())
            {
                selectedValueIndex = textTemplateEngine.Keywords
                                     .ElementAt(selectedKeywordIndex)
                                     .values.FindIndex(_i => _i == valueProp.stringValue);
            }

            var pos = new GUILayoutPosition(position);

            pos.Indent(false, 0.3f);
            var  posWidth   = 6;
            var  popupWidth = 2;
            bool isUpdated  = false;

            using (var horizontalScope = new GUILayout.HorizontalScope())
            {
                EditorGUI.LabelField(pos.GetSplitPos(posWidth, 0, 1), "Key");

                var keywordList = textTemplateEngine.Keywords.Select(_k => _k.key);
                if (!keywordList.Any())
                {
                    keywordList = Enumerable.Repeat("", 1);
                }

                selectedKeywordIndex = Mathf.Clamp(selectedKeywordIndex, 0, keywordList.Count() - 1);
                var newSelectedKeywordIndex = EditorGUI.Popup(pos.GetSplitPos(posWidth, 1, popupWidth), selectedKeywordIndex, keywordList.ToArray());

                if (newSelectedKeywordIndex != selectedKeywordIndex)
                {
                    isUpdated            = true;
                    selectedKeywordIndex = newSelectedKeywordIndex;
                    selectedValueIndex   = 0;
                }

                EditorGUI.LabelField(pos.GetSplitPos(posWidth, 3, 1), "Val");
                var valueList = (selectedKeywordIndex < textTemplateEngine.Keywords.Count())
                    ? textTemplateEngine.Keywords.ElementAt(selectedKeywordIndex).values.AsEnumerable()
                    : Enumerable.Repeat("", 1);
                var newSelectedValueIndex = EditorGUI.Popup(pos.GetSplitPos(posWidth, 4, popupWidth), selectedValueIndex, valueList.ToArray());
                if (newSelectedValueIndex != selectedValueIndex)
                {
                    isUpdated          = true;
                    selectedValueIndex = newSelectedValueIndex;
                }
            }

            if (isUpdated)
            {
                var keyword = textTemplateEngine.Keywords.ElementAt(selectedKeywordIndex);
                property.FindPropertyRelative("keyword").stringValue = keyword.key;
                property.FindPropertyRelative("value").stringValue   = keyword.values[selectedValueIndex];
            }
        }
コード例 #5
0
 protected virtual (bool doApply, GUILayoutPosition nextPos) OnDrawElementsBefore(GUILayoutPosition position, SerializedObject SO, float rowHeight)
 {
     return(false, position);
 }
コード例 #6
0
        /// <summary>
        /// Targetと引数SOが参照しているものは同じだが、場合によってTargetがポインタ型の場合があるためプロパティを変更したい場合はSOの方を使用してください。
        /// </summary>
        /// <param name="position"></param>
        /// <param name="SO"></param>
        /// <param name="rowHeight"></param>
        /// <returns></returns>
        protected override (bool doApply, GUILayoutPosition nextPos) OnDrawElementsBefore(GUILayoutPosition position, SerializedObject SO, float rowHeight)
        {
            position.IncrementRow();
            var        labelPos = position.GetSplitPos(layoutPosDivideCount, 0);
            RectOffset offset   = new RectOffset((int)(labelPos.width * -0.1f), 0, 0, 0);

            labelPos = offset.Add(labelPos);
            EditorGUI.LabelField(labelPos, "Used Type");

            bool doApply          = false;
            bool enableUpdateType = true;
            //UsedUnityObjectAttributeが指定されていたら型は固定し、そうでなければ選択できるようにする
            var instance = SO.targetObject as TDictionary;

            if (Target.CurrentType == SerializedTarget.Type.SerializedProperty &&
                UsedTypeAttribute.SetTypeFromFieldInfo(Target.SerializedProperty.GetFieldInfo(), instance))
            {
                var typeNameProp = SO.FindProperty("_typeName");
                Assert.IsNotNull(typeNameProp, $"{Target.SerializedProperty.propertyPath}");
                if (typeNameProp.stringValue != instance.HasType.FullName)
                {
                    typeNameProp.stringValue = instance.HasType.FullName;
                    var typeCache = GetOrCreateTypeCahce(SO, out bool _);
                    typeCache.CurrentType = instance.HasType;
                    doApply = true;
                }
                enableUpdateType = false;
            }

            doApply |= DrawTypePopUp(SO, position, enableUpdateType);

            if (doApply)
            {
                var typeNameProp = SO.FindProperty("_typeName");
                if (typeNameProp.stringValue != _typeCache.CurrentType.FullName)
                {
                    typeNameProp.stringValue = _typeCache.CurrentType.FullName;
                    SO.FindProperty("_values").ClearArray();
                    SO.ApplyModifiedProperties();
                }

                var inst = SO.targetObject as TDictionary;
                inst.Refresh();
            }

            return(doApply, position);
        }
コード例 #7
0
        /// <summary>
        /// 要素の配列を表示する
        /// </summary>
        /// <param name="position"></param>
        /// <param name="SO"></param>
        /// <param name="rowHeight"></param>
        void DrawElements(GUILayoutPosition position, SerializedObject SO, float rowHeight)
        {
            var valuesProp = SO.FindProperty("_values");

            if (valuesProp == null)
            {
                return;
            }

            bool doChanged    = false;
            int  newArraySize = -1;

            using (var scope = new EditorGUI.ChangeCheckScope())
            {
                position.IncrementRow();
                newArraySize = Mathf.Max(0, EditorGUI.IntField(position.Pos, "Size", valuesProp.arraySize));
                if (scope.changed)
                {
                    doChanged |= true;
                }
            }

            var nameHash = new HashSet <string>();
            var endIndex = Mathf.Min(valuesProp.arraySize, (_page + 1) * ELEMENT_PER_PAGE);

            for (var i = _page * ELEMENT_PER_PAGE; i < endIndex; ++i)
            {
                var element = valuesProp.GetArrayElementAtIndex(i);
                position.IncrementRow();

                // 使用する型が指定されていたらそれをKeyValueに渡す
                CustomGUIContent label = new CustomGUIContent();
                if (Target.CurrentType == SerializedTarget.Type.SerializedProperty)
                {
                    var usedTypeAttr = Target.SerializedProperty.GetFieldAttributes <UsedTypeAttribute>().FirstOrDefault();
                    if (usedTypeAttr != null)
                    {
                        label.Parameter = usedTypeAttr;
                    }
                }
                else if (SO.targetObject is IKeyValueDictionaryWithTypeName <TKeyValue, T> )
                {
                    var inst = SO.targetObject as IKeyValueDictionaryWithTypeName <TKeyValue, T>;
                    label.Parameter = inst.HasType;
                }

                doChanged |= EditorGUI.PropertyField(position.Pos, element, label);

                //キーが更新されたか?
                var keyProp    = element.FindPropertyRelative("_key");
                var doContains = nameHash.Contains(keyProp.stringValue);
                doChanged |= doContains;
                if (!doContains)
                {
                    nameHash.Add(keyProp.stringValue);
                }
            }
            if (doChanged)
            {
                valuesProp.arraySize = newArraySize;
                Reflesh(valuesProp);
                OnApplyBefore(SO, valuesProp);
                if (SO.ApplyModifiedProperties())
                {
                    var instance = SO.targetObject as TDictionary;
                    instance.Refresh();
                    OnApplyAfter(instance);
                }
            }

            //Show Pagination
            if (valuesProp.arraySize >= ELEMENT_PER_PAGE)
            {
                position.IncrementRow();

                var maxPage = (valuesProp.arraySize / ELEMENT_PER_PAGE);
                maxPage += (valuesProp.arraySize % ELEMENT_PER_PAGE) == 0 ? 0 : 1;
                _page    = EditorGUI.IntSlider(position.Pos, $"Pagination {_page + 1}/{maxPage}", _page + 1, 1, maxPage);
                _page    = Mathf.Clamp(_page - 1, 0, maxPage);
            }
        }