Exemplo n.º 1
0
        public void Init()
        {
            if (!Initialized)
            {
                if (ShowDebugMessages)
                {
                    Debug.Log("Initialize");
                }

                string templateMenuItems        = IOUtils.LoadTextFileFromDisk(AssetDatabase.GUIDToAssetPath(TemplateMenuItemsFileGUID));
                bool   refreshTemplateMenuItems = false;

                foreach (KeyValuePair <string, string> kvp in OfficialTemplates)
                {
                    if (!string.IsNullOrEmpty(AssetDatabase.GUIDToAssetPath(kvp.Key)))
                    {
                        TemplateMultiPass template = ScriptableObject.CreateInstance <TemplateMultiPass>();
                        template.Init(kvp.Value, kvp.Key, false);
                        AddTemplate(template);
                        if (!refreshTemplateMenuItems && templateMenuItems.IndexOf(kvp.Value) < 0)
                        {
                            refreshTemplateMenuItems = true;
                        }
                    }
                }

                // Search for other possible templates on the project
                string[] allShaders = AssetDatabase.FindAssets("t:shader");
                for (int i = 0; i < allShaders.Length; i++)
                {
                    if (!m_availableTemplates.ContainsKey(allShaders[i]))
                    {
                        CheckAndLoadTemplate(allShaders[i]);
                    }
                }

                // TODO: Sort list alphabeticaly
                AvailableTemplateNames    = new string[m_sortedTemplates.Count + 1];
                AvailableTemplateNames[0] = "Custom";
                for (int i = 0; i < m_sortedTemplates.Count; i++)
                {
                    m_sortedTemplates[i].OrderId  = i;
                    AvailableTemplateNames[i + 1] = m_sortedTemplates[i].Name;
                }

                if (refreshTemplateMenuItems)
                {
                    CreateTemplateMenuItems();
                }

                Initialized = true;
            }
        }
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            TemplatesManager templatesManager;
            bool             firstTimeDummyFlag = false;

            if (UIUtils.CurrentWindow == null)
            {
                if (DummyManager == null)
                {
                    DummyManager           = ScriptableObject.CreateInstance <TemplatesManager>();
                    DummyManager.hideFlags = HideFlags.HideAndDontSave;
                    firstTimeDummyFlag     = true;
                }
                templatesManager = DummyManager;
            }
            else
            {
                Destroy();
                templatesManager = UIUtils.CurrentWindow.TemplatesManagerInstance;
            }

            if (templatesManager == null)
            {
                return;
            }

            if (!templatesManager.Initialized)
            {
                templatesManager.Init();
            }

            bool refreshMenuItems = false;

            for (int i = 0; i < importedAssets.Length; i++)
            {
                if (TemplateHelperFunctions.CheckIfTemplate(importedAssets[i]))
                {
                    string             guid         = AssetDatabase.AssetPathToGUID(importedAssets[i]);
                    TemplateDataParent templateData = templatesManager.GetTemplate(guid);
                    if (templateData != null)
                    {
                        refreshMenuItems = templateData.Reload() || refreshMenuItems || firstTimeDummyFlag;
                        int windowCount = IOUtils.AllOpenedWindows.Count;
                        AmplifyShaderEditorWindow currWindow = UIUtils.CurrentWindow;
                        for (int windowIdx = 0; windowIdx < windowCount; windowIdx++)
                        {
                            if (IOUtils.AllOpenedWindows[windowIdx].OutsideGraph.CurrentCanvasMode == NodeAvailability.TemplateShader)
                            {
                                if (IOUtils.AllOpenedWindows[windowIdx].OutsideGraph.MultiPassMasterNodes.NodesList[0].CurrentTemplate == templateData)
                                {
                                    UIUtils.CurrentWindow = IOUtils.AllOpenedWindows[windowIdx];
                                    IOUtils.AllOpenedWindows[windowIdx].OutsideGraph.ForceMultiPassMasterNodesRefresh();
                                }
                            }
                        }
                        UIUtils.CurrentWindow = currWindow;
                    }
                    else
                    {
                        refreshMenuItems = true;
                        string            name = TemplatesManager.OfficialTemplates.ContainsKey(guid) ? TemplatesManager.OfficialTemplates[guid] : string.Empty;
                        TemplateMultiPass mp   = TemplateMultiPass.CreateInstance <TemplateMultiPass>();
                        mp.Init(name, guid, true);
                        templatesManager.AddTemplate(mp);
                    }
                }
            }

            if (deletedAssets.Length > 0)
            {
                if (deletedAssets[0].IndexOf(Constants.InvalidPostProcessDatapath) < 0)
                {
                    for (int i = 0; i < deletedAssets.Length; i++)
                    {
                        string             guid         = AssetDatabase.AssetPathToGUID(deletedAssets[i]);
                        TemplateDataParent templateData = templatesManager.GetTemplate(guid);
                        if (templateData != null)
                        {
                            // Close any window using that template
                            int windowCount = IOUtils.AllOpenedWindows.Count;
                            for (int windowIdx = 0; windowIdx < windowCount; windowIdx++)
                            {
                                TemplateMasterNode masterNode = IOUtils.AllOpenedWindows[windowIdx].CurrentGraph.CurrentMasterNode as TemplateMasterNode;
                                if (masterNode != null && masterNode.CurrentTemplate.GUID.Equals(templateData.GUID))
                                {
                                    IOUtils.AllOpenedWindows[windowIdx].Close();
                                }
                            }

                            templatesManager.RemoveTemplate(templateData);
                            refreshMenuItems = true;
                        }
                    }
                }
            }

            //for ( int i = 0; i < movedAssets.Length; i++ )
            //{
            //	if ( TemplateHelperFunctions.CheckIfTemplate( movedAssets[ i ] ) )
            //	{
            //		refreshMenuItems = true;
            //		break;
            //	}
            //}

            //for ( int i = 0; i < movedFromAssetPaths.Length; i++ )
            //{
            //	if ( TemplateHelperFunctions.CheckIfTemplate( movedFromAssetPaths[ i ] ) )
            //	{
            //		refreshMenuItems = true;
            //		break;
            //	}
            //}

            if (refreshMenuItems)
            {
                //UnityEngine.Debug.Log( "Refresh Menu Items" );
                refreshMenuItems = false;
                templatesManager.CreateTemplateMenuItems();

                AmplifyShaderEditorWindow currWindow = UIUtils.CurrentWindow;

                int windowCount = IOUtils.AllOpenedWindows.Count;
                for (int windowIdx = 0; windowIdx < windowCount; windowIdx++)
                {
                    UIUtils.CurrentWindow = IOUtils.AllOpenedWindows[windowIdx];
                    IOUtils.AllOpenedWindows[windowIdx].CurrentGraph.ForceCategoryRefresh();
                }
                UIUtils.CurrentWindow = currWindow;
            }
        }
Exemplo n.º 3
0
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            if (UIUtils.CurrentWindow == null)
            {
                return;
            }

            TemplatesManager templatesManager = UIUtils.CurrentWindow.TemplatesManagerInstance;

            if (templatesManager == null)
            {
                return;
            }

            if (!templatesManager.Initialized)
            {
                templatesManager.Init();
            }

            bool refreshMenuItems = false;

            for (int i = 0; i < importedAssets.Length; i++)
            {
                if (TemplateHelperFunctions.CheckIfTemplate(importedAssets[i]))
                {
                    string             guid         = AssetDatabase.AssetPathToGUID(importedAssets[i]);
                    TemplateDataParent templateData = templatesManager.GetTemplate(guid);
                    if (templateData != null)
                    {
                        refreshMenuItems = templateData.Reload() || refreshMenuItems;
                    }
                    else
                    {
                        refreshMenuItems = true;
                        string            name = TemplatesManager.OfficialTemplates.ContainsKey(guid) ? TemplatesManager.OfficialTemplates[guid] : string.Empty;
                        TemplateMultiPass mp   = TemplateMultiPass.CreateInstance <TemplateMultiPass>();
                        mp.Init(name, guid, true);
                        templatesManager.AddTemplate(mp);
                    }
                }
            }

            if (deletedAssets.Length > 0)
            {
                if (deletedAssets[0].IndexOf(Constants.InvalidPostProcessDatapath) < 0)
                {
                    for (int i = 0; i < deletedAssets.Length; i++)
                    {
                        string             guid         = AssetDatabase.AssetPathToGUID(deletedAssets[i]);
                        TemplateDataParent templateData = templatesManager.GetTemplate(guid);
                        if (templateData != null)
                        {
                            // Close any window using that template
                            int windowCount = IOUtils.AllOpenedWindows.Count;
                            for (int windowIdx = 0; windowIdx < windowCount; windowIdx++)
                            {
                                TemplateMasterNode masterNode = IOUtils.AllOpenedWindows[windowIdx].CurrentGraph.CurrentMasterNode as TemplateMasterNode;
                                if (masterNode != null && masterNode.CurrentTemplate.GUID.Equals(templateData.GUID))
                                {
                                    IOUtils.AllOpenedWindows[windowIdx].Close();
                                }
                            }

                            templatesManager.RemoveTemplate(templateData);
                            refreshMenuItems = true;
                        }
                    }
                }
            }

            //for ( int i = 0; i < movedAssets.Length; i++ )
            //{
            //	if ( TemplateHelperFunctions.CheckIfTemplate( movedAssets[ i ] ) )
            //	{
            //		refreshMenuItems = true;
            //		break;
            //	}
            //}

            //for ( int i = 0; i < movedFromAssetPaths.Length; i++ )
            //{
            //	if ( TemplateHelperFunctions.CheckIfTemplate( movedFromAssetPaths[ i ] ) )
            //	{
            //		refreshMenuItems = true;
            //		break;
            //	}
            //}

            if (refreshMenuItems)
            {
                //UnityEngine.Debug.Log( "Refresh Menu Items" );
                refreshMenuItems = false;
                templatesManager.CreateTemplateMenuItems();

                int windowCount = IOUtils.AllOpenedWindows.Count;
                for (int windowIdx = 0; windowIdx < windowCount; windowIdx++)
                {
                    IOUtils.AllOpenedWindows[windowIdx].CurrentGraph.ForceCategoryRefresh();
                }
            }
        }