I’m looking to do something a little crazy and incorporate a TypeScript workflow into Unity. At least, I’m interested in seeing how it goes.
The only problem is I’m lacking a .d.ts declaration file to make the TypeScript compiler aware of the Unity Scripting API, and I can’t really find one online. This is not really surprising, so I’m willing to put in the effort to make one which I could share with others (assuming all things go well). I think this would be a cool thing for the community as well as a fun project.
ANYWAYS. Since the Unity Scripting API is literally thousands of possible objects and function calls I am really looking for a solution to automate the generation of this declaration file, so that I may save effort + also be able to keep it up to date.
I can only assume the Unity docs are generated from some source that is updated with each version, and they have detailed signature information on each page for each overload, which would be perfect for what I am looking to do.
So I ask to the community (and perhaps any Unity contributors that may be passing by), is there some form of source for the Scripting Reference around somewhere that I may use to generate a declaration file?
I’ve found this repo on GitHub that seems to have achieved my goal already but the files don’t seem to compile / are generated wrong in some way.
Please let me know if you know of something that could help on this front
Unityscript is NOT javascript… though they stupidly allow it to be called javascript.
Rather it’s a javascript inspired language that compiles to CIL (the intermediate language used by .net/mono runtimes).
You’ll end up with huge issues in that the way TypeScript compilers generate the js constructor functions for ‘classes’ is NOTHING like the way unityscript expects classes to be defined. Nevermind numerous other syntactical issues that would arise.
Nevermind the weirdness of TypeScript compiling to javascript compiling to CIL… even if you got it to work by modding out the TypeScript compiler.
If you want a type safe language that scales well, like what TypeScript attempts to accomplish… why not write C#?
So you’re saying that what Unity has listed as “Java Script” is a proprietary language … ? Wuuut?
What differences are there in syntax from JavaScript?
I do find C# very cumbersome and it’s a real shame Unity is so locked into it =/
It’s honestly easier to list the things that they have in common, which might actually be limited to the use of curly brackets, and the “var” keyword. It’s not JavaScript, at all. Separate yourself from that notion entirely.
To clarify, we are not just talking about some minor syntax differences. We are talking about a major structural difference.
Under the hood UnityScript is C#. It’s syntax is slightly modified to make it look like JavaScript. But it’s still C#. It’s a statically typed, OOP, .Net language.
The reason for calling it JavaScript in the docs is to dupe JavaScript developers like yourself into using the engine. It’s quite frankly a dirty marketing tatic. Everyone within Unity and everyone on the community knows the name is deliberately misleading.
To top it off, UnityScript is a second class citizen in terms of support, learning materials, examples, documentation and the community. So not only are they duping people into using a language that’s not what they expected, they are also duping those same folks into using a substandard language.
While I don’t think there is any need for Unity to force users onto C#, they should at least be honest about what UnityScript is.
Game engines often have to pick something as its scripting language du jour.
Some of them get weird, like Unreal using unrealscript… a bizarrely hamfisted language (of course they now give free access to C++, thank god… but if C# is cumbersome in your book, I could only imagine what you think of C++. Oh and funny enough, there is a ‘mono for unreal’ framework that lets you write C# in unreal as well… lol).
Unity went with using mono as its script interpreter… probably because it meant they got a fast script interpreter for little effort that was cross platform. And since C# is the primary language of mono, C# it is.
Note though, other languages had been supported over time… but the community slowly but surely selected a language of preference. Boo got all but removed (the compiler is technically still there, but there is no support beyond that).
Funny enough unityscript used to be the more popular of the languages, it was only in recent years that C# became the more popular.
What I want to know is… why do you find C# cumbersome… but not TypeScript? I’ve written TypeScript… I REALLY like TypeScript because it makes what is a horrendous language (javascript) actually freaking manageable on a scale that is anything larger than website animations.
But the primary reason I do like TypeScript is because it makes it so similar to C# and other C-like languages.
What is C# doing that is so cumbersome relative to TypeScript?
I had a reply to this all typed up about how this is a sin of the past, that Unity has stopped using JavaScript in its marketing materials to refer to UnityScript, and I think that for a while they did (and for sure on the forums they’ve all acknowledged that it never should’ve been done).
But then I checked the page where they describe Unity to new users (which I haven’t looked at in several years) and sure enough, there’s Javascript, sitting there right next to C#. Putting myself in the shoes of a new Unity user excited about using Javascript, I clicked on. The next page deep also calls it JavaScript, and it’s not until you get several pages into the documentation that it starts calling it UnityScript.
EDIT: Clicked around in archive.org a bit to investigate the history of JavaScript/UnityScript marketing. In 2008 it had JS and C# listed on the main page. Starting in 2011 they have C# listed alone. In 2013 the website got retooled, and JavaScript was back, and Boo makes the cut this time too! A bold move, given that Boo would be killed off a year later, at which point it would also be removed from this page - JS remains there, however. The story gets interesting again in 2015 when Boo is added back to the page at the time of the next redesign! Boo was being advertised next to C# and JS until literally last month, 3 years after its deprecation, which brings us to the current page.
In summary: There was a brief time when the website did not advertise JavaScript on equal footing with C#; however, there has never been a time when it was called UnityScript in marketing materials. And amusingly, it seems like someone forgot to tell marketing that Boo was dead. Perhaps someone should let marketing know that Unityscript isn’t Javascript.
It’s enough that any reasonable person doing a cursory glance would assume something close to real JavaScript. Unless it’s something you dive into on google, you won’t see the difference.
Note that the differences between C# and US are significantly smaller then the differences between US and JS.
I can get by in US. When I was first starting out the language was much more common then it is now, and was actually reccomended for beginners. But these days the only reason to use it is to support legacy projects.
Mostly just the syntax. I am a web developer in my day job so I live in modern JavaScript land. Take for example an interface in TypeScript:
interface Player {
position: Point,
health: number,
name: string
}
let player1: Player = {
position: new Point(10, 20),
health: 100,
name: "Player 1"
};
Which in C# might be something like
public interface Player {
Point position { get; set; }
int Health { get; set; }
String Name { get; set; }
}
public class GamePlayer : Player {
public Point Position { get; set; }
public int Health { get; set; }
public String Name { get; set; }
public GamePlayer(position, health, name) {
this.Position = position;
this.Health = health;
this.Name = name;
}
}
...
var player = new GamePlayer(new Point(10, 20), 100, "Player 1");
Which is obviously a contrived / specific example, but it’s just a general theme of C# that I find a lot of things require a fair amount of effort, relatively speaking.
That said, with this revelation that Unity does not actually have support for JavaScript I am feeling resigned to just using C# anyway.
You actually don’t need “this” in front of those in the constructor, since they are not spelled the same(caps)…heck, you don’t even need a constructor and you can still assign values to the properties when you create a new GamePlayer.
You’ve gone out of your way to make your c# example longer…
public class Player {
public Point position;
public int health;
public string name;
}
var player = new Player{
position = new Point(10, 20),
health = 100,
name = "Player 1",
};
I was making a point about its brevity, and I acknowledge my C# chops are not as sharp as they could be. Though, technically I was demonstrating an interface… but it does not functionally affect the example I gave. Anyways. Yeah. Gonna get back into C# I guess.