Material SetupMaterial(GameObject gameObject) { ZGONMeshStringProperty mat = (ZGONMeshStringProperty)SearchMeshProps(ZGONAssetProperties.matName); string matPath = _meshsetRootDir + _matDirName + "/" + mat.Value + ".mat"; // Check to see if the material already exists Material oldMat = (Material)AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)); if (oldMat == null) { // Create a new material from props in XML file ZGONMeshShaderProperty matShader = (ZGONMeshShaderProperty)SearchMeshProps(ZGONAssetProperties.matShaderName); Material newMaterial = new Material(Shader.Find(matShader.Value)); foreach (ZGONMeshProperty meshProp in _meshProps) { if (meshProp is ZGONMeshTextureProperty) { ZGONMeshTextureProperty texProp = (ZGONMeshTextureProperty)meshProp; newMaterial.SetTexture(texProp.Parameter, texProp.Value); } if (meshProp is ZGONMeshFloatProperty) { ZGONMeshFloatProperty floatProp = (ZGONMeshFloatProperty)meshProp; switch (floatProp.Name) { case "MatShine": newMaterial.SetFloat("_Shininess", floatProp.Value); break; case "MatBodyOcclusionPower": newMaterial.SetFloat("_BodyOcclusionPower", floatProp.Value); break; case "MatBodyNormalPower": newMaterial.SetFloat("_BodyBumpPower", floatProp.Value); break; case "MatNormalPower": newMaterial.SetFloat("_BumpPower", floatProp.Value); break; case "MatDetailNormalPower": newMaterial.SetFloat("_BumpDetailPower", floatProp.Value); break; case "MatDetailTexOffset": newMaterial.SetFloat("_DetailTexOffset", floatProp.Value); break; case "MatDetailTexScale": newMaterial.SetFloat("_DetailTexScale", floatProp.Value); break; case "MatDetailTexPower": newMaterial.SetFloat("_DetailTexPower", floatProp.Value); break; case "UvOffsetY": newMaterial.SetFloat("_AtlasOffsetY", floatProp.Value); break; case "UvOffsetX": newMaterial.SetFloat("_AtlasOffsetX", floatProp.Value); break; case "UvScaleY": newMaterial.SetFloat("_AtlasScaleY", floatProp.Value); break; case "UvScaleX": newMaterial.SetFloat("_AtlasScaleX", floatProp.Value); break; } } } // Create and set the new material AssetDatabase.CreateAsset(newMaterial, matPath); gameObject.GetComponent <MeshRenderer>().material = newMaterial; return(newMaterial); } else { gameObject.GetComponent <MeshRenderer>().material = oldMat; } return(oldMat); }
void ParseXMLData() { if (_meshsetName != _lastMeshsetName) { _meshsetXml.Load(_meshsetRootDir + "/" + _meshsetName + ".xml"); _lastMeshsetName = _meshsetName; } // Find the imported mesh in the XML file XmlNodeList nodeList = _meshsetXml.GetElementsByTagName(_assetName); IEnumerator enumNodeList = nodeList[0].GetEnumerator(); while (enumNodeList.MoveNext()) { XmlNode currentNode = (XmlNode) enumNodeList.Current; XmlNode attribTypeNode = currentNode.Attributes.GetNamedItem("type"); string typeValue = attribTypeNode.Value; if (typeValue == "string") { ZGONMeshStringProperty meshProp = new ZGONMeshStringProperty(); meshProp.Name = currentNode.Name; meshProp.Value = currentNode.Attributes.GetNamedItem("value").Value; _meshProps.Add(meshProp); } else if (typeValue == "bool") { ZGONMeshBoolProperty meshProp = new ZGONMeshBoolProperty(); meshProp.Name = currentNode.Name; meshProp.Value = Convert.ToBoolean(currentNode.Attributes.GetNamedItem("value").Value); _meshProps.Add(meshProp); } else if (typeValue == "float") { ZGONMeshFloatProperty meshProp = new ZGONMeshFloatProperty(); meshProp.Name = currentNode.Name; meshProp.Value = Convert.ToSingle(currentNode.Attributes.GetNamedItem("value").Value); _meshProps.Add(meshProp); } else if (typeValue == "color") { ZGONMeshColorProperty meshProp = new ZGONMeshColorProperty(); meshProp.Name = currentNode.Name; Color colorValue = new Color(); colorValue.r = Convert.ToSingle(currentNode.Attributes.GetNamedItem("r").Value); colorValue.g = Convert.ToSingle(currentNode.Attributes.GetNamedItem("g").Value); colorValue.b = Convert.ToSingle(currentNode.Attributes.GetNamedItem("b").Value); meshProp.Value = colorValue; _meshProps.Add(meshProp); } else if (typeValue == "texture") { ZGONMeshTextureProperty meshProp = new ZGONMeshTextureProperty(); meshProp.Name = currentNode.Name; meshProp.Parameter = currentNode.Attributes.GetNamedItem("parameter").Value; string texPath = null; List<string> foundTextures = new List<string>(); string[] fileExtensions = new string[3] { "*.psd", "*.png", "*.tga" }; FindFilesInProject(fileExtensions, _meshsetRootDir, ref foundTextures); FindFilesInProject(fileExtensions, _sharedDir, ref foundTextures); FindFilesInProject(fileExtensions, _meshsetSharedDir, ref foundTextures); for (int i = 0; i < foundTextures.Count; i++) { foundTextures[i] = CleanFilePathString(foundTextures[i]); } foreach (string foundTex in foundTextures) { string texName = currentNode.Attributes.GetNamedItem("value").Value; if (texName == string.Empty) { texPath = "none"; continue; } if (foundTex.Contains(texName)) texPath = foundTex; } if (texPath != null) { meshProp.Value = (Texture)AssetDatabase.LoadAssetAtPath(texPath, typeof(Texture)); _meshProps.Add(meshProp); } else if (texPath == "none") Debug.Log(meshProp.Parameter + " : Has an empty texure reference."); else Debug.LogError("Did not find texture in project."); } else if (typeValue == "shader") { ZGONMeshShaderProperty meshProp = new ZGONMeshShaderProperty(); meshProp.Name = currentNode.Name; meshProp.Value = currentNode.Attributes.GetNamedItem("value").Value; _meshProps.Add(meshProp); } } }
void ParseXMLData() { if (_meshsetName != _lastMeshsetName) { _meshsetXml.Load(_meshsetRootDir + "/" + _meshsetName + ".xml"); _lastMeshsetName = _meshsetName; } // Find the imported mesh in the XML file XmlNodeList nodeList = _meshsetXml.GetElementsByTagName(_assetName); IEnumerator enumNodeList = nodeList[0].GetEnumerator(); while (enumNodeList.MoveNext()) { XmlNode currentNode = (XmlNode)enumNodeList.Current; XmlNode attribTypeNode = currentNode.Attributes.GetNamedItem("type"); string typeValue = attribTypeNode.Value; if (typeValue == "string") { ZGONMeshStringProperty meshProp = new ZGONMeshStringProperty(); meshProp.Name = currentNode.Name; meshProp.Value = currentNode.Attributes.GetNamedItem("value").Value; _meshProps.Add(meshProp); } else if (typeValue == "bool") { ZGONMeshBoolProperty meshProp = new ZGONMeshBoolProperty(); meshProp.Name = currentNode.Name; meshProp.Value = Convert.ToBoolean(currentNode.Attributes.GetNamedItem("value").Value); _meshProps.Add(meshProp); } else if (typeValue == "float") { ZGONMeshFloatProperty meshProp = new ZGONMeshFloatProperty(); meshProp.Name = currentNode.Name; meshProp.Value = Convert.ToSingle(currentNode.Attributes.GetNamedItem("value").Value); _meshProps.Add(meshProp); } else if (typeValue == "color") { ZGONMeshColorProperty meshProp = new ZGONMeshColorProperty(); meshProp.Name = currentNode.Name; Color colorValue = new Color(); colorValue.r = Convert.ToSingle(currentNode.Attributes.GetNamedItem("r").Value); colorValue.g = Convert.ToSingle(currentNode.Attributes.GetNamedItem("g").Value); colorValue.b = Convert.ToSingle(currentNode.Attributes.GetNamedItem("b").Value); meshProp.Value = colorValue; _meshProps.Add(meshProp); } else if (typeValue == "texture") { ZGONMeshTextureProperty meshProp = new ZGONMeshTextureProperty(); meshProp.Name = currentNode.Name; meshProp.Parameter = currentNode.Attributes.GetNamedItem("parameter").Value; string texPath = null; List <string> foundTextures = new List <string>(); string[] fileExtensions = new string[3] { "*.psd", "*.png", "*.tga" }; FindFilesInProject(fileExtensions, _meshsetRootDir, ref foundTextures); FindFilesInProject(fileExtensions, _sharedDir, ref foundTextures); FindFilesInProject(fileExtensions, _meshsetSharedDir, ref foundTextures); for (int i = 0; i < foundTextures.Count; i++) { foundTextures[i] = CleanFilePathString(foundTextures[i]); } foreach (string foundTex in foundTextures) { string texName = currentNode.Attributes.GetNamedItem("value").Value; if (texName == string.Empty) { texPath = "none"; continue; } if (foundTex.Contains(texName)) { texPath = foundTex; } } if (texPath != null) { meshProp.Value = (Texture)AssetDatabase.LoadAssetAtPath(texPath, typeof(Texture)); _meshProps.Add(meshProp); } else if (texPath == "none") { Debug.Log(meshProp.Parameter + " : Has an empty texure reference."); } else { Debug.LogError("Did not find texture in project."); } } else if (typeValue == "shader") { ZGONMeshShaderProperty meshProp = new ZGONMeshShaderProperty(); meshProp.Name = currentNode.Name; meshProp.Value = currentNode.Attributes.GetNamedItem("value").Value; _meshProps.Add(meshProp); } } }