public static void MarkPTABDir() { var path = EditorUtils.GetSelectedPathOrFallback(); MarkAB(path); }
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) { Debug.Log("编译失败"); return; } Debug.Log(type); var gameObject = GameObject.Find(gameObjectName); if (!gameObject) { Debug.Log("上次的 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(); } }
static void AddComponent2GameObject() { // Debug.Log("DidReloadScripts"); var generateClassName = EditorPrefs.GetString("GENERATE_CLASS_NAME"); var gameObjectName = EditorPrefs.GetString("GAME_OBJECT_NAME"); // Debug.Log(generateClassName); if (string.IsNullOrEmpty(generateClassName)) { // Debug.Log("不继续操作"); EditorPrefs.DeleteKey("GENERATE_CLASS_NAME"); EditorPrefs.DeleteKey("GAME_OBJECT_NAME"); } else { // Debug.Log("继续操作"); var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var defaultAssembly = assemblies.First(assembly => assembly.GetName().Name == "Assembly-CSharp"); var typeName = UIKitSettingData.Load().Namespace + "." + generateClassName; var type = defaultAssembly.GetType(typeName); if (type == null) { // Debug.Log("编译失败"); return; } // Debug.Log(type); var gameObject = GameObject.Find(gameObjectName); 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; serialiedScript.FindProperty(name).objectReferenceValue = gameObject.transform.Find(bindInfo.PathToElement) .GetComponent(bindInfo.BindScript.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; 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 = fullPrefabFolder + "/" + gameObject.name + ".prefab"; #if UNITY_2018_3_OR_NEWER PrefabUtility.SaveAsPrefabAssetAndConnect(gameObject, fullPrefabFolder + "/" + gameObject.name + ".prefab", InteractionMode.AutomatedAction); #else genereateFolder = prefabFolder + "/" + gameObject.name + ".prefab"; PrefabUtility.CreatePrefab(genereateFolder, gameObject, ReplacePrefabOptions.ConnectToPrefab); #endif } } else { serialiedScript.FindProperty("ScriptsFolder").stringValue = "Assets/Scripts"; serialiedScript.ApplyModifiedPropertiesWithoutUndo(); } EditorPrefs.DeleteKey("GENERATE_CLASS_NAME"); EditorPrefs.DeleteKey("GAME_OBJECT_NAME"); EditorUtils.MarkCurrentSceneDirty(); } }