UnityScript “generic functions” in Unity 5

2020-04-16 18:45发布

问题:

While following through the Unity3D tutorials on their website (http://unity3d.com/learn/tutorials/projects/roll-a-ball/moving-the-player) I have come across "generic functions".

http://docs.unity3d.com/Manual/GenericFunctions.html

In C++ speak, I would have called them templates. In their UnityScript it looks like this:

var obj = GetComponent.<Rigidbody>();

I've tried looking elsewhere online to get a rundown of exactly how this stuff works, and how I can write UnityScript code that uses it, but I've come up empty. I wonder if I'm just not searching for the correct terms. They say that UnityScript is based off of Javascript, but I suspect that this is specific to Unity's UnityScript compiler.

"Generic functions" do not exist in the ECMAScript version of JavaScript, only in Unity's version.

Edit: This question was posted when Unity was still calling their proprietary scripting language "Javascript", although they now seem to be calling it UnityScript again. This would have prevented the question from ever being asked in the first place.

回答1:

UnityScript and JavaScript are two different languages.

UnityScript is a proprietary language, and it doesn't follow any concrete [ECMAScript] specification

Regarding the generics/templates.

Since [UnityScript] also supports explicit typing, it also has support for "generics" (runtime type enforcement), which JavaScript has no notion of.

Source UnityScript versus JavaScript: UnityScript has .NET's OOP features

See also:

  • Head First into Unity with UnityScript
  • UnityScript versus JavaScript