public static void Check() { // package.json检查 var hasFile = HasPackageJson; if (hasFile == false) { return; } mainFlag = HasMainDir ? "/_main_/" : "/"; _packageJsonInfo = GetPackageJsonInfo(); // PackagePath.cs检查 PackagePathCheck(); // UPMToolImporter.cs检查 // UPMToolImporterCheck(); // Editor下.asmdef检查 EditorAsmdefCheck(); AssetDatabase.Refresh(); }
/// <summary> /// 绘制所有依赖项 /// </summary> /// <param name="packageJsonInfo"></param> private void DrawDependencyItems(PackageJsonInfo packageJsonInfo, string dependType) { var serializedObject = new SerializedObject(packageJsonInfo); var dependencies = serializedObject.FindProperty(dependType); for (int i = 0; i < dependencies.arraySize; i++) { var property = dependencies.GetArrayElementAtIndex(i); DrawDependencyItem(property, dependType); } DrawExistDependencyItem(dependType); }
/// <summary> /// package.json文本转为PackageJsonInfo对象 /// </summary> /// <param name="json"></param> /// <returns></returns> public static PackageJsonInfo Parse(string json) { try { var jObject = JsonConvert.DeserializeObject <JObject>(json); var packageJson = (PackageJsonInfo)PackageJsonInfo.CreateInstance(typeof(PackageJsonInfo)); packageJson.name = (string)jObject["name"]; packageJson.displayName = (string)jObject["displayName"]; packageJson.version = (string)jObject["version"]; packageJson.unity = (string)jObject["unity"]; packageJson.description = (string)jObject["description"]; packageJson.type = (string)jObject["type"]; // 读取Unity依赖方式 packageJson.dependencies = new List <PackageDependency>(); if (jObject.ContainsKey("dependencies")) { var ds = (JObject)jObject["dependencies"]; foreach (var d in ds) { var pd = new PackageDependency { packageName = (string)d.Key, version = (string)d.Value }; packageJson.dependencies.Add(pd); } } // 读取UPMTool依赖方式 packageJson.dependenciesUt = new List <PackageDependency>(); if (jObject.ContainsKey("dependenciesUt")) { var ds = (JObject)jObject["dependenciesUt"]; foreach (var d in ds) { var pd = new PackageDependency { packageName = (string)d.Key, version = (string)d.Value }; packageJson.dependenciesUt.Add(pd); } } return(packageJson); } catch (Exception e) { Debug.LogError(e); throw; } }
public void DrawDependenciesUt(PackageJsonInfo packageJsonInfo, UPMToolExtension upmToolExtension) { var dependRoot = this.Q <VisualElement>("dependencies_item_root"); // 归还所有节点 var childCount = dependRoot.childCount; for (int i = 0; i < childCount; i++) { var item = dependRoot.ElementAt(0); ReturnDependenciesItem(item); dependRoot.RemoveAt(0); } if (packageJsonInfo.dependenciesUt.Count <= 0) { GetNoneDependenciesTip().style.display = new StyleEnum <DisplayStyle>(DisplayStyle.Flex); return; } else { GetNoneDependenciesTip().style.display = new StyleEnum <DisplayStyle>(DisplayStyle.None); } // 绘制节点 foreach (var dependency in packageJsonInfo.dependenciesUt) { var item = GetDependenciesItem(); dependRoot.Add(item); item.Q <Label>("packageName_lab").text = dependency.packageName; item.Q <Label>("version_lab").text = dependency.version; var button = item.Q <Button>("import_btn"); button.clickable = null; if (upmToolExtension.InstalledPackageInfos.ContainsKey(dependency.packageName)) { button.text = "imported"; button.SetEnabled(false); } else { button.text = "import"; button.SetEnabled(true); button.clicked += () => { Debug.Log($"click {dependency.packageName}"); upmToolExtension.InstallDependenciesPackage($"{dependency.packageName}@{dependency.version}"); }; } } }
/// <summary> /// 移除依赖项 /// </summary> /// <param name="packageJsonInfo"></param> private void RemoveDependencyItem(PackageJsonInfo packageJsonInfo, List <PackageDependency> dependList, string dependType) { var lastIndex = dependList.Count - 1; dependList.RemoveAt(lastIndex); var root = GetDependencyItemsRoot(dependType); var i = root.ElementAt(lastIndex); ReturnDependenciesItem(i); root.RemoveAt(lastIndex); DrawExistDependencyItem(dependType); }
/// <summary> /// 添加依赖项 /// </summary> /// <param name="packageJsonInfo"></param> private void AddDependencyItem(PackageJsonInfo packageJsonInfo, List <PackageDependency> dependList, string dependType) { var dependency = new PackageDependency(); dependList.Add(dependency); var serializedObject = new SerializedObject(packageJsonInfo); var dependencies = serializedObject.FindProperty(dependType); var property = dependencies.GetArrayElementAtIndex(dependencies.arraySize - 1); DrawDependencyItem(property, dependType); DrawExistDependencyItem(dependType); }
/// <summary> /// 初始化创建界面UI /// </summary> public void InitUIElementCreate(PackageJsonInfo packageJsonInfo, string path) { var root = this; // 预览 var preview = root.Q <TextField>("preview_tf"); preview.value = PackageJson.ToJson(packageJsonInfo); // 创建按钮响应点击 var button = root.Q <Button>("create_btn"); button.clicked += () => { // 创建插件包的动作 PackageJsonEditor.CreatePackageAction(packageJsonInfo); // 添加"UPMTool"依赖 // PackageJsonEditor.AddUPMToolDependency(packageJsonInfo); // 创建或修改package.json var rst = PackageJsonEditor.SavePackageJsonChange(packageJsonInfo, path, out var msg); DrawSavePackageJsonInfoRet(rst, msg); // 保存失败直接结束 if (rst == false) { return; } preview.value = PackageJson.ToJson(packageJsonInfo); // 刷新,显示插件包框架 AssetDatabase.Refresh(); // 创建PackagePath.cs,需要检查插件包路径才能创建 PackageJsonEditor.AfterCreatePackageAction(); // 刷新,显示PackagePath.cs AssetDatabase.Refresh(); }; // 编辑按钮隐藏 var element = root.Q <VisualElement>("edit_box"); element.parent.Remove(element); // 初始化依赖操作(Create界面不需要依赖操作,所以要隐藏) InitDependenciesUIElement(root, packageJsonInfo, "dependencies", false); InitDependenciesUIElement(root, packageJsonInfo, "dependenciesUt", false); }
/// <summary> /// 初始化编辑界面UI /// </summary> public void InitUIElementEditor(PackageJsonInfo packageJsonInfo, string path) { var root = this; // 预览 var preview = root.Q <TextField>("preview_tf"); preview.value = PackageJson.ToJson(packageJsonInfo); // TODO 编辑按钮-撤销修改响应点击 var button = root.Q <Button>("revert_btn"); button.clicked += () => { Debug.Log("抱歉!撤销功能还没做..."); }; // 编辑按钮-应用修改响应点击 button = root.Q <Button>("apply_btn"); button.clicked += () => { var rst = PackageJsonEditor.SavePackageJsonChange(packageJsonInfo, path, out var msg); DrawSavePackageJsonInfoRet(rst, msg); // 保存失败直接结束 if (rst == false) { return; } preview.value = PackageJson.ToJson(packageJsonInfo); AssetDatabase.Refresh(); }; // 创建按钮隐藏 var element = root.Q <VisualElement>("create_box"); element.parent.Remove(element); // 初始化依赖操作 InitDependenciesUIElement(root, packageJsonInfo, "dependencies", true); InitDependenciesUIElement(root, packageJsonInfo, "dependenciesUt", true); }
/// <summary> /// 插件依赖相关UIElement交互 /// </summary> private void InitDependenciesUIElement(VisualElement root, PackageJsonInfo packageJsonInfo, string dependType, bool isShow) { if (isShow == false) { var element = root.Q <VisualElement>($"{dependType}_box"); element.parent.Remove(element); element = root.Q <VisualElement>($"{dependType}_lab_box"); element.parent.Remove(element); return; } // 预览 var preview = root.Q <TextField>("preview_tf"); preview.value = PackageJson.ToJson(packageJsonInfo); // 添加依赖按钮响应 var button = root.Q <Button>($"{dependType}_add"); button.clicked += () => { AddDependencyItem(packageJsonInfo, packageJsonInfo.GetDependenciesByType(dependType), dependType); }; // 移除依赖按钮响应 button = root.Q <Button>($"{dependType}_remove"); button.clicked += () => { RemoveDependencyItem(packageJsonInfo, packageJsonInfo.GetDependenciesByType(dependType), dependType); }; // 绘制依赖项 DrawDependencyItems(packageJsonInfo, dependType); }
/// <summary> /// 将PackageJsonInfo对象转为json文本 /// </summary> /// <param name="packageJsonInfo"></param> /// <returns></returns> public static string ToJson(PackageJsonInfo packageJsonInfo) { var setting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; var jObject = new JObject { ["name"] = packageJsonInfo.name, ["displayName"] = packageJsonInfo.displayName, ["version"] = packageJsonInfo.version, ["unity"] = packageJsonInfo.unity, ["description"] = packageJsonInfo.description, ["type"] = packageJsonInfo.type }; // Unity的依赖方式 var dependencies = packageJsonInfo.dependencies; if (dependencies != null && dependencies.Count > 0) { var ds = new JObject(); foreach (var dependency in dependencies) { if (string.IsNullOrEmpty(dependency.packageName) || string.IsNullOrEmpty(dependency.version)) { continue; } ds[dependency.packageName] = dependency.version; } if (ds.Count > 0) { jObject["dependencies"] = ds; } } // UPMTool的依赖方式 var dependenciesUt = packageJsonInfo.dependenciesUt; if (dependenciesUt != null && dependenciesUt.Count > 0) { var ds = new JObject(); foreach (var dependency in dependenciesUt) { if (string.IsNullOrEmpty(dependency.packageName) || string.IsNullOrEmpty(dependency.version)) { continue; } ds[dependency.packageName] = dependency.version; } if (ds.Count > 0) { jObject["dependenciesUt"] = ds; } } var json = JsonConvert.SerializeObject(jObject, Formatting.Indented, setting); return(json); }
/// <summary> /// 初始化共用部分UI /// </summary> public void InitUIElementCommon(PackageJsonInfo packageJsonInfo) { var root = this; // 预览 var preview = root.Q <TextField>("preview_tf"); preview.value = PackageJson.ToJson(packageJsonInfo); // 插件名 com.xxx.xxx var textField = root.Q <TextField>("name_tf"); textField.bindingPath = "name"; // textField.value = packageJsonInfo.name; // textField.RegisterValueChangedCallback(value => // { // packageJsonInfo.name = value.newValue; // preview.value = packageJsonInfo.ToJson(); // }); // 显示名称 textField = root.Q <TextField>("displayName_tf"); textField.bindingPath = "displayName"; // textField.value = packageJsonInfo.displayName; // textField.RegisterValueChangedCallback(value => // { // packageJsonInfo.displayName = value.newValue; // preview.value = packageJsonInfo.ToJson(); // }); // 版本 textField = root.Q <TextField>("version_tf"); textField.bindingPath = "version"; // textField.value = packageJsonInfo.version; // textField.RegisterValueChangedCallback(value => // { // packageJsonInfo.version = value.newValue; // preview.value = packageJsonInfo.ToJson(); // }); // unity版本,例:2019.4 textField = root.Q <TextField>("unity_tf"); textField.bindingPath = "unity"; // textField.value = packageJsonInfo.unity; // textField.RegisterValueChangedCallback(value => // { // packageJsonInfo.unity = value.newValue; // preview.value = packageJsonInfo.ToJson(); // }); // 类型(内部保留使用) textField = root.Q <TextField>("type_tf"); textField.bindingPath = "type"; // textField.value = packageJsonInfo.type; // textField.RegisterValueChangedCallback(value => // { // packageJsonInfo.type = value.newValue; // preview.value = packageJsonInfo.ToJson(); // }); // 描述 textField = root.Q <TextField>("description_tf"); textField.bindingPath = "description"; // textField.value = packageJsonInfo.description; // textField.RegisterValueChangedCallback(value => // { // packageJsonInfo.description = value.newValue; // preview.value = packageJsonInfo.ToJson(); // }); // 依赖关系(依赖相关UI不在这里添加,在PackageJsonEditor中) // 返回消息 var label = root.Q <Label>("msg_lab"); label.text = ""; // PackageJsonInfo和UIElement绑定 var serializedObject = new SerializedObject(packageJsonInfo); root.Bind(serializedObject); }