ts2vrcs

Cette page est la référence de mappage TS → C# généré.

Primitives et alias usuels

  • stringstring
  • numberfloat
  • booleanbool
  • voidvoid
  • anyobject
  • nullnull (valeur)
  • undefined → ignoré uniquement dans unions (voir nullabilité)

Types Unity/Udon pris en charge

  • Vector2Vector2
  • Vector3Vector3
  • Vector4Vector4
  • QuaternionQuaternion

Remarque: d’autres types personnalisés sont émis tels quels (par nom) et nécessitent une définition côté C# dans votre projet.

Nullabilité et unions

  • Union contenant null ou undefined → on garde le premier type « effectif » et on ajoute ?.
    • number | nullfloat?
    • string | undefinedstring?
    • Vector3 | null | undefinedVector3?
  • Type avec suffixe TS ? (cas rares dans l’AST type): on tente d’extraire le type de base et d’ajouter ? si absent.

Limites: unions hétérogènes complexes (ex.: number | string) sont réduites au premier type reconnu par l’algorithme (comportement actuel). Préférez des types explicites côté TS.

Propriétés et champs

  • Nom toujours capitalisé (PascalCase) sauf s’il commence par _.
  • Modificateurs supportés: public, private, protected, static.
  • Propriété optionnelle (prop?: T) → T?.
  • Décorateurs TS mappés en attributs C#:
    • @X()[X]
    • @X[X]

Exemple

@UdonSynced() private health?: number = 100
[UdonSynced]
private float? Health = 100;

Méthodes

  • Nom capitalisé par défaut, avec table de correspondance des événements Udon:
    • startStart
    • updateUpdate
    • lateUpdateLateUpdate
    • fixedUpdateFixedUpdate
    • onEnableOnEnable
    • onDisableOnDisable
    • onDestroyOnDestroy
    • interactInteract
    • interactionInteraction
    • onPickupOnPickup
    • onDropOnDrop
    • onPickupUseDownOnPickupUseDown
    • onPickupUseUpOnPickupUseUp
    • onPlayerJoinedOnPlayerJoined
    • onPlayerLeftOnPlayerLeft
    • onStationEnteredOnStationEntered
    • onStationExitedOnStationExited
    • onOwnershipTransferredOnOwnershipTransferred
    • onPreSerializationOnPreSerialization
    • onPostSerializationOnPostSerialization
    • onDeserializationOnDeserialization
    • onTriggerEnterOnTriggerEnter
    • onTriggerExitOnTriggerExit
    • onTriggerStayOnTriggerStay
    • onCollisionEnterOnCollisionEnter
    • onCollisionExitOnCollisionExit
    • onCollisionStayOnCollisionStay
    • onVideoStartOnVideoStart
    • onVideoEndOnVideoEnd
    • onVideoPlayOnVideoPlay
    • onVideoPauseOnVideoPause
    • onVideoErrorOnVideoError
  • Ajout automatique de override si c’est un événement Udon et que la méthode est public ou protected.
  • Paramètres: mapType + nom inchangé.
  • Retour: mapType ou void.

Appels de fonctions et accès membres

  • DebugLog(x)Debug.Log(x).
  • Appels membres sur this: this.foo()this.Foo() (ou mappage Udon event si applicable).
  • Appels globaux: foo()Foo() sauf pour une poignée reconnue (Vector3, Quaternion, Color, LayerMask, DebugLog).
  • Accès propriétés: capitalisation systématique, sauf _ en tête.
  • Math.*Mathf.* avec membre capitalisé: Math.sin(x)Mathf.Sin(x).
  • Chaînage optionnel conservé: a?.b / a?.b().

Constructions de langage

  • Instructions
    • if (cond) { ... } else { ... } → identique en C#.
    • return expr; → identique.
    • new Type(args) → identique.
  • Expressions
    • Opérateurs binaires: rendus tels quels (+, -, *, /, %, &&, ||, ==, !=, <, >, <=, >=).
    • Coalescence nulle ?? supportée.
    • Unaires pré/post: ++a, a++, --a, a--, !a, -a rendus tels quels.
  • Littéraux
    • Chaînes: "...".
    • Nombres: rendus tels quels (pas de suffixe f ajouté automatiquement).

Éléments ignorés (pas de génération C# directe)

  • Import/Export TypeScript (les import sont supprimés dans la sortie).
  • type alias et interface (déclarations de type pures). Si vous avez besoin de types côté C#, définissez‑les manuellement.

Exemples de bout‑en‑bout

Entrée TS

class Player {
  @UdonSynced() private health?: number = 100
  start() { DebugLog("Ready") }
  takeDamage(amount: number | null) {
    this.health = (this.health ?? 0) - (amount ?? 0)
  }
}

Sortie C#

using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;

public class Player : UdonSharpBehaviour
{
    [UdonSynced]
    private float? Health = 100;

    public override void Start()
    {
        Debug.Log("Ready");
    }

    public void TakeDamage(float? amount)
    {
        this.Health = (this.Health ?? 0) - (amount ?? 0);
    }
}