public void SaveData() { if (target == null) { return; } string templateId = ""; string colorSheet = ""; var template = cmbTemplate.SelectedItem as EntityBase; if (template != null) { templateId = template.id; } colorSheet = cmbFill.SelectedItem as string; if (colorSheet == null) { colorSheet = ""; } Logic.CategoryCustomData_Material data = new Logic.CategoryCustomData_Material(); data.TemplateId = templateId; data.ColorSheet = colorSheet; data.ColorParamName = txtColorParamName.Text; data.MatPackageName = txtMatPackageName.Text; data.MatUrl = txtMatUrl.Text; target.customData = Newtonsoft.Json.JsonConvert.SerializeObject(data); }
public void SetTarget(CategoryModel Target) { target = Target; Logic.CategoryCustomData_Material customData = null; if (target != null && string.IsNullOrEmpty(target.customData) == false) { customData = Newtonsoft.Json.JsonConvert.DeserializeObject <Logic.CategoryCustomData_Material>(target.customData); } if (customData == null) { cmbTemplate.SelectedIndex = -1; cmbFill.SelectedIndex = 0; txtColorParamName.Text = "BaseColor"; txtMatUrl.Text = ""; txtMatPackageName.Text = ""; return; } cmbFill.SelectedItem = customData.ColorSheet; txtColorParamName.Text = customData.ColorParamName; if (templateList == null || templateList.data == null) { return; } MaterialModel template = null; foreach (var item in templateList.data) { if (item.id == customData.TemplateId) { template = item; cmbTemplate.SelectedItem = item; txtMatPackageName.Text = item.packageName; txtMatUrl.Text = item.url; break; } } }
async Task <int> parseCategoryDir(string dir, CategoryModel cate) { if (cate == null) { //templog("category is null"); return(0); } var files = Directory.GetFiles(dir); Dictionary <string, List <MaterialTextureInfo> > textureMap = new Dictionary <string, List <MaterialTextureInfo> >(); MaterialModel matTemplate = null; Logic.CategoryCustomData_Material matTemplateInfo = null; if (string.IsNullOrEmpty(cate.customData) == false) { try { matTemplateInfo = Newtonsoft.Json.JsonConvert.DeserializeObject <Logic.CategoryCustomData_Material>(cate.customData); } catch { } } if (matTemplateInfo?.TemplateId != null) { var getres = await Logic.BambooEditor.Instance.api.GetAsync <MaterialModel>("/material/" + matTemplateInfo.TemplateId); matTemplate = getres.Content; categoryTemplateMap[cate.id] = matTemplate; } foreach (var file in files) { MaterialTextureInfo info = new MaterialTextureInfo(); info.Path = Path.GetDirectoryName(file); string filename = Path.GetFileNameWithoutExtension(file); info.Name = ""; info.ParamName = ""; int tagIndex = filename.IndexOf('_'); if (tagIndex > 0) { info.Name = filename.Substring(0, tagIndex); if (tagIndex < filename.Length - 1) { info.ParamName = filename.Substring(tagIndex + 1); } } else { info.Name = filename; } List <MaterialTextureInfo> textures = null; if (textureMap.ContainsKey(info.Name)) { textures = textureMap[info.Name]; } else { textures = new List <MaterialTextureInfo>(); textureMap[info.Name] = textures; } textures.Add(info); } //templog($"{dir} find {files.Length} files, get {textureMap.Count} materials"); foreach (var item in textureMap) { MaterialFactoryItem mat = new MaterialFactoryItem(); mat.TextureList = item.Value; mat.Name = item.Key; mat.CategoryId = cate.id; mat.CategoryPath = getCategoryPathName(cate); mat.MaterialTempateName = matTemplate?.name; mat.PropObjToString(); Items.Add(mat); } var dirs = Directory.GetDirectories(dir); foreach (var d in dirs) { string dirname = Path.GetFileName(d); CategoryModel childcate = cate.children.Find(t => t.name == dirname); await parseCategoryDir(d, childcate); } return(textureMap.Count); }
async Task <string> CreateMaterials() { if (listSelectObjs.Items.Count == 0) { return("nothing is selected"); } var category = cateSelector.SelectedCategory; Logic.CategoryCustomData_Material cateCustomData = null; if (string.IsNullOrEmpty(category.customData) == false) { cateCustomData = Newtonsoft.Json.JsonConvert.DeserializeObject <Logic.CategoryCustomData_Material>(category.customData); } if (cateCustomData == null || string.IsNullOrEmpty(cateCustomData.ColorParamName) || string.IsNullOrEmpty(cateCustomData.MatPackageName)) { return("selected category custom data invalid"); //custom data invalid. } string ParamName = cateCustomData.ColorParamName; var tempMatRes = await Logic.BambooEditor.Instance.api.GetAsync <MaterialModel>("/material/" + cateCustomData.TemplateId); MaterialModel tempMat = tempMatRes.Content; if (tempMat == null) { return("selected category template material is empty"); //template material is empty. } StringBuilder sb = new StringBuilder(); int success = 0; foreach (var item in listObjs.SelectedItems) { EntityBase entity = item as EntityBase; if (entity == null) { sb.AppendLine("-- invalid entity"); continue; } // get object first. var texRes = await Logic.BambooEditor.Instance.api.GetAsync <TextureModel>("/texture/" + entity.id); var texture = texRes.Content; if (texture == null) { sb.AppendLine($"{entity.id} get data failed {texRes.StatusCode}"); continue; } MaterialModel material = new MaterialModel(); material.icon = texture.icon; material.IconUrl = texture.IconUrl; material.iconAssetId = texture.iconAssetId; material.name = texture.name; material.categoryId = category.id; material.categoryName = category.categoryName; material.fileAssetId = tempMat.fileAssetId; material.packageName = tempMat.packageName; material.dependencies = tempMat.dependencies; string textureFileAssetUrl = texture.fileAsset == null ? "" : texture.fileAsset.url; material.parameters = $"T&_{ParamName}={ParamName};{ParamName}Id={texture.id};{ParamName}Icon={texture.icon};{ParamName}P={texture.packageName};{ParamName}U={textureFileAssetUrl};"; var result = await Logic.BambooEditor.Instance.api.PostAsync("/material", material); if (result.IsSuccess) { EntityBase newMat = Newtonsoft.Json.JsonConvert.DeserializeObject <EntityBase>(result.Content); string newId = newMat == null ? "-" : newMat.id; sb.AppendLine($"{entity.id} {entity.name} create material {result.StatusCode} {newId}"); success++; } } return(sb.ToString()); }