Пример #1
0
        public override void OnPostImport(string assetPath)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(assetPath);
            XmlNode root = doc.DocumentElement;

            string prefabPath = root.SelectSingleNode("Path").InnerText;
            string fileName   = Path.GetFileNameWithoutExtension(assetPath);
            string fullPath   = Path.Combine(prefabPath, fileName + ".prefab");

            // Load the prefab asset
            GameObject prefab = AssetDatabase.LoadMainAssetAtPath(fullPath) as GameObject;

            if (prefab == null)
            {
                SmallLogger.LogWarning(SmallLogger.LogType.PostImport, "There is no prefab at path " + fullPath);
                return;
            }
            GameObject prefabInstance = PrefabUtility.InstantiatePrefab(prefab) as GameObject;

            // Load and set children
            SmallParserUtils.RecursiveParseTransformXml(root, prefabInstance);

            // Save prefab asset
            PrefabUtility.RecordPrefabInstancePropertyModifications(prefabInstance.GetComponent <Transform>());
            PrefabUtility.ApplyPrefabInstance(prefabInstance, InteractionMode.AutomatedAction);

            // Clean up
            GameObject.DestroyImmediate(prefabInstance);

            // Force Unity to update the asset, without this we have to manually reload unity (by losing and gaining focus on the editor)
            AssetDatabase.ImportAsset(fullPath);
        }
Пример #2
0
        public bool SetColor(XmlNode channels, string valueName)
        {
            XmlNode nodeXml = channels.SelectSingleNode(valueName);

            if (nodeXml != null)
            {
                Color color = SmallParserUtils.ParseColorXml(nodeXml.InnerText);
                _material.SetColor(valueName, color);
                return(color != Color.black);
            }
            return(false);
        }
Пример #3
0
        public override void OnPostImport(string assetPath)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(assetPath);
            XmlNode root = doc.DocumentElement;

            string prefabPath = root.SelectSingleNode("Path").InnerText;
            string fileName   = Path.GetFileNameWithoutExtension(assetPath);
            string fullPath   = Path.Combine(prefabPath, fileName + ".prefab");

            // Load the prefab asset
            GameObject prefab = AssetDatabase.LoadMainAssetAtPath(fullPath) as GameObject;

            if (prefab == null)
            {
                SmallLogger.LogWarning(SmallLogger.LogType.PostImport, "There is no prefab at path " + fullPath);
                return;
            }
            GameObject prefabInstance = PrefabUtility.InstantiatePrefab(prefab) as GameObject;

            Camera camera = prefabInstance.GetOrAddComponent <Camera>();

            string projection = root.SelectSingleNode("Projection").InnerText;

            camera.orthographic = (projection != "PERSP");

            float fov = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Fov").InnerText);

            camera.fieldOfView = (Mathf.Atan(Mathf.Tan(Mathf.Deg2Rad * fov / 2.0f) / camera.aspect) * 2.0f) * Mathf.Rad2Deg;

            float near = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Near").InnerText);

            camera.nearClipPlane = near;

            float far = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Far").InnerText);

            camera.farClipPlane = far;

            float size = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Size").InnerText);

            camera.orthographicSize = size * 0.28f;

            // Save prefab asset
            PrefabUtility.RecordPrefabInstancePropertyModifications(camera);
            PrefabUtility.ApplyPrefabInstance(prefabInstance, InteractionMode.AutomatedAction);

            // Clean up
            GameObject.DestroyImmediate(prefabInstance);

            // Force Unity to update the asset, without this we have to manually reload unity (by losing and gaining focus on the editor)
            AssetDatabase.ImportAsset(fullPath);
        }
Пример #4
0
        public bool SetVector(XmlNode channels, string valueName)
        {
            XmlNode nodeXml = channels.SelectSingleNode(valueName);

            if (nodeXml != null)
            {
                Vector3 vector = SmallParserUtils.ParseVectorXml(nodeXml.InnerText);
                _material.SetVector(valueName, vector);
                return(true);
            }
            return(false);
        }
Пример #5
0
        public bool SetTexture(XmlNode channels, string textureName)
        {
            XmlNode nodeXml = channels.SelectSingleNode(textureName);

            if (nodeXml != null)
            {
                Texture texture = SmallParserUtils.ParseTextureXml(nodeXml.InnerText);
                _material.SetTexture(textureName, texture);
                return(texture != null);
            }
            return(false);
        }
Пример #6
0
        public bool SetFloat(XmlNode channels, string valueName)
        {
            XmlNode nodeXml = channels.SelectSingleNode(valueName);

            if (nodeXml != null)
            {
                float value = SmallParserUtils.ParseFloatXml(nodeXml.InnerText);
                _material.SetFloat(valueName, value);
                return(true);
            }
            return(false);
        }
Пример #7
0
        public static void ParseTransformXml(XmlNode node, GameObject gameObject, string type)
        {
            string location = node.SelectSingleNode("Position").InnerText;
            string rotation = node.SelectSingleNode("Rotation").InnerText;
            string scale    = node.SelectSingleNode("Scale").InnerText;
            string name     = node.SelectSingleNode("Name").InnerText;

            gameObject.name = name;
            gameObject.transform.localPosition = SmallParserUtils.ParseVectorXml(location);
            gameObject.transform.localScale    = SmallParserUtils.ParseVectorXml(scale);

            Vector3 rotationVector = SmallParserUtils.ParseVectorXml(rotation);

            gameObject.transform.rotation = new Quaternion();
            gameObject.transform.Rotate(new Vector3(rotationVector[0] * -1, 0, 0), Space.World);
            gameObject.transform.Rotate(new Vector3(0, rotationVector[2] * -1, 0), Space.World);
            gameObject.transform.Rotate(new Vector3(0, 0, rotationVector[1] * -1), Space.World);
        }
Пример #8
0
        public override void CreateDependencies(string assetPath)
        {
            XmlDocument xml = new XmlDocument();

            xml.Load(assetPath);
            XmlNode root = xml.DocumentElement;

            // Add textures dependencies
            XmlNode channels = root.SelectSingleNode("Channels");

            if (channels != null)
            {
                foreach (XmlNode node in channels.ChildNodes)
                {
                    if (node != null && SmallParserUtils.IsValidTextureXml(node.InnerText))
                    {
                        AddDependency <Texture>(SmallImporterUtils.GetTexturePath(node.InnerText));
                    }
                }
            }
        }
Пример #9
0
        public void SetTransparency(XmlNode channels, string propName)
        {
            XmlNode transparentXml = channels.SelectSingleNode("_Transparent");

            if (transparentXml != null)
            {
                string value     = transparentXml.InnerText;
                float  mode      = 0.0f;
                float  threshold = 0.5f;
                if (value == "OPAQUE")
                {
                    mode = 0.0f;
                }
                else if (value == "CLIP")
                {
                    mode = 1.0f;
                    XmlNode clipThresholdXml = channels.SelectSingleNode("_Cutoff");
                    if (clipThresholdXml != null)
                    {
                        threshold = SmallParserUtils.ParseFloatXml(clipThresholdXml.InnerText);
                    }
                }
                else if (value == "BLEND")
                {
                    mode = 2.0f;
                }

                // _Mode is for standard shaders
                _material.SetFloat("_Mode", mode);

                // Surface is for URP
                // Set surface to 1 if transparent
                _material.SetFloat("_Surface", mode > 0.0f ? 1.0f : 0.0f);
                _material.SetFloat("_AlphaClip", mode == 1.0f ? 1.0f : 0.0f);
                _material.SetFloat("_Cutoff", threshold);
            }
        }
Пример #10
0
        public void SetLightmap(XmlNode channels, string propName)
        {
            // Tile and lightmap data
            XmlNode tileMaxXml = channels.SelectSingleNode("_TileMax");

            if (tileMaxXml != null)
            {
                float tileMax   = SmallParserUtils.ParseFloatXml(tileMaxXml.InnerText);
                float tileScale = 1.0f / tileMax;
                _material.SetFloat("_TileScale", tileScale);

                float tileX = SmallParserUtils.ParseFloatXml(channels.SelectSingleNode("_TileX").InnerText);
                float tileY = SmallParserUtils.ParseFloatXml(channels.SelectSingleNode("_TileY").InnerText);
                _material.SetVector("_Offset", new Vector4(tileX * tileScale, tileY * tileScale));

                // Lightmap
                XmlNode lightmapXml = channels.SelectSingleNode("_Lightmap");
                if (lightmapXml != null)
                {
                    Texture texture = SmallParserUtils.ParseTextureXml(lightmapXml.InnerText);
                    _material.SetTexture("_Lightmap", texture);
                }
            }
        }
Пример #11
0
        public override void OnPostImport(string assetPath)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(assetPath);
            XmlNode root = doc.DocumentElement;

            string prefabPath = root.SelectSingleNode("Path").InnerText;
            string fileName   = Path.GetFileNameWithoutExtension(assetPath);
            string fullPath   = Path.Combine(prefabPath, fileName + ".prefab");

            // Load the prefab asset
            GameObject prefab = AssetDatabase.LoadMainAssetAtPath(fullPath) as GameObject;

            if (prefab == null)
            {
                SmallLogger.LogWarning(SmallLogger.LogType.PostImport, "There is no prefab at path " + fullPath);
                return;
            }
            GameObject prefabInstance = PrefabUtility.InstantiatePrefab(prefab) as GameObject;

            // Load and assign the mesh
            string     meshPath   = root.SelectSingleNode("Model").InnerText + ".fbx";
            Mesh       mesh       = AssetDatabase.LoadAssetAtPath <Mesh>(meshPath);
            MeshFilter meshFilter = prefabInstance.GetOrAddComponent <MeshFilter>();

            meshFilter.mesh = mesh;
            PrefabUtility.RecordPrefabInstancePropertyModifications(meshFilter);

            // Load and assign materials
            MeshRenderer renderer      = prefabInstance.GetOrAddComponent <MeshRenderer>();
            XmlNodeList  materialsNode = root.SelectSingleNode("Materials").ChildNodes;

            if (materialsNode.Count != 0)
            {
                Material[] materials = new Material[materialsNode.Count];
                for (int i = 0; i < materialsNode.Count; i++)
                {
                    string path         = materialsNode[i].SelectSingleNode("Path").InnerText;
                    string name         = materialsNode[i].SelectSingleNode("Name").InnerText;
                    string materialPath = Path.Combine(path, name + ".mat");

                    materials[i] = AssetDatabase.LoadAssetAtPath <Material>(materialPath);
                }

                renderer.sharedMaterials = materials;
                PrefabUtility.RecordPrefabInstancePropertyModifications(renderer);
            }

            // Load and set children
            SmallParserUtils.RecursiveParseTransformXml(root, prefabInstance);

            // Save prefab asset
            PrefabUtility.RecordPrefabInstancePropertyModifications(prefabInstance.GetComponent <Transform>());
            PrefabUtility.ApplyPrefabInstance(prefabInstance, InteractionMode.AutomatedAction);

            // Clean up
            GameObject.DestroyImmediate(prefabInstance);

            // Force Unity to update the asset, without this we have to manually reload unity (by losing and gaining focus on the editor)
            AssetDatabase.ImportAsset(fullPath);
        }
Пример #12
0
        public static void RecursiveParseTransformXml(XmlNode root, GameObject parent)
        {
            XmlNode childrenNode = root.SelectSingleNode("Children");

            if (childrenNode != null)
            {
                XmlNodeList childrenNodeList = childrenNode.ChildNodes;
                for (int i = 0; i < childrenNodeList.Count; i++)
                {
                    string type = childrenNodeList[i].SelectSingleNode("Type").InnerText;
                    string name = childrenNodeList[i].SelectSingleNode("Name").InnerText;

                    GameObject gameObject = parent.transform.Find(name)?.gameObject;
                    if (gameObject == null)
                    {
                        if (type == "MESH")
                        {
                            string     path  = childrenNodeList[i].SelectSingleNode("Prefab").InnerText;
                            GameObject child = AssetDatabase.LoadMainAssetAtPath(path) as GameObject;
                            gameObject = PrefabUtility.InstantiatePrefab(child) as GameObject;

                            if (gameObject != null)
                            {
                                string isStatic = childrenNodeList[i].SelectSingleNode("Static").InnerText;
                                gameObject.isStatic = (isStatic == "Static");

                                // Check if Layer is Valid and Set
                                string layer = childrenNodeList[i].SelectSingleNode("Layer").InnerText;
                                if (layer != "")
                                {
                                    int layeridx = LayerMask.NameToLayer(layer);
                                    gameObject.layer = ((layeridx >= 0) ? layeridx : 0);
                                }

                                // Check if Tag is Valid and Set
                                string tag = childrenNodeList[i].SelectSingleNode("Tag").InnerText;
                                if (tag != "")
                                {
                                    for (int j = 0; j < UnityEditorInternal.InternalEditorUtility.tags.Length; j++)
                                    {
                                        if (UnityEditorInternal.InternalEditorUtility.tags[j].Contains(tag))
                                        {
                                            gameObject.tag = tag;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        else if (type == "LIGHT" || type == "CAMERA")
                        {
                            string     path  = childrenNodeList[i].SelectSingleNode("Prefab").InnerText;
                            GameObject child = AssetDatabase.LoadMainAssetAtPath(path) as GameObject;
                            gameObject = PrefabUtility.InstantiatePrefab(child) as GameObject;
                        }
                        else if (type == "EMPTY")
                        {
                            gameObject = new GameObject();
                        }
                    }

                    if (gameObject != null)
                    {
                        SmallParserUtils.ParseTransformXml(childrenNodeList[i], gameObject, type);
                        gameObject.GetComponent <Transform>().SetParent(parent.GetComponent <Transform>(), false);
                        PrefabUtility.RecordPrefabInstancePropertyModifications(gameObject.GetComponent <Transform>());

                        SmallParserUtils.RecursiveParseTransformXml(childrenNodeList[i], gameObject);
                    }
                }
            }
        }
Пример #13
0
        public override void OnPostImport(string assetPath)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(assetPath);
            XmlNode root = doc.DocumentElement;

            string prefabPath = root.SelectSingleNode("Path").InnerText;
            string fileName   = Path.GetFileNameWithoutExtension(assetPath);
            string fullPath   = Path.Combine(prefabPath, fileName + ".prefab");

            // Load the prefab asset
            GameObject prefab = AssetDatabase.LoadMainAssetAtPath(fullPath) as GameObject;

            if (prefab == null)
            {
                SmallLogger.LogWarning(SmallLogger.LogType.PostImport, "There is no prefab at path " + fullPath);
                return;
            }
            GameObject prefabInstance = PrefabUtility.InstantiatePrefab(prefab) as GameObject;

            Light light = prefabInstance.GetOrAddComponent <Light>();

            // Light type
            string type = root.SelectSingleNode("Type").InnerText;

            if (type == "POINT")
            {
                light.type  = LightType.Point;
                light.range = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Radius").InnerText);
            }
            else if (type == "SPOT")
            {
                light.type      = LightType.Spot;
                light.spotAngle = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("SpotSize").InnerText);
                light.range     = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Radius").InnerText);
            }
            else if (type == "SUN")
            {
                light.type = LightType.Directional;
            }
            else if (type == "AREA")
            {
                string shape = root.SelectSingleNode("Shape").InnerText;
                if (shape == "RECTANGLE")
                {
                    light.type = LightType.Rectangle;
                    float width  = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Width").InnerText);
                    float height = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Height").InnerText);
                    light.areaSize = new Vector2(width, height);
                }
                else if (shape == "DISC")
                {
                    light.type = LightType.Disc;
                    float radius = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Radius").InnerText);
                    light.areaSize = new Vector2(radius, radius);
                }
            }

            // Light color
            light.color     = SmallParserUtils.ParseColorXml(root.SelectSingleNode("Color").InnerText);
            light.intensity = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Power").InnerText) / 100.0f;

            // Save prefab asset
            PrefabUtility.RecordPrefabInstancePropertyModifications(light);
            PrefabUtility.ApplyPrefabInstance(prefabInstance, InteractionMode.AutomatedAction);

            // Clean up
            GameObject.DestroyImmediate(prefabInstance);

            // Force Unity to update the asset, without this we have to manually reload unity (by losing and gaining focus on the editor)
            AssetDatabase.ImportAsset(fullPath);
        }