コード例 #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 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);
        }
コード例 #3
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);
                    }
                }
            }
        }