static void AddComponent2GameObject()
        {
            var generateClassName = EditorPrefs.GetString("GENERATE_CLASS_NAME");
            var gameObjectName    = EditorPrefs.GetString("GAME_OBJECT_NAME");
            var generateNamespace = EditorPrefs.GetString("GENERATE_NAMESPACE");

            if (string.IsNullOrEmpty(generateClassName))
            {
                EditorPrefs.DeleteKey("GENERATE_CLASS_NAME");
                EditorPrefs.DeleteKey("GAME_OBJECT_NAME");
            }
            else
            {
                var assemblies = AppDomain.CurrentDomain.GetAssemblies();

                var defaultAssembly = assemblies.First(assembly => assembly.GetName().Name == "Assembly-CSharp");

                var typeName = generateNamespace + "." + generateClassName;

                var type = defaultAssembly.GetType(typeName);

                if (type == null)
                {
                    Log.I("编译失败");
                    return;
                }

                Log.I(type);

                var gameObject = GameObject.Find(gameObjectName);

                if (!gameObject)
                {
                    Log.I("上次的 View Controller 生成失败,找不到 GameObject:{0}".FillFormat(gameObjectName));

                    Clear();
                    return;
                }


                var scriptComponent = gameObject.GetComponent(type);

                if (!scriptComponent)
                {
                    scriptComponent = gameObject.AddComponent(type);
                }

                var serialiedScript = new SerializedObject(scriptComponent);

                var panelCodeInfo = new PanelCodeInfo();

                panelCodeInfo.GameObjectName = gameObjectName;

                // 搜索所有绑定
                BindCollector.SearchBinds(gameObject.transform, "", panelCodeInfo);

                foreach (var bindInfo in panelCodeInfo.BindInfos)
                {
                    var name = bindInfo.Name;

                    var componentName = bindInfo.BindScript.ComponentName.Split('.').Last();

                    serialiedScript.FindProperty(name).objectReferenceValue =
                        gameObject.transform.Find(bindInfo.PathToElement)
                        .GetComponent(componentName);
                }


                var codeGenerateInfo = gameObject.GetComponent <ViewController>();

                if (codeGenerateInfo)
                {
                    serialiedScript.FindProperty("ScriptsFolder").stringValue = codeGenerateInfo.ScriptsFolder;
                    serialiedScript.FindProperty("PrefabFolder").stringValue  = codeGenerateInfo.PrefabFolder;
                    serialiedScript.FindProperty("GeneratePrefab").boolValue  = codeGenerateInfo.GeneratePrefab;
                    serialiedScript.FindProperty("ScriptName").stringValue    = codeGenerateInfo.ScriptName;
                    serialiedScript.FindProperty("Namespace").stringValue     = codeGenerateInfo.Namespace;

                    var generatePrefab = codeGenerateInfo.GeneratePrefab;
                    var prefabFolder   = codeGenerateInfo.PrefabFolder;

                    var fullPrefabFolder = prefabFolder.Replace("Assets", Application.dataPath);

                    if (codeGenerateInfo.GetType() != type)
                    {
                        DestroyImmediate(codeGenerateInfo, false);
                    }

                    serialiedScript.ApplyModifiedPropertiesWithoutUndo();

                    if (generatePrefab)
                    {
                        fullPrefabFolder.CreateDirIfNotExists();

                        var genereateFolder = prefabFolder + "/" + gameObject.name + ".prefab";

                        PrefabUtils.SaveAndConnect(genereateFolder, gameObject);
                    }
                }
                else
                {
                    serialiedScript.FindProperty("ScriptsFolder").stringValue = "Assets/Scripts";
                    serialiedScript.ApplyModifiedPropertiesWithoutUndo();
                }

                Clear();

                EditorUtils.MarkCurrentSceneDirty();
            }
        }
Example #2
0
        public void OnCreateUIPanelClick()
        {
            var controllerNode = ControllerNode <PackageKit> .Allocate();

            var panelName = mPanelNameToCreate;

            if (panelName.IsNotNullAndEmpty())
            {
                var fullScenePath = "Assets/Scenes/TestUIPanels/".CreateDirIfNotExists()
                                    .Append("Test{0}.unity".FillFormat(panelName)).ToString();

                var panelPrefabPath = "Assets/Art/UIPrefab/".CreateDirIfNotExists()
                                      .Append("{0}.prefab".FillFormat(panelName)).ToString();

                if (File.Exists(panelPrefabPath))
                {
                    SingletonProperty <PackageKit> .Instance.GetUtility <IEditorDialogUtility>()
                    .ShowErrorMsg("UI 界面已存在:{0}".FillFormat(panelPrefabPath));

                    return;
                }

                if (File.Exists(fullScenePath))
                {
                    SingletonProperty <PackageKit> .Instance.GetUtility <IEditorDialogUtility>()
                    .ShowErrorMsg("测试场景已存在:{0}".FillFormat(fullScenePath));

                    return;
                }

                RenderEndCommandExecuter.PushCommand(() =>
                {
                    var currentScene = SceneManager.GetActiveScene();
                    EditorSceneManager.SaveScene(currentScene);

                    var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene);
                    EditorSceneManager.SaveScene(scene, fullScenePath);

                    var uiroot = Resources.Load <GameObject>("UIRoot").Instantiate().Name("UIRoot");

                    var designTransform = uiroot.transform.Find("Design");

                    var gameObj = new GameObject(panelName);
                    gameObj.transform.Parent(designTransform)
                    .LocalScaleIdentity();

                    var rectTransform                = gameObj.AddComponent <RectTransform>();
                    rectTransform.offsetMin          = Vector2.zero;
                    rectTransform.offsetMax          = Vector2.zero;
                    rectTransform.anchoredPosition3D = Vector3.zero;
                    rectTransform.anchorMin          = Vector2.zero;
                    rectTransform.anchorMax          = Vector2.one;

                    var prefab = PrefabUtils.SaveAndConnect(panelPrefabPath, gameObj);

                    EditorSceneManager.SaveScene(scene);

                    // 标记 AssetBundle
                    ResKitAssetsMenu.MarkAB(panelPrefabPath);

                    var tester              = new GameObject("Test{0}".FillFormat(panelName));
                    var uiPanelTester       = tester.AddComponent <UIPanelTester>();
                    uiPanelTester.PanelName = panelName;

                    // 开始生成代码
                    UICodeGenerator.DoCreateCode(new[] { prefab });
                });
            }

            controllerNode.Recycle2Cache();
            controllerNode = null;
        }