Esempio n. 1
0
        private void CreateMaterialsFromInternalTextures(XDocument xml)
        {
            var texData = xml.Root.Elements("InternalTexture");

            foreach (var tex in texData)
            {
                string texAssetPath = tex.Attribute("assetPath").Value;
                string materialPath = ImportUtils.GetMaterialPath(texAssetPath);

                Material material = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;
                if (material == null)
                {
                    // Create our material
                    material = new Material(Shader.Find("Tiled/TextureTintSnap"));

                    // Assign to it the texture that is already internal to our Unity project
                    Texture2D texture2d = AssetDatabase.LoadAssetAtPath(texAssetPath, typeof(Texture2D)) as Texture2D;
                    material.SetTexture("_MainTex", texture2d);

                    // Write the material to our asset database
                    ImportUtils.ReadyToWrite(materialPath);
                    AssetDatabase.CreateAsset(material, materialPath);
                }
            }
        }
        public void TextureImported(string texturePath)
        {
            // This is fixup method due to materials and textures, under some conditions, being imported out of order
            Texture2D texture2d = AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)) as Texture2D;
            Material  material  = AssetDatabase.LoadAssetAtPath(ImportUtils.GetMaterialPath(texturePath), typeof(Material)) as Material;

            material.SetTexture("_MainTex", texture2d);
        }
Esempio n. 3
0
        private void ImportTexturesFromXml(XDocument xml)
        {
            var texData = xml.Root.Elements("ImportTexture");

            foreach (var tex in texData)
            {
                string name = tex.Attribute("filename").Value;
                string data = tex.Value;

                // The data is gzip compressed base64 string. We need the raw bytes.
                //byte[] bytes = ImportUtils.GzipBase64ToBytes(data);
                byte[] bytes = ImportUtils.Base64ToBytes(data);

                // Save and import the texture asset
                {
                    string pathToSave = ImportUtils.GetTexturePath(name);
                    ImportUtils.ReadyToWrite(pathToSave);
                    File.WriteAllBytes(pathToSave, bytes);
                    AssetDatabase.ImportAsset(pathToSave, ImportAssetOptions.ForceSynchronousImport);
                }

                // Create a material if needed in prepartion for the texture being successfully imported
                {
                    string   materialPath = ImportUtils.GetMaterialPath(name);
                    Material material     = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;
                    if (material == null)
                    {
                        // We need to create the material afterall
                        // Use our custom shader
                        material = new Material(Shader.Find("Tiled/TextureTintSnap"));
                        ImportUtils.ReadyToWrite(materialPath);
                        AssetDatabase.CreateAsset(material, materialPath);
                    }
                }
            }
        }
        // We need to call this while the renderers on the model is having its material assigned to it
        public Material FixMaterialForMeshRenderer(string objName, Renderer renderer)
        {
            string xmlPath = ImportUtils.GetXmlPath(objName);

            XDocument xml = XDocument.Load(xmlPath);

            // The mesh to match
            string meshName = renderer.name;

            // The mesh name may be decorated by Unity
            string pattern = @"_MeshPart[\d]$";
            Regex  regex   = new Regex(pattern);

            meshName = regex.Replace(meshName, "");

            var assignMaterials = xml.Root.Elements("AssignMaterial");

            // Find an assignment that matches the mesh renderer
            XElement match = assignMaterials.FirstOrDefault(el => el.Attribute("mesh").Value == meshName);

            if (match == null)
            {
                // The names of our meshes in the AssignMaterials elements may be wrong
                // This happened before when Unity replaced whitespace with underscore in our named meshes
                // That case is handled now, but there may be others
                StringBuilder builder = new StringBuilder();
                builder.AppendFormat("Could not find mesh named '{0}' for material matching\n", renderer.name);
                string choices = String.Join("\n  ", assignMaterials.Select(m => m.Attribute("mesh").Value).ToArray());
                builder.AppendFormat("Choices are:\n  {0}", choices);

                Debug.LogError(builder.ToString());
                return(null);
            }

            string materialName = match.Attribute("material").Value;
            string materialPath = ImportUtils.GetMaterialPath(materialName);

            // Assign the material
            renderer.sharedMaterial = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;

            // Set the sorting layer for the mesh
            string sortingLayer = match.Attribute("sortingLayerName").Value;

            if (!String.IsNullOrEmpty(sortingLayer) && !SortingLayerExposedEditor.GetSortingLayerNames().Contains(sortingLayer))
            {
                Debug.LogError(string.Format("Sorting Layer \"{0}\" does not exist. Check your Project Settings -> Tags and Layers", sortingLayer));
                renderer.sortingLayerName = "Default";
            }
            else
            {
                renderer.sortingLayerName = sortingLayer;
            }

            // Set the sorting order
            renderer.sortingOrder = ImportUtils.GetAttributeAsInt(match, "sortingOrder");

            // Do we have an alpha color key?
            string htmlColor = ImportUtils.GetAttributeAsString(match, "alphaColorKey", "");

            if (!String.IsNullOrEmpty(htmlColor))
            {
                // Take for granted color is in the form '#RRGGBB'
                byte  r     = byte.Parse(htmlColor.Substring(1, 2), System.Globalization.NumberStyles.HexNumber);
                byte  g     = byte.Parse(htmlColor.Substring(3, 2), System.Globalization.NumberStyles.HexNumber);
                byte  b     = byte.Parse(htmlColor.Substring(5, 2), System.Globalization.NumberStyles.HexNumber);
                Color color = new Color32(r, g, b, 255);
                renderer.sharedMaterial.SetColor("_AlphaColorKey", color);
            }

            return(renderer.sharedMaterial);
        }