コード例 #1
0
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            // Post import all assets if dependecies can be loaded
            List <string> toDelete = new List <string>();

            foreach (KeyValuePair <string, AAssetImporter> item in _importers)
            {
                if (item.Value.CanLoadDependencies())
                {
                    SmallLogger.Log(SmallLogger.LogType.AssetPostProcessor, "Post import asset: " + item.Key);
                    item.Value.OnPostImport(item.Key);
                    toDelete.Add(item.Key);
                }
            }

            // Delete imported assets
            foreach (string key in toDelete)
            {
                _importers.Remove(key);
            }

            SmallLogger.Log(SmallLogger.LogType.Dependency, _importers.Count + " asset(s) waiting for dependencies");
            foreach (KeyValuePair <string, AAssetImporter> item in _importers)
            {
                SmallLogger.Log(SmallLogger.LogType.Dependency, Path.GetFileName(item.Key) + " -> " + item.Value.ToString());
            }
        }
コード例 #2
0
        public static void CreatePrefabFromXml(string xmlPath)
        {
            XmlDocument xml = new XmlDocument();

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

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

            if (PrefabExists(fileName))
            {
                SmallLogger.Log(SmallLogger.LogType.PreImport, "Prefab '" + fileName + "' already exists.");
            }
            else
            {
                GameObject prefab = new GameObject();
                prefab.name = fileName;

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                SmallLogger.Log(SmallLogger.LogType.PreImport, "Creating prefab '" + fileName + "' from xml '" + xmlPath + "'");

                PrefabUtility.SaveAsPrefabAsset(prefab, fullPath);
                GameObject.DestroyImmediate(prefab);
            }

            // Update the created asset to ensure we trigger the 'OnPostprocessAllAssets' method
            AssetDatabase.ImportAsset(fullPath);
        }
コード例 #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;

            // 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);
        }
コード例 #4
0
        public static Shader GetShaderFromName(string shaderName)
        {
            Shader shader = Shader.Find(shaderName);

            if (shader == null)
            {
                SmallLogger.LogWarning(SmallLogger.LogType.PreImport, "The shader \"" + shaderName + "\" does not exists.");
            }
            return(shader);
        }
コード例 #5
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);
        }
コード例 #6
0
        public static Material CreateMaterialFromXml(string xmlPath)
        {
            XmlDocument xml = new XmlDocument();

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

            string fileName = Path.GetFileNameWithoutExtension(xmlPath);
            string path     = Path.Combine(root.SelectSingleNode("Path").InnerText, fileName + ".mat");

            // If the material does not exists, we create it
            bool     isCreating = false;
            Material material   = AssetDatabase.LoadAssetAtPath <Material>(path);

            if (material == null)
            {
                isCreating = true;
                material   = new Material(Shader.Find("Standard"));
                AssetDatabase.CreateAsset(material, path);
            }

            // Try to set the shader
            XmlNode shaderNode = root.SelectSingleNode("Shader");
            string  shaderName = "Standard";

            if (shaderNode != null)
            {
                shaderName = root.SelectSingleNode("Shader").InnerText;
            }
            else
            {
                SmallLogger.LogWarning(SmallLogger.LogType.PreImport, "Shader name not found in material '" + fileName + "'. The material is probably missing a group node.");
            }

            Shader shader = GetShaderFromName(shaderName);

            if (shader != null)
            {
                material.shader = shader;
                SmallLogger.Log(SmallLogger.LogType.PreImport, (isCreating ? "Creating" : "Loading") + " material '" + fileName + "' from xml '" + xmlPath + "' using shader '" + shaderName + "'");
            }
            else
            {
                SmallLogger.Log(SmallLogger.LogType.PreImport, (isCreating ? "Creating" : "Loading") + " material '" + fileName + "'from xml '" + xmlPath + "'. Shader name is invalid '" + shaderName + "'");
            }

            return(material);
        }
コード例 #7
0
        void OnGUI()
        {
            GUILayout.Label("Small Importer:", EditorStyles.boldLabel);
            prefixPrefab = EditorGUILayout.TextField("Prefix Identification", prefixPrefab);
            logMask      = (SmallLogger.LogType)EditorGUILayout.MaskField("Log", (int)logMask, Enum.GetNames(typeof(SmallLogger.LogType)));

            if (GUILayout.Button("Refresh SUBlime"))
            {
                SmallLogger.Log(SmallLogger.LogType.Debug, "Sublime refreshed.");
                SmallAssetPostprocessor.Reset();
            }

            // Save in EditorPlayerPrefs
            EditorPrefs.SetString("SBI_prefixPrefab", prefixPrefab);
            EditorPrefs.SetInt("SBI_log", (int)logMask);
        }
コード例 #8
0
        public void AddDependency <T>(string assetPath)
        {
            bool            duplicate     = false;
            AssetDependency newDependency = new AssetDependency(assetPath, typeof(T));

            foreach (AssetDependency dependency in _dependencies)
            {
                if (dependency.IsEqual(newDependency))
                {
                    duplicate = true;
                }
            }

            if (!duplicate)
            {
                SmallLogger.Log(SmallLogger.LogType.Dependency, "Add dependency '" + newDependency.ToString() + "' for asset '" + System.IO.Path.GetFileName(assetPath) + "'");
                _dependencies.Add(newDependency);
            }
        }
コード例 #9
0
        void OnPreprocessAsset()
        {
            // During this phase we create the needed assets without linking them together
            AAssetImporter importer = GetAssetImporter(assetPath);

            if (importer != null)
            {
                if (!_importers.ContainsKey(assetPath))
                {
                    importer.CreateDependencies(assetPath);
                    SmallLogger.Log(SmallLogger.LogType.AssetPostProcessor, "Importing asset: " + Path.GetFileName(assetPath) + " with " + importer.DependencyCount + (importer.DependencyCount == 1 ? " dependency" : " dependencies"));
                    importer.OnPreImport(assetPath);
                    _importers.Add(assetPath, importer);
                }
                else
                {
                    SmallLogger.LogError(SmallLogger.LogType.AssetPostProcessor, "Asset already in the list : " + assetPath);
                }
            }
        }
コード例 #10
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);
        }
コード例 #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;

            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);
        }