Skip to content

Lightweight scripting system for Unity with custom syntax, syntax highlighting, and attribute-based context binding.

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta
Notifications You must be signed in to change notification settings

xavierarpa/InScript

InScript

InScript - Lightweight Scripting for Unity

Unity MIT License PRs Welcome

A lightweight, designer-friendly scripting system for Unity. Define game logic using a simple custom syntax with real-time syntax highlighting in the Inspector.

✨ Features

  • πŸ“ Simple Syntax - Easy-to-learn DSL designed for game designers
  • 🎨 Syntax Highlighting - Real-time colored preview in the Unity Inspector
  • πŸ”’ Line Numbers - Visual Studio-like line number gutter
  • 🎯 Attribute-Based Binding - Connect scripts to your code with simple attributes
  • πŸ“¦ Inline or Asset - Use scripts embedded in components or as reusable ScriptableObjects
  • βœ… Live Validation - Instant error detection as you type
  • πŸ”§ Zero Dependencies - Pure C#, no external libraries required

πŸ“¦ Installation

Via Git URL (Package Manager)

  1. Open Package Manager in Unity (Window > Package Manager)
  2. Click the + button and select Add package from git URL...
  3. Enter the following URL:
https://github.com/xavierarpa/InScript.git

Manual Installation

  1. Download or clone this repository
  2. Copy the InScript folder into your Assets/Plugins folder

πŸš€ Quick Start

1. Create a Script Context

Add attributes to expose fields and methods to scripts:

using UnityEngine;
using InScript;

public class Character : MonoBehaviour
{
    [SerializeField, InScript] private float hp = 100;
    [SerializeField, InScript] private float maxHp = 100;
    [SerializeField, InScript] private float attack = 10;
    
    [SerializeField, InScript("Target")] private Character target;
    
    [InScript]
    private void Heal(float amount)
    {
        hp = Mathf.Min(hp + amount, maxHp);
    }
    
    [InScript]
    private void Log(string message, float value)
    {
        Debug.Log($"{message}: {value}");
    }
}

2. Write a Script

Create a ScriptAsset or use an inline Script field:

@main
    $damage = attack * 1.5
    
    ? hp < maxHp * 0.5
        Log("Low HP!", hp)
        Heal(20)
    ;
    
    #Target.TakeDamage($damage)
;

3. Execute

[SerializeField] private Script script;

void Start()
{
    script.ExecuteBlock("main", this);
}

πŸ“– Syntax Reference

Blocks

@blockName          // Start a named block
    // code here
;                   // End block

Variables

$localVar = 10              // Local variable (script scope)
$localVar += 5              // Compound assignment (+=, -=, *=, /=)
contextVar = value          // Context variable (from your code)

Selectors

#Target.hp                  // Access selector property
#Target.TakeDamage(10)      // Call selector method

Conditionals

? condition                 // If
    // code
:? otherCondition           // Else if
    // code
:                           // Else
    // code
;                           // End if

Built-in Functions

Math Functions

Function Description Example
min(a, b) Returns the smaller of two values min(hp, maxHp) β†’ 50 if hp=50, maxHp=100
max(a, b) Returns the larger of two values max(0, damage - armor) β†’ prevents negative
clamp(value, min, max) Constrains a value between min and max clamp(hp, 0, maxHp) β†’ keeps hp in valid range
abs(x) Returns the absolute (positive) value abs(-5) β†’ 5
sign(x) Returns -1, 0, or 1 based on sign sign(-10) β†’ -1

Rounding Functions

Function Description Example
floor(x) Rounds down to nearest integer floor(3.7) β†’ 3
ceil(x) Rounds up to nearest integer ceil(3.2) β†’ 4
round(x) Rounds to nearest integer round(3.5) β†’ 4

Advanced Functions

Function Description Example
sqrt(x) Square root sqrt(16) β†’ 4
pow(base, exp) Power/exponent pow(2, 3) β†’ 8
lerp(a, b, t) Linear interpolation between a and b lerp(0, 100, 0.5) β†’ 50
random(min, max) Random float between min and max random(1, 10) β†’ 5.7 (varies)
random() Random float between 0 and 1 random() β†’ 0.42 (varies)

Operators

Type Operators
Arithmetic + - * /
Comparison == != < > <= >=

πŸ—οΈ Architecture

InScript/
β”œβ”€β”€ Runtime/
β”‚   β”œβ”€β”€ Script.cs              # Serializable script container
β”‚   β”œβ”€β”€ ScriptAsset.cs         # ScriptableObject wrapper
β”‚   β”œβ”€β”€ ScriptRunner.cs        # Script execution engine
β”‚   β”œβ”€β”€ IScriptContext.cs      # Context interface
β”‚   β”œβ”€β”€ ReflectionContext.cs   # Attribute-based context
β”‚   └── Attributes/
β”‚       └── InScriptAttribute.cs # Unified attribute for values/selectors/methods
└── Editor/
    β”œβ”€β”€ ScriptDrawer.cs           # PropertyDrawer with syntax highlighting
    β”œβ”€β”€ ScriptAssetEditor.cs      # Custom editor for ScriptAsset
    β”œβ”€β”€ ScriptDebugWindow.cs      # Debug panel for testing scripts
    └── SyntaxReferenceWindow.cs  # Dockable syntax documentation

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

🀝 Contributing

Contributions are welcome! Please read CONTRIBUTING.md for details.

πŸ‘€ Author

Xavier Arpa - @xavierarpa

About

Lightweight scripting system for Unity with custom syntax, syntax highlighting, and attribute-based context binding.

Topics

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Code of conduct

Contributing

Stars

Watchers

Forks

Languages