Пример #1
0
 //编辑模式下绑定测试动作
 public void BindTestEvent(GWidget preview)
 {
     //return;
     for (int i = 0; i < preview.eventInfos.Count; i++)
     {
         string[] component_event = preview.eventInfos[i].eventName.Split('.');
         if (component_event.Length == 2)
         {
             GTestAction action = GetAction(preview.eventInfos[i].eventName, false);
             if (action != null)
             {
                 preview.eventInfos[i].actionType = action.actionType;
                 preview.eventInfos[i].targetA    = action.targetA;
                 preview.eventInfos[i].targetB    = action.targetB;
                 Component c = preview.eventInfos[i].sender.GetComponent(component_event[0]);
                 if (c is Button)
                 {
                     if (component_event[1] == "onClick")
                     {
                         (c as Button).onClick.AddListener(preview.eventInfos[i].Execute);
                     }
                 }
             }
         }
     }
 }
Пример #2
0
 public GTestAction GetAction(string name, bool autoAddIfNotFound)
 {
     for (int i = 0; i < actions.Count; i++)
     {
         if (actions[i].name == name)
         {
             return(actions[i]);
         }
     }
     if (autoAddIfNotFound)
     {
         GTestAction action = new GTestAction();
         action.name = name;
         actions.Add(action);
         return(action);
     }
     return(null);
 }
    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        //检查能否同时编辑
        bool canMultipleEdit = true;

        if (serializedObject.isEditingMultipleObjects)
        {
            for (int i = 1; i < allTargets.Length; i++)
            {
                if (allTargets[i].prefab != firstTarget.prefab)  //只有全部是相同的prefab才能同时编辑
                {
                    canMultipleEdit = false;
                }
            }
        }
        if (!canMultipleEdit)
        {
            GUILayout.Label("只能同时编辑相同prefab的项");
            return;
        }

        //Prefab替换功能
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.BeginHorizontal();
        GWidget newPrefab = (GWidget)EditorGUILayout.ObjectField("prefab", firstTarget.prefab, typeof(GWidget), false);
        bool    reload    = GUILayout.Button("Reload", GUILayout.Width(60));

        if (EditorGUI.EndChangeCheck() || reload)
        {
            for (int i = 0; i < allTargets.Length; i++)
            {
                if (allTargets[i].preview)
                {
                    GameObject.DestroyImmediate(allTargets[i].preview.gameObject);
                    allTargets[i].preview = null;
                }
                allTargets[i].prefab = newPrefab;
                allTargets[i].Instantiate();
            }
        }

        if (GUILayout.Button("Edit", GUILayout.Width(50)))
        {
            GWidgetInspector.StartEditWidget(firstTarget.prefab);
        }

        EditorGUILayout.EndHorizontal();
        //base.OnInspectorGUI();
        if (firstTarget.gameObject.scene.name != null)
        {
            GWidget prefab = firstTarget.prefab;
            //属性设置
            bool bChangeAnyOne = false;
            for (int i = 0; i < prefab.propertyInfos.Count; i++)
            {
                string propertyName = prefab.propertyInfos[i].propertyName;
                string key          = propertyName;
                if (prefab.propertyInfos[i].target != prefab.gameObject)  //如果属性的目标不是prefab根节点的话,要在key上携带目标节点的名称
                {
                    key = prefab.propertyInfos[i].target.name + "." + key;
                }
                PropertyInfo pi           = null;
                string[]     cn           = propertyName.Split('.');
                string       label        = prefab.propertyInfos[i].rename;
                object       defaultValue = null;
                if (cn.Length == 2)
                {
                    if (label == "")
                    {
                        label = propertyName;
                    }
                    MonoBehaviour mb = (MonoBehaviour)prefab.propertyInfos[i].target.GetComponent(cn[0]);
                    pi           = mb.GetType().GetProperty(cn[1]);
                    defaultValue = pi.GetValue(mb, null);
                }
                else if (cn.Length == 1)
                {
                    pi           = prefab.propertyInfos[i].target.GetType().GetProperty(cn[0]);
                    defaultValue = pi.GetValue(prefab.propertyInfos[i].target, null);
                }
                if (pi != null)
                {
                    if (hasMultipleDifferentValues(allTargets, key, pi.PropertyType, defaultValue))
                    {
                        EditorGUI.showMixedValue = true;
                    }
                    EditorGUI.BeginChangeCheck();

                    object oldValue = firstTarget.GetValue(key, pi.PropertyType, defaultValue);
                    object newValue = null;
                    if (pi.PropertyType == typeof(bool))
                    {
                        newValue = EditorGUILayout.Toggle(label, (bool)oldValue);
                    }
                    else if (pi.PropertyType == typeof(string))
                    {
                        GUILayout.BeginHorizontal();
                        GUILayout.Label(label, GUILayout.Width(100));
                        newValue = EditorGUILayout.TextArea((string)oldValue);
                        GUILayout.EndHorizontal();
                    }
                    else if (pi.PropertyType == typeof(Sprite))
                    {
                        newValue = EditorGUILayout.ObjectField(label, (Object)oldValue, typeof(Sprite), false);
                    }
                    else if (pi.PropertyType.IsEnum)
                    {
                        newValue = EditorGUILayout.EnumPopup(label, (System.Enum)oldValue);
                    }
                    else if (pi.PropertyType == typeof(GameObject))
                    {
                        newValue = EditorGUILayout.ObjectField(label, (Object)oldValue, typeof(GameObject), false);
                    }
                    else if (pi.PropertyType == typeof(int))
                    {
                        newValue = EditorGUILayout.IntField(label, (int)oldValue);
                    }
                    else if (pi.PropertyType == typeof(float))
                    {
                        newValue = EditorGUILayout.FloatField(label, (float)oldValue);
                    }
                    else if (pi.PropertyType == typeof(Vector2))
                    {
                        newValue = EditorGUILayout.Vector2Field(label, (Vector2)oldValue);
                    }
                    else if (pi.PropertyType == typeof(Vector3))
                    {
                        newValue = EditorGUILayout.Vector3Field(label, (Vector3)oldValue);
                    }
                    else if (pi.PropertyType == typeof(string[]))
                    {
                        List <Dropdown.OptionData> opts = oldValue == null ? (new List <Dropdown.OptionData>()) : (List <Dropdown.OptionData>)oldValue;
                        string s = string.Join("\n", (from opt in opts select opt.text).ToArray());
                        s        = EditorGUILayout.TextArea(s, GUILayout.Height(100));
                        newValue = s == ""?new List <Dropdown.OptionData>():new List <Dropdown.OptionData>(from t in s.Split('\n') select new Dropdown.OptionData(t));
                    }
                    EditorGUI.showMixedValue = false;
                    if (EditorGUI.EndChangeCheck())
                    {
                        bChangeAnyOne = true;
                        for (int t = 0; t < allTargets.Length; t++)
                        {
                            allTargets[t].SetValue(key, newValue);
                        }
                    }
                }
            }

            if (bChangeAnyOne)
            {
                for (int t = 0; t < allTargets.Length; t++)
                {
                    allTargets[t].UpdatePreviewValues(allTargets[t].preview);
                    EditorUtility.SetDirty(allTargets[t]);
                }
            }

            if (!serializedObject.isEditingMultipleObjects)
            {
                //事件绑定
                for (int i = 0; i < prefab.eventInfos.Count; i++)
                {
                    string[] cn = prefab.eventInfos[i].eventName.Split('.');
                    if (cn.Length == 2)
                    {
                        string label = prefab.eventInfos[i].rename;
                        GUILayout.BeginHorizontal();
                        GUILayout.Label(label);
                        GTestAction action = firstTarget.GetAction(prefab.eventInfos[i].eventName, true);
                        if (action != null)
                        {
                            action.actionType = (GExportEventInfo.ActionType)EditorGUILayout.EnumPopup(action.actionType);
                            if (GExportEventInfo.GetTargetNum(action.actionType) == 1)
                            {
                                GUILayout.Label("target:");
                                action.targetA = (GameObject)EditorGUILayout.ObjectField(action.targetA, typeof(GameObject), true);
                            }
                            else if (GExportEventInfo.GetTargetNum(action.actionType) == 2)
                            {
                                GUILayout.Label("A:");
                                action.targetA = (GameObject)EditorGUILayout.ObjectField(action.targetA, typeof(GameObject), true);
                                GUILayout.Label("B:");
                                action.targetB = (GameObject)EditorGUILayout.ObjectField(action.targetB, typeof(GameObject), true);
                            }
                        }
                        GUILayout.EndHorizontal();
                    }
                }
            }
        }

        if (!serializedObject.isEditingMultipleObjects)
        {
            EditorGUILayout.Space();

            EditorGUILayout.TextArea(GetScriptString2(firstTarget));
        }
    }