Пример #1
0
        public override void OnEnable()
        {
            base.OnEnable();

            if (config is null)
            {
                config = Configuration.LoadOrDefault <ScriptsConfiguration>();
            }
            scriptAsset = assetTarget as ScriptAsset;

            if (config.EnableVisualEditor)
            {
                visualEditor = new ScriptView(config, ApplyRevertHackGUI, ApplyAndImport);
                visualEditor.GenerateForScript(scriptAsset);

                ScriptImporter.OnModified += visualEditor.GenerateForScript;
                return;
            }

            previewContent = scriptAsset.ScriptText;
            if (previewContent.Length > previewLengthLimit)
            {
                previewContent  = previewContent.Substring(0, previewLengthLimit);
                previewContent += $"{System.Environment.NewLine}<...>";
            }

            var script = new Script(scriptAsset.name, scriptAsset.ScriptText, ignoreErrors: true);

            labelTags = script.LabelLines.Select(l => new GUIContent($"# {l.LabelText}")).ToArray();
            gotoTags  = script.CommandLines
                        .Where(c => c.CommandName.EqualsFastIgnoreCase("goto") && c.CommandParameters.TryGetValue(string.Empty, out var path) && !path.StartsWithFast("."))
                        .Select(c => new GUIContent($"@goto {c.CommandParameters[string.Empty]}")).ToArray();
        }
Пример #2
0
        public override void OnImportAsset(AssetImportContext ctx)
        {
            var contents = string.Empty;

            try
            {
                var bytes = File.ReadAllBytes(ctx.assetPath);
                contents = Encoding.UTF8.GetString(bytes, 0, bytes.Length);

                // Purge BOM. Unity auto adding it when creating script assets: https://git.io/fjVgY
                if (contents.Length > 0 && contents[0] == '\uFEFF')
                {
                    contents = contents.Substring(1);
                    File.WriteAllText(ctx.assetPath, contents);
                }
            }
            catch (IOException exc)
            {
                ctx.LogImportError($"IOException : {exc.Message}");
            }
            finally
            {
                var asset = ScriptAsset.FromScriptText(contents);
                asset.hideFlags = HideFlags.NotEditable;

                ctx.AddObjectToAsset("naniscript", asset);
                ctx.SetMainObject(asset);

                OnModified?.Invoke(asset);
            }
        }
Пример #3
0
        public override void OnDisable()
        {
            base.OnDisable();

            if (visualEditor != null)
            {
                ScriptImporter.OnModified -= visualEditor.GenerateForScript;
            }
            scriptAsset = null;
        }
Пример #4
0
        public void GenerateForScript(ScriptAsset scriptAsset)
        {
            this.scriptAsset = scriptAsset;
            ScriptModified   = false;

            // Prevent re-generating the editor after saving the script (applying the changes done in the editor).
            if (lastGeneratedTextHash == scriptAsset.ScriptText.GetHashCode())
            {
                return;
            }

            lastGenerateDelayedAction    = GenerateForScriptDelayed;
            EditorApplication.delayCall -= GenerateForScriptDelayed;
            EditorApplication.delayCall += GenerateForScriptDelayed;

            void GenerateForScriptDelayed()  // Otherwise, it's invoked twice when entering playmode.
            {
                EditorApplication.delayCall -= GenerateForScriptDelayed;
                if (lastGenerateDelayedAction != GenerateForScriptDelayed)
                {
                    return;
                }

                linesContainer.SetEnabled(!EditorApplication.isPlayingOrWillChangePlaymode);
                infoLabel.style.display = EditorApplication.isPlayingOrWillChangePlaymode ? DisplayStyle.None : DisplayStyle.Flex;

                lines.Clear();
                linesContainer.Clear();
                var scriptLinesText = scriptAsset.ScriptText?.TrimFull()?.SplitByNewLine() ?? new[] { string.Empty };

                for (int i = 0; i < scriptLinesText.Length; i++)
                {
                    if (scriptLinesText.Length > showLoadAt && (i % showLoadAt) == 0) // Update bar for each n processed items.
                    {
                        if (scriptLinesText is null || EditorUtility.DisplayCancelableProgressBar("Generating Visual Editor", "Processing naninovel script...", i / (float)scriptLinesText.Length))
                        {
                            EditorUtility.ClearProgressBar();
                            Add(new IMGUIContainer(() => EditorGUILayout.HelpBox("Visual editor generation has been canceled.", MessageType.Error)));
                            return;
                        }
                    }
                    var scriptLineText = scriptLinesText[i].TrimFull();
                    if (string.IsNullOrEmpty(scriptLineText))
                    {
                        lines.Add(null); continue;
                    }                                                                        // Skip empty lines.
                    var lineView = CreateLineView(scriptLineText, i);
                    lines.Add(lineView);
                    if (ViewRange.Contains(i))
                    {
                        linesContainer.Add(lineView);
                    }
                }
                EditorUtility.ClearProgressBar();

                if (lines.Count > config.VisualEditorPageLength)
                {
                    paginationView.style.display = DisplayStyle.Flex;
                    UpdatePaginationLabel();
                }
                else
                {
                    paginationView.style.display = DisplayStyle.None;
                }

                var hotKeyInfo   = config.InsertLineKey == KeyCode.None ? string.Empty : $" or {config.InsertLineKey}";
                var modifierInfo = (config.InsertLineKey == KeyCode.None || config.InsertLineModifier == EventModifiers.None) ? string.Empty : $"{config.InsertLineModifier}+";

                if (!string.IsNullOrEmpty(modifierInfo))
                {
                    hotKeyInfo = hotKeyInfo.Insert(4, modifierInfo);
                }
                infoLabel.text = $"Right-click{hotKeyInfo} to insert a new line.";

                Engine.OnInitializationFinished -= HandleEngineInitialized;
                if (Engine.IsInitialized)
                {
                    HandleEngineInitialized();
                }
                else
                {
                    Engine.OnInitializationFinished += HandleEngineInitialized;
                }

                if (scriptLinesText.Length > showLoadAt)
                {
                    EditorUtility.DisplayProgressBar("Generating Visual Editor", "Building layout...", .5f);
                }
                EditorApplication.delayCall += ClearProgressBarDelayed;
            }
        }