Example #1
0
        /// <summary>
        /// 根据input port的类型,将对应数据初始化
        /// </summary>
        /// <param name="inputPortReflectionInfo"></param>
        /// <param name="type"></param>
        /// <param name="valueString"></param>
        /// <param name="nodeView"></param>
        private static void SetNodeInputVariableValue(InputPortReflectionInfo inputPortReflectionInfo, Type type, string valueString,
                                                      NodeEditorView nodeView)
        {
            if (inputPortReflectionInfo.inputValueType != type)
            {
                Debug.LogErrorFormat("节点{0}中的Input Port {1} 的泛型改变了,之前是{2},现在是{3}", nodeView.ReflectionInfo.Type,
                                     inputPortReflectionInfo.PortName, type.FullName, inputPortReflectionInfo.inputValueType.FullName);
                return;
            }

            if (type == typeof(int))
            {
                int value = int.Parse(valueString);
                inputPortReflectionInfo.SetInputNodeVariableValue(value);
            }
            else if (type == typeof(float))
            {
                float value = float.Parse(valueString);
                inputPortReflectionInfo.SetInputNodeVariableValue(value);
            }
            else if (type == typeof(string))
            {
                inputPortReflectionInfo.SetInputNodeVariableValue(valueString);
            }
            else if (type == typeof(bool))
            {
                bool value = Boolean.Parse(valueString);
                inputPortReflectionInfo.SetInputNodeVariableValue(value);
            }
            else if (type.IsEnum)
            {
                object value = Enum.Parse(type, valueString);
                inputPortReflectionInfo.SetInputNodeVariableValue(value);
            }
            else if (type == typeof(List <int>))
            {
                string[]   splitString = valueString.Split('|');
                List <int> value       = new List <int>();
                for (int i = 0; i < splitString.Length; i++)
                {
                    value.Add(int.Parse(splitString[i]));
                }

                inputPortReflectionInfo.SetInputNodeVariableValue(value);
            }
            else if (type == typeof(LayerMaskWrapper))
            {
                LayerMaskWrapper value = valueString;
                inputPortReflectionInfo.SetInputNodeVariableValue(value);
            }
            else if (type == typeof(VariableWrapper))
            {
                int intValue;
                if (int.TryParse(valueString, out intValue))
                {
                    VariableWrapper variableWrapper = intValue;
                    inputPortReflectionInfo.SetInputNodeVariableValue(variableWrapper);
                }
                else
                {
                    Debug.LogErrorFormat("标记为VariableWrapper类型的input接口,但是它记录的值不是int类型的: {0}", valueString);
                }
            }
        }
Example #2
0
        /// <summary>
        /// 绘制各种类型的输入框
        /// </summary>
        /// <param name="type"></param>
        void DrawInputFieldLabel(Type type)
        {
            if (type == typeof(float))
            {
                string typeName = Utility.BeautifyTypeName(type) + ": ";
                CalculateAndDrawLabelCommonElement(typeName, 100f);

                EditorGUI.BeginChangeCheck();
                float value = (float)inputPortReflectionInfo.GetInputNodeVariableValue();
                value = EditorGUI.FloatField(labelInputAreaRect, value);
                if (EditorGUI.EndChangeCheck())
                {
                    inputPortReflectionInfo.SetInputNodeVariableValue(value);
                }
            }
            else if (type == typeof(int))
            {
                string typeName = Utility.BeautifyTypeName(type) + ": ";
                CalculateAndDrawLabelCommonElement(typeName, 100f);

                EditorGUI.BeginChangeCheck();
                int value = (int)inputPortReflectionInfo.GetInputNodeVariableValue();
                value = EditorGUI.IntField(labelInputAreaRect, value);
                if (EditorGUI.EndChangeCheck())
                {
                    inputPortReflectionInfo.SetInputNodeVariableValue(value);
                }
            }
            else if (type == typeof(string))
            {
                string typeName = Utility.BeautifyTypeName(type) + ": ";
                CalculateAndDrawLabelCommonElement(typeName, 100f);

                EditorGUI.BeginChangeCheck();
                string value = (string)inputPortReflectionInfo.GetInputNodeVariableValue();
                if (GUI.Button(labelInputAreaRect, value))
                {
                    PopupWindow.Show(labelInputAreaRect,
                                     new StringEditPopupWindow(value,
                                                               stringContent => { inputPortReflectionInfo.SetInputNodeVariableValue(stringContent); }));
                }
            }
            else if (type == typeof(bool))
            {
                string typeName = Utility.BeautifyTypeName(type) + ": ";
                CalculateAndDrawLabelCommonElement(typeName, 20f);

                EditorGUI.BeginChangeCheck();
                bool value = (bool)inputPortReflectionInfo.GetInputNodeVariableValue();
                value = EditorGUI.Toggle(labelInputAreaRect, value);
                if (EditorGUI.EndChangeCheck())
                {
                    inputPortReflectionInfo.SetInputNodeVariableValue(value);
                }
            }
            else if (type.IsEnum)
            {
                if (maxEnumNeedWidth < 0)
                {
                    maxEnumNeedWidth = Utility.GetEnumMaxGuiWidth(type, 16);
                }

                string typeName = Utility.BeautifyTypeName(type) + ": ";
                CalculateAndDrawLabelCommonElement(typeName, maxEnumNeedWidth + 25); //25是enum选择框箭头的位置

                EditorGUI.BeginChangeCheck();
                Enum enumValue = (Enum)inputPortReflectionInfo.GetInputNodeVariableValue();
                enumValue = EditorGUI.EnumPopup(labelInputAreaRect, enumValue);
                if (EditorGUI.EndChangeCheck())
                {
                    inputPortReflectionInfo.SetInputNodeVariableValue(enumValue);
                }
            }
            //支持list<int>
            else if (type == typeof(List <int>))
            {
                string typeName = Utility.BeautifyTypeName(type) + ": ";
                CalculateAndDrawLabelCommonElement(typeName, 100f);

                List <int> list = inputPortReflectionInfo.GetInputNodeVariableValue() as List <int>;
                if (list == null)
                {
                    list = new List <int>();
                }

                string listHint = string.Join(",", list.Select(x => x.ToString()).ToArray());
                if (GUI.Button(labelInputAreaRect, listHint))
                {
                    ListIntEditorWindow.ShowWindow(list,
                                                   editedList => { inputPortReflectionInfo.SetInputNodeVariableValue(editedList); });
                }
            }
            else if (type == typeof(LayerMaskWrapper))
            {
                if (maxLayerMaskNeedWidth < 0)
                {
                    maxLayerMaskNeedWidth = Utility.GetLayerMaxGuiLength(8);
                }

                string typeName = Utility.BeautifyTypeName(type) + ": ";
                CalculateAndDrawLabelCommonElement(typeName, maxLayerMaskNeedWidth + 25);

                EditorGUI.BeginChangeCheck();
                LayerMaskWrapper layerMaskWrapperValue = (LayerMaskWrapper)inputPortReflectionInfo.GetInputNodeVariableValue();
                if (layerMaskWrapperValue == null)
                {
                    layerMaskWrapperValue = new LayerMaskWrapper();
                }

                string[] gameLayerNames = Utility.GetUnityLayerNames();

                int selectLayer = EditorGUI.MaskField(labelInputAreaRect, layerMaskWrapperValue.layer, gameLayerNames);
                if (EditorGUI.EndChangeCheck())
                {
                    layerMaskWrapperValue.layer = selectLayer;
//                    Debug.Log("select layer mask: " + layerMaskValue);
                    inputPortReflectionInfo.SetInputNodeVariableValue(layerMaskWrapperValue);
                }
            }
            //选择技能变量
            else if (type == typeof(VariableWrapper))
            {
                VariableWrapper variableWrapper = inputPortReflectionInfo.GetInputNodeVariableValue() as VariableWrapper;
                if (variableWrapper == null)
                {
                    variableWrapper = new VariableWrapper();
                }

                int               varibleId         = variableWrapper.variableId;
                GraphEditorData   data              = GraphEditorWindow.instance.data;
                GraphVariableInfo graphVariableInfo = data.GetGraphVariableInfo(varibleId);

                string buttonName;
                //当前没有选择任何一个变量
                if (graphVariableInfo == null)
                {
                    buttonName = "选择一个变量!";
                }
                else
                {
                    buttonName = graphVariableInfo.name;
                }

                CalculateAndDrawLabelCommonElement("变量: ", EditorStyles.label.CalcSize(new GUIContent(buttonName)).x + 20);
                if (GUI.Button(labelInputAreaRect, buttonName))
                {
                    PopupWindow.Show(labelInputAreaRect, new SelectGraphVariablePopupWindow(data.CurrentGraphVariableInfoList,
                                                                                            selectVariableId =>
                    {
                        variableWrapper.variableId = selectVariableId;
                        inputPortReflectionInfo.SetInputNodeVariableValue(variableWrapper);

                        Type variableType = GraphEditorWindow.instance.data
                                            .GetGraphVariableInfo(selectVariableId).valueType;
                        NodeView.UpdateGraphVariableNodeIOPortType(variableType, true);
                    }));
                }
            }
            //所有需要自定义Input标签的类型写在这个分支前面
            else if (type.IsClass)
            {
                string typeName = Utility.BeautifyTypeName(type) + ": ";
                CalculateAndDrawLabelCommonElement(typeName, 30f);

                GUI.Label(labelInputAreaRect, "(Null)", labelTextStyle);
            }
            else
            {
                string typeName = Utility.BeautifyTypeName(type) + ": ";
                string content  = string.Format("Default({0})", type.Name);
                CalculateAndDrawLabelCommonElement(typeName, Utility.GetStringGuiWidth(content, labelTextStyle.fontSize));

                GUI.Label(labelInputAreaRect, content, labelTextStyle);
            }
        }