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 assetName = Path.GetFileNameWithoutExtension(ctx.assetPath); var asset = Script.FromScriptText(assetName, contents); asset.hideFlags = HideFlags.NotEditable; ctx.AddObjectToAsset("naniscript", asset); ctx.SetMainObject(asset); } }
private async void PlayScriptAsync() { if (!string.IsNullOrEmpty(scriptName)) { var player = Engine.GetService <IScriptPlayer>(); if (player is null) { throw new Exception($"Failed to play a script via `{nameof(PlayScript)}` component attached to `{gameObject.name}` game object: script player service is not available."); } await player.PreloadAndPlayAsync(scriptName); return; } if (!string.IsNullOrWhiteSpace(scriptText)) { var text = string.IsNullOrEmpty(argument) ? scriptText : scriptText.Replace("{arg}", argument); var script = Script.FromScriptText($"`{name}` generated script", text); var playlist = new ScriptPlaylist(script); foreach (var command in playlist) { if (command.ShouldExecute) { if (!command.Wait && !(command is Commands.Command.IForceWait)) { command.ExecuteAsync().Forget(); } else { await command.ExecuteAsync(); } } } } }