/// <summary> /// 専用列挙型から、文字列を取得します。 /// </summary> /// <param name="type"></param> /// <returns></returns> public static string GetMaterialString(MaterialInfo.MaterialType type) { switch (type) { case MaterialInfo.MaterialType.Material: return("材料"); case MaterialInfo.MaterialType.Tool: return("道具"); } // デフォルト 到達不可能コード return("材料"); }
public static void GenerateUserMaterial(string texturePath, string textureName, Material templateMat, bool supportsCustomColors, MaterialInfo.MaterialType matType) { //stolen from http://gyanendushekhar.com/2017/07/08/load-image-runtime-unity/ byte[] byteArray = File.ReadAllBytes(texturePath); //create a texture and load byte array to it // Texture size does not matter Texture2D sampleTexture = new Texture2D(2, 2); // the size of the texture will be replaced by image size bool isLoaded = sampleTexture.LoadImage(byteArray); // apply this texure as per requirement on image or material GameObject image = GameObject.Find("RawImage"); Material newMat = Instantiate(templateMat); newMat.name = textureName; if (isLoaded) { newMat.SetTexture("_Tex", sampleTexture); //Logger.log.Debug("Loaded user texture " + textureName); PartBundleDescriptor newBundleDesc = new PartBundleDescriptor(); newBundleDesc.partBundleAuthor = "User"; newBundleDesc.partBundleName = "UserTextures"; MaterialDescriptor newMatDesc = new MaterialDescriptor(); newMatDesc.materialType = matType; newMatDesc.materialDisplayName = textureName; newMatDesc.supportsCustomColors = supportsCustomColors; AssetLoader.AddMaterialToList(newBundleDesc, newMatDesc, newMat); } }
//find user textures and send them to be loaded into custom materials public static void FindUserTextures() { List <string> imagePaths = (Directory.GetFiles(Path.Combine(Application.dataPath, "../SaberForgeAssets/UserTextures/"), "*.png", SearchOption.AllDirectories).ToList()); Logger.log.Debug($"Found {imagePaths.Count} user textures"); foreach (string path in imagePaths) { string fileName = path.Replace(Path.Combine(Application.dataPath, "../SaberForgeAssets/UserTextures/"), ""); string[] fileInfo = fileName.Split('_'); if (fileInfo.Length != 3) { Logger.log.Debug("Invalid texture name " + fileName + ", please refer to user guide for texture naming guidelines"); continue; } bool customColor = false; if (fileInfo[1] == "cctrue") { customColor = true; } else if (fileInfo[1] == "ccfalse") { customColor = false; } else { Logger.log.Debug("Invalid texture name " + fileName + ", please refer to user guide for texture naming guidelines"); continue; } string textureName = fileInfo[2].Replace(".png", ""); Material templateMat; MaterialInfo.MaterialType matType = MaterialInfo.MaterialType.NamePlate; //find template material using file name if (fileInfo[0] == "cutout") { textureName += "_Cutout"; if (customColor) { templateMat = PartEditor.FindMaterialInList("FrostDragon.CoreParts.Template.Cutout_Demo_CC", PartEditor.TemplateList); } else { templateMat = PartEditor.FindMaterialInList("FrostDragon.CoreParts.Template.Cutout_Demo_NoCC", PartEditor.TemplateList); } } else if (fileInfo[0] == "trans") { textureName += "_Trans"; if (customColor) { templateMat = PartEditor.FindMaterialInList("FrostDragon.CoreParts.Template.Trans_Demo_CC", PartEditor.TemplateList); } else { templateMat = PartEditor.FindMaterialInList("FrostDragon.CoreParts.Template.Trans_Demo_NoCC", PartEditor.TemplateList); } } else if (fileInfo[0] == "opaque") { textureName += "_Opaque"; if (customColor) { templateMat = PartEditor.FindMaterialInList("FrostDragon.CoreParts.Template.Opaque_Demo_CC", PartEditor.TemplateList); } else { templateMat = PartEditor.FindMaterialInList("FrostDragon.CoreParts.Template.Opaque_Demo_NoCC", PartEditor.TemplateList); } } else if (fileInfo[0] == "trail") { textureName += "_Trail"; matType = MaterialInfo.MaterialType.Trail; if (customColor) { templateMat = PartEditor.FindMaterialInList("FrostDragon.CoreParts.Template.Basic_Trail_CC", PartEditor.TemplateList); } else { templateMat = PartEditor.FindMaterialInList("FrostDragon.CoreParts.Template.Basic_Trail_NoCC", PartEditor.TemplateList); } } else if (fileInfo[0] == "flagtrail") { textureName += "_FlagTrail"; matType = MaterialInfo.MaterialType.Trail; if (customColor) { templateMat = PartEditor.FindMaterialInList("FrostDragon.CoreParts.Template.Flag_Trail_CC", PartEditor.TemplateList); } else { templateMat = PartEditor.FindMaterialInList("FrostDragon.CoreParts.Template.Flag_Trail_NoCC", PartEditor.TemplateList); } } else { Logger.log.Debug("Invalid texture name " + fileName + ", please refer to user guide for texture naming guidelines"); continue; } GenerateUserMaterial(path, textureName, templateMat, customColor, matType); } //update labels UIFunctions.UpdateAllMatLabels(); }