Ejemplo n.º 1
0
 private static void CreateOrReadUpdatedInkFiles(List <string> importedInkAssets)
 {
     foreach (var importedAssetPath in importedInkAssets)
     {
         InkFile inkFile = InkLibrary.GetInkFileWithPath(importedAssetPath);
         if (inkFile == null)
         {
             DefaultAsset asset = AssetDatabase.LoadAssetAtPath <DefaultAsset>(importedAssetPath);
             inkFile = new InkFile(asset);
             InkLibrary.Instance.inkLibrary.Add(inkFile);
         }
         inkFile.ParseContent();
     }
     // Now we've updated all the include paths for the ink library we can create master/child references between them.
     InkLibrary.RebuildInkFileConnections();
 }
        private static void OnMoveAssets(string[] movedAssets)
        {
            if (!InkSettings.Instance.handleJSONFilesAutomatically)
            {
                return;
            }

            List <string> validMovedAssets = new List <string>();

            for (var i = 0; i < movedAssets.Length; i++)
            {
                if (!InkEditorUtils.IsInkFile(movedAssets[i]))
                {
                    continue;
                }
                validMovedAssets.Add(movedAssets[i]);
                queuedMovedAssets.Add(movedAssets[i]);
            }
            // Move compiled JSON files.
            // This can cause Unity to postprocess assets again.
            bool assetMoved = false;

            foreach (var inkFilePath in validMovedAssets)
            {
                InkFile inkFile = InkLibrary.GetInkFileWithPath(inkFilePath);
                if (inkFile == null)
                {
                    continue;
                }
                if (inkFile.jsonAsset == null)
                {
                    continue;
                }

                string jsonAssetPath = AssetDatabase.GetAssetPath(inkFile.jsonAsset);

                string movedAssetDir  = Path.GetDirectoryName(inkFilePath);
                string movedAssetFile = Path.GetFileName(inkFilePath);
                string newPath        = InkEditorUtils.CombinePaths(movedAssetDir, Path.GetFileNameWithoutExtension(movedAssetFile)) + ".json";
                AssetDatabase.MoveAsset(jsonAssetPath, newPath);
                assetMoved = true;
            }

            // Check if no JSON assets were moved (as a result of none needing to move, or this function being called as a result of JSON files being moved)
            if (!assetMoved && queuedMovedAssets.Count > 0)
            {
                List <InkFile> filesToCompile = new List <InkFile>();

                // Add the old master file to the files to be recompiled
                foreach (var inkFilePath in queuedMovedAssets)
                {
                    InkFile inkFile = InkLibrary.GetInkFileWithPath(inkFilePath);
                    if (inkFile == null)
                    {
                        continue;
                    }
                    foreach (var masterInkFile in inkFile.masterInkFilesIncludingSelf)
                    {
                        if (!filesToCompile.Contains(inkFile))
                        {
                            filesToCompile.Add(inkFile);
                        }
                    }
                }

                InkLibrary.RebuildInkFileConnections();

                // Add the new file to be recompiled
                foreach (var inkFilePath in queuedMovedAssets)
                {
                    InkFile inkFile = InkLibrary.GetInkFileWithPath(inkFilePath);
                    if (inkFile == null)
                    {
                        continue;
                    }

                    foreach (var masterInkFile in inkFile.masterInkFilesIncludingSelf)
                    {
                        if (!filesToCompile.Contains(inkFile))
                        {
                            filesToCompile.Add(inkFile);
                        }
                    }
                }

                queuedMovedAssets.Clear();

                // Compile any ink files that are deemed master files a rebuild
                foreach (var inkFile in filesToCompile)
                {
                    if (inkFile.isMaster)
                    {
                        if (InkSettings.Instance.compileAutomatically || inkFile.compileAutomatically)
                        {
                            InkCompiler.CompileInk(inkFile);
                        }
                    }
                }
            }
        }
        private static void OnMoveAssets(string[] movedAssets)
        {
            if (!InkSettings.instance.handleJSONFilesAutomatically)
            {
                return;
            }

            List <string> validMovedAssets = new List <string>();

            for (var i = 0; i < movedAssets.Length; i++)
            {
                if (!InkEditorUtils.IsInkFile(movedAssets[i]))
                {
                    continue;
                }
                validMovedAssets.Add(movedAssets[i]);
                queuedMovedInkFileAssets.Add(movedAssets[i]);
            }
            // Move compiled JSON files.
            // This can cause Unity to postprocess assets again.
            foreach (var inkFilePath in validMovedAssets)
            {
                InkFile inkFile = InkLibrary.GetInkFileWithPath(inkFilePath);
                if (inkFile == null)
                {
                    continue;
                }
                if (inkFile.jsonAsset == null)
                {
                    continue;
                }

                string jsonAssetPath = AssetDatabase.GetAssetPath(inkFile.jsonAsset);

                string movedAssetDir  = Path.GetDirectoryName(inkFilePath);
                string movedAssetFile = Path.GetFileName(inkFilePath);
                string newPath        = InkEditorUtils.CombinePaths(movedAssetDir, Path.GetFileNameWithoutExtension(movedAssetFile)) + ".json";

                // On moving an ink file, we either recompile it, creating a new json file in the correct location, or we move the json file.
                if (InkSettings.instance.ShouldCompileInkFileAutomatically(inkFile))
                {
                    // We have to delay this, or it doesn't properly inform unity (there's no version of "ImportAsset" for delete); I guess it doesn't want OnPostprocessAllAssets to fire recursively.
                    EditorApplication.delayCall += () => {
                        AssetDatabase.DeleteAsset(jsonAssetPath);
                        AssetDatabase.Refresh();
                    };
                }
                else
                {
                    if (string.IsNullOrEmpty(AssetDatabase.ValidateMoveAsset(jsonAssetPath, newPath)))
                    {
                        Debug.Assert(newPath == inkFile.jsonPath);
                        EditorApplication.delayCall += () => {
                            AssetDatabase.MoveAsset(jsonAssetPath, newPath);
                            AssetDatabase.ImportAsset(newPath);
                            AssetDatabase.Refresh();
                            inkFile.FindCompiledJSONAsset();
                        };
                        // Debug.Log(jsonAssetPath+" to "+newPath);
                    }
                    else
                    {
                        // This will fire if the JSON file is also moved with the ink - in this case the json file will be in movedAssets.
                        // Debug.Log($"Failed to move asset from path '{jsonAssetPath}' to '{newPath}'.");
                    }
                }
            }
            // Check if no JSON assets were moved (as a result of none needing to move, or this function being called as a result of JSON files being moved)
            if (queuedMovedInkFileAssets.Count > 0)
            {
                List <InkFile> filesToCompile = new List <InkFile>();

                // Add the old master file to the files to be recompiled
                foreach (var inkFilePath in queuedMovedInkFileAssets)
                {
                    InkFile inkFile = InkLibrary.GetInkFileWithPath(inkFilePath);
                    if (inkFile == null)
                    {
                        continue;
                    }
                    foreach (var masterInkFile in inkFile.masterInkFilesIncludingSelf)
                    {
                        if (InkSettings.instance.ShouldCompileInkFileAutomatically(masterInkFile) && !filesToCompile.Contains(masterInkFile))
                        {
                            filesToCompile.Add(masterInkFile);
                        }
                    }
                }

                InkLibrary.RebuildInkFileConnections();

                // If rebuilding connections caused a file that was previously considered a master file to no longer be, then we remove it.
                for (int i = filesToCompile.Count - 1; i >= 0; i--)
                {
                    if (!filesToCompile[i].compileAsMasterFile)
                    {
                        filesToCompile.RemoveAt(i);
                    }
                }

                // Add the new file to be recompiled
                foreach (var inkFilePath in queuedMovedInkFileAssets)
                {
                    InkFile inkFile = InkLibrary.GetInkFileWithPath(inkFilePath);
                    if (inkFile == null)
                    {
                        continue;
                    }

                    foreach (var masterInkFile in inkFile.masterInkFilesIncludingSelf)
                    {
                        if (InkSettings.instance.ShouldCompileInkFileAutomatically(masterInkFile) && !filesToCompile.Contains(masterInkFile))
                        {
                            filesToCompile.Add(masterInkFile);
                        }
                    }
                }

                queuedMovedInkFileAssets.Clear();


                // Compile any ink files that are deemed master files a rebuild
                InkCompiler.CompileInk(filesToCompile.ToArray(), compileImmediatelyOnImport);
            }
        }