- Home /
translate C# with => to unityscript
I'm trying to translate a piece of code from cs to unityscript, but I don't understand the syntax. The code initializes the gree webview which adds webkit to unity. https://github.com/gree/unity-webview
The cs I'd like to translate to unityscript:
webViewObject.Init(
cb: (msg) =>
{
Debug.Log(string.Format("CallFromJS[{0}]", msg));
status.text = msg;
status.GetComponent<Animation>().Play();
},
err: (msg) =>
{
Debug.Log(string.Format("CallOnError[{0}]", msg));
status.text = msg;
status.GetComponent<Animation>().Play();
});
My attempt which does not work:
webViewObject.Init( function(msg:String) {},function(err:String) {} );
I don't really understand why the original even works since it doesn't even have the same arguments. The cs parameters are defined here:
#if UNITY_EDITOR || UNITY_STANDALONE_OSX
public void Init(Callback cb = null, bool transparent = false, string ua = @"Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53", Callback err = null)
#else
public void Init(Callback cb = null, bool transparent = false, Callback err = null)
#endif
So sure, you could look up that stuff in C#, find the technical term, and see if Unityscript can do that. But it's better just not to use Unityscript.
It was the beginner language, but it's not even recommended for that anymore (you can't use the easy shortcuts for phones.)
Answer by Bunny83 · Jun 08, 2016 at 01:25 AM
Those parameters are named parameters which is a feature of the C# compiler. You don't have to use it. If you don't use named parameters you have to pass the parameters in the right order. Default parameters (like in that method definition) in conjunction with positional parameters only work for the last parameters. So you can't omit a parameter in between two other parameters. Named parameters do allow that since you specify which parameter you pass and the compiler doesn't rely on the order in which you pass the parameters.
Your main problem is that you can't omit the "transparent" parameter which is between the two delegate parameters.
I never really use UnityScript but i think this should work:
webViewObject.Init( function(msg:String) {}, false, function(err:String) {} );
However if your platform is UNITY_EDITOR or UNITY_STANDALONE_OSX then you have to pass additional parameters in case you really need the "err" callback. Again you can't omit parameters in between, only at the end. Since "err" is the last parameter you have to pass all parameters manually.
I don't think that UnityScriupt supports named parameters, so your only option is to pass all parameters in the right order.
Ahhh thanks for the links and explanations about named parameters, that was really helpful and it's working now.
Answer by FortisVenaliter · Jun 07, 2016 at 10:19 PM
I highly recommend the Learn section of this website to learn the coding languages... they have some really good tutorials. But there are a lot of resources online, too, so you might try googling C# beginners guide or something similar.
Once you understand the syntax structure of both languages, converting code is merely tedious.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Can someone translate this to Javascript? 1 Answer
Can someone help me translate this to c#? 1 Answer
Translate C# into Js 1 Answer
Problem Javascript to C# 3 Answers