Ejemplo n.º 1
0
        public void AddDependency(AssetImportContext context, string assetPath)
        {
            if (!m_AssetDependencies.Contains(assetPath))
            {
#if UNITY_2018_3_OR_NEWER
                context.DependsOnSourceAsset(assetPath);
#endif
                m_AssetDependencies.Add(assetPath);
            }
        }
Ejemplo n.º 2
0
        public override void OnImportAsset(ASSET_IMPORT_CONTEXT c)
        {
            var importer = new TemplateXImporter();
            var context  = new AssetImportContext(c.assetPath);

            importer.OnImportAsset(context);

            foreach (var obj in context.Objects)
            {
                c.AddObjectToAsset(obj.Identifier, obj.Object, obj.Icon);
            }

            c.SetMainObject(context.MainObject);

            foreach (var path in context.Dependencies)
            {
                c.DependsOnSourceAsset(path);
            }
        }
        static void RegisterDependencies(AssetImportContext importContext, ConversionDependencies dependencies)
        {
            using (var assets = dependencies.AssetDependentsByInstanceId.GetKeyArray(Allocator.Temp))
            {
                for (int i = 0; i < assets.Length; i++)
                {
                    var asset = EditorUtility.InstanceIDToObject(assets[i]);
                    if (asset == null)
                    {
                        var    dependents = FormatDependents(assets[i]);
                        string errorMsg   =
                            $"Invalid asset dependency on instance ID {assets[i]} - this instance ID does not correspond to an object.\n" +
                            "This dependency was registered by: " + dependents;
                        Debug.LogWarning(errorMsg);
                        continue;
                    }

                    var path = AssetDatabase.GetAssetPath(asset);
                    if (string.IsNullOrEmpty(path))
                    {
                        var    dependents = FormatDependents(assets[i]);
                        string errorMsg   =
                            $"Invalid asset dependency on object {asset.name}. This object does not have a valid asset path.\n" +
                            "This dependency was registered by: " + dependents;
                        Debug.LogWarning(errorMsg, asset);
                        continue;
                    }

                    var guid = new GUID(AssetDatabase.AssetPathToGUID(path));
                    if (GUIDHelper.IsBuiltin(in guid))
                    {
                        // AssetImportContext does not support dependencies on inbuilt assets
                        continue;
                    }

                    if (guid.Empty())
                    {
                        // This should never happen
                        var    dependents = FormatDependents(assets[i]);
                        string errorMsg   =
                            $"Invalid asset dependency on object {asset.name} at path {path}. It doesn't have a valid GUID.\n" +
                            "This dependency was registered by: " + dependents;
                        Debug.LogWarning(errorMsg, asset);
                        continue;
                    }

                    importContext.DependsOnSourceAsset(path);
                }
            }

            string FormatDependents(int assetInstance)
            {
                var    iter = dependencies.AssetDependentsByInstanceId.GetValuesForKey(assetInstance);
                string deps = "";

                while (iter.MoveNext())
                {
                    if (deps.Length > 0)
                    {
                        deps += ", ";
                    }
                    var obj = EditorUtility.InstanceIDToObject(iter.Current);
                    deps += $"{(obj == null ? "NULL" : obj.name)}";
                }

                return(deps);
            }
        }
Ejemplo n.º 4
0
        public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx)
        {
            var oldShader = AssetDatabase.LoadAssetAtPath <Shader>(ctx.assetPath);

            if (oldShader != null)
            {
                ShaderUtil.ClearShaderMessages(oldShader);
            }

            List <PropertyCollector.TextureInfo> configuredTextures;
            string path = ctx.assetPath;
            var    sourceAssetDependencyPaths = new List <string>();

            UnityEngine.Object mainObject;

            var       textGraph = File.ReadAllText(path, Encoding.UTF8);
            GraphData graph     = JsonUtility.FromJson <GraphData>(textGraph);

            graph.messageManager = new MessageManager();
            graph.assetGuid      = AssetDatabase.AssetPathToGUID(path);
            graph.OnEnable();
            graph.ValidateGraph();

            if (graph.outputNode is VfxMasterNode vfxMasterNode)
            {
                var vfxAsset = GenerateVfxShaderGraphAsset(vfxMasterNode);

                mainObject = vfxAsset;
            }
            else
            {
                var text   = GetShaderText(path, out configuredTextures, sourceAssetDependencyPaths, graph);
                var shader = ShaderUtil.CreateShaderAsset(text, false);

                if (graph != null && graph.messageManager.nodeMessagesChanged)
                {
                    foreach (var pair in graph.messageManager.GetNodeMessages())
                    {
                        var node = graph.GetNodeFromTempId(pair.Key);
                        MessageManager.Log(node, path, pair.Value.First(), shader);
                    }
                }

                EditorMaterialUtility.SetShaderDefaults(
                    shader,
                    configuredTextures.Where(x => x.modifiable).Select(x => x.name).ToArray(),
                    configuredTextures.Where(x => x.modifiable).Select(x => EditorUtility.InstanceIDToObject(x.textureId) as Texture).ToArray());
                EditorMaterialUtility.SetShaderNonModifiableDefaults(
                    shader,
                    configuredTextures.Where(x => !x.modifiable).Select(x => x.name).ToArray(),
                    configuredTextures.Where(x => !x.modifiable).Select(x => EditorUtility.InstanceIDToObject(x.textureId) as Texture).ToArray());

                mainObject = shader;
            }
            Texture2D texture = Resources.Load <Texture2D>("Icons/sg_graph_icon@64");

            ctx.AddObjectToAsset("MainAsset", mainObject, texture);
            ctx.SetMainObject(mainObject);

            var metadata = ScriptableObject.CreateInstance <ShaderGraphMetadata>();

            metadata.hideFlags = HideFlags.HideInHierarchy;
            if (graph != null)
            {
                metadata.outputNodeTypeName = graph.outputNode.GetType().FullName;
            }
            ctx.AddObjectToAsset("Metadata", metadata);

            foreach (var sourceAssetDependencyPath in sourceAssetDependencyPaths.Distinct())
            {
                // Ensure that dependency path is relative to project
                if (!sourceAssetDependencyPath.StartsWith("Packages/") && !sourceAssetDependencyPath.StartsWith("Assets/"))
                {
                    Debug.LogWarning($"Invalid dependency path: {sourceAssetDependencyPath}", mainObject);
                    continue;
                }

                ctx.DependsOnSourceAsset(sourceAssetDependencyPath);
            }
        }