public static void AddMaterialToList(PartBundleDescriptor partBundleDesc, MaterialDescriptor matDesc, Material mat) { PartEditor.MatList matList = null; switch (matDesc.materialType) { case (MaterialInfo.MaterialType.Glow): matList = PartEditor.GlowList; break; case (MaterialInfo.MaterialType.Secondary): matList = PartEditor.SecondaryList; break; case (MaterialInfo.MaterialType.Trail): matList = PartEditor.TrailList; break; case (MaterialInfo.MaterialType.NamePlate): matList = PartEditor.NamePlateList; break; case (MaterialInfo.MaterialType.Template): matList = PartEditor.TemplateList; break; } string trimmedName = mat.name.Replace(" (Instance)", ""); string matRefName = partBundleDesc.partBundleAuthor + "." + partBundleDesc.partBundleName + "." + matDesc.materialType.ToString() + "." + trimmedName; foreach (MaterialInfo listMatInfo in matList.mats) { if (listMatInfo.materialReferenceName == matRefName) { Logger.log.Debug("A material with the name " + listMatInfo.materialReferenceName + " has already been added - skipping. Make sure all material and bundle names are unique!"); return; } } MaterialInfo matInfo = new MaterialInfo(matDesc.materialType, mat, matRefName, matDesc.materialDisplayName, matDesc.supportsCustomColors); matList.mats.Add(matInfo); Logger.log.Debug("Added " + matRefName + " to the list of " + matInfo.materialType.ToString() + " materials"); }
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); } }