Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help please, C# to Javascript.

Discussion in 'Scripting' started by psycocrusher, Sep 30, 2014.

  1. psycocrusher

    psycocrusher

    Joined:
    Jul 24, 2011
    Posts:
    71
    I' am trying to convert the script from the PSM InApp purchase example to Javascript.
    It's the three first lines that are causing me problems, any help would be appreciated. Thank you.

    Code (CSharp):
    1. public class MessageDialog : MonoBehaviour{
    2.  
    3.     public String message { private get; set; }
    4.  
    5.     public GUISkin mySkin { private get; set; }
    6.  
    7.     public event EventHandler<EventArgs> Terminated;
    8.  
    9.     void OnGUI(){
    10.    
    11.         GUI.skin = mySkin;
    12.         GUI.depth = -1;
    13.  
    14.         float sw = Screen.width;
    15.         float sh = Screen.height;
    16.  
    17.         GUI.Box(new Rect(sw / 5, sh / 3, sw * 3 / 5, sh / 3), message);
    18.  
    19.         if (GUI.Button(new Rect(sw * 2 / 5, sh / 2, sw / 5, sh / 10), "OK"))
    20.         {
    21.             Destroy(this);
    22.             Terminated(this, EventArgs.Empty);
    23.         }
    24.     }
    25. }
     
  2. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    Javascript (or rather, Unityscript) doesn't have the get and set stuff built into it like C# does.
    You could just write them out as functions, like:

    Code (JavaScript):
    1. var message : String;
    2.  
    3. function GetMessage()
    4. {
    5.     return message;
    6. }
    7.  
    8. function SetMessage(string:String)
    9. {
    10.     message = string;
    11. }
    12.  
    You can also get rid of the entire MessageDialog public class everything is couched in.
     
  3. psycocrusher

    psycocrusher

    Joined:
    Jul 24, 2011
    Posts:
    71
    I got it working thanks for the input.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes it does.

    Code (csharp):
    1. private var _message : String;
    2.  
    3. function get message () : String {return _message;}
    4. function set message (value : String) {_message = value;}
    --Eric