Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Muzz5 · May 22, 2011 at 05:56 PM · languagejavascript-specificlanguage-comparison

Script translation

Could anyone translate this script from C# to JS? If this is too hard/would take to long for the community, tell me roughly what I need to do.

 using UnityEngine;
 using System.Collections;
 
 public class SimpleCity : MonoBehaviour
 {
     public Transform player;
 
     public Transform[] blocks;
     public Transform[] bases;
     public Transform[] floors;
     public Transform[] roofs;
 
     public int blockXNum = 10;
     public int blockZNum = 10;
     public float blockSpan = 10f;
 
     public int buildingXNum = 4;
     public int buildingZNum = 2;
     public float buildingSpan = 10f;
 
     GameObject cityRoot;
 
     int numBlocks;
     int numBuildings;
 
     float[] cardinals = { 0, 90, 180, 270 };
 
     bool building;
 
     void Start()
     {
         StartCoroutine(Generate());
     }
 
     IEnumerator Generate()
     {
         building = true;
 
         GameObject gPlayer = GameObject.FindGameObjectWithTag("Player");
 
         if (gPlayer)
             gPlayer.SetActiveRecursively(false);
 
         if (cityRoot)
             Destroy(cityRoot);
 
         cityRoot = new GameObject("CityRoot");
 
         float blockXSize = buildingXNum * buildingSpan;
         float blockZSize = buildingZNum * buildingSpan;
 
         float blockXOffset = (((blockXNum - 1) * blockSpan) + ((blockXNum - 1) * blockXSize)) * 0.5f;
         float blockZOffset = (((blockZNum - 1) * blockSpan) + ((blockZNum - 1) * blockZSize)) * 0.5f;
 
         Vector3 blockPos = new Vector3(-blockXOffset, 0, -blockZOffset);
 
         for (int x = 0; x < blockXNum; ++x)
         {
             for (int z = 0; z < blockZNum; ++z)
             {
                 CreateBlock(blockPos);
                 blockPos.z += blockZSize + blockSpan;
                 yield return new WaitForEndOfFrame();
             }
 
             blockPos.x += blockXSize + blockSpan;
             blockPos.z = -blockZOffset;
         }
 
         if (player)
         {
             if (gPlayer)
             {
                 gPlayer.SetActiveRecursively(true);
                 gPlayer.transform.position = new Vector3(0f, 1f, 0f);
                 gPlayer.transform.rotation = Quaternion.identity;
             }
             else
             {
                 Transform p = (Transform)Instantiate(player, new Vector3(0f, 1f, 0f), Quaternion.identity);
 
                 if (p)
                 {
                     Camera.main.transform.position = new Vector3(0f, 1.5f, 0f);
                     Camera.main.transform.rotation = Quaternion.identity;
                     Camera.main.transform.parent = p;
                 }
             }
         }
 
         building = false;
     }
 
     void CreateBlock(Vector3 position)
     {
         Transform block = null;
 
         if (blocks.Length > 0)
         {
             block = (Transform)Instantiate(blocks[Random.Range(0,blocks.Length)], position, Quaternion.identity);
             block.name = "Block" + ++numBlocks;
         }
         else
         {
             block = new GameObject("Block" + ++numBlocks).transform;
             block.position = position;
         }
         
         block.parent = cityRoot.transform;
 
         float blockXScale = ((buildingXNum * buildingSpan) + blockSpan) * 0.1f;
         float blockZScale = ((buildingZNum * buildingSpan) + blockSpan) * 0.1f;
 
         block.localScale = new Vector3(blockXScale, 1, blockZScale);
 
         float buildingXOffset = ((buildingXNum - 1) * buildingSpan) * 0.5f;
         float buildingZOffset = ((buildingZNum - 1) * buildingSpan) * 0.5f;
 
         Vector3 buildingPos = new Vector3(position.x - buildingXOffset, 0, position.z - buildingZOffset);
 
         for (int x = 0; x < buildingXNum; ++x)
         {
             for (int z = 0; z < buildingZNum; ++z)
             {
                 CreateBuilding(buildingPos, block);
                 buildingPos.z += buildingSpan;
             }
 
             buildingPos.x += buildingSpan;
             buildingPos.z = position.z - buildingZOffset;
         }
 
         block.gameObject.AddComponent("CombineChildren");
     }
 
     void CreateBuilding(Vector3 position, Transform block)
     {
         Vector3 curPosition = position;
 
         ++numBuildings;
 
         Transform c_parent = null;
 
         if (bases.Length > 0)
         {
             Transform b = (Transform)Instantiate(bases[Random.Range(0, bases.Length)], curPosition, Quaternion.AngleAxis(cardinals[Random.Range(0,cardinals.Length)], Vector3.up));
             b.name = "Base" + numBuildings;
             b.parent = block;
             c_parent = b;
         }
 
         if (floors.Length > 0)
         {
             int numFloors = Random.Range(1, 10);
 
             for (int i = 0; i < numFloors; ++i)
             {
                 curPosition.y += 4;
                 Transform f = (Transform)Instantiate(floors[Random.Range(0, floors.Length)], curPosition, Quaternion.identity);
                 f.name = "Floor" + numBuildings + "_" + i;
                 f.parent = c_parent;
                 c_parent = f;
             }
         }
 
         curPosition.y += 4;
 
         if (roofs.Length > 0)
         {
             Transform r = (Transform)Instantiate(roofs[Random.Range(0, roofs.Length)], curPosition, Quaternion.AngleAxis(cardinals[Random.Range(0, cardinals.Length)], Vector3.up));
             r.name = "Roof" + numBuildings;
             r.parent = c_parent;
         }
     }
 
     void OnGUI()
     {
         if (building)
         {
             GUILayout.Label("Building...");
         }
         else
         {
             //public int blockXNum = 10;
             GUILayout.BeginHorizontal();
             GUILayout.Label("Block X Num: ");
             blockXNum = System.Int32.Parse(GUILayout.TextField(blockXNum.ToString(), GUILayout.Width(50)));
             GUILayout.EndHorizontal();
 
             //public int blockZNum = 10;
             GUILayout.BeginHorizontal();
             GUILayout.Label("Block Z Num: ");
             blockZNum = System.Int32.Parse(GUILayout.TextField(blockZNum.ToString(), GUILayout.Width(50)));
             GUILayout.EndHorizontal();
 
             //public float blockSpan = 10f;
             GUILayout.BeginHorizontal();
             GUILayout.Label("Block Span: ");
             blockSpan = (float)System.Double.Parse(GUILayout.TextField(blockSpan.ToString(), GUILayout.Width(50)));
             GUILayout.EndHorizontal();
 
             GUILayout.Space(20);
 
             //public int buildingXNum = 4;
             GUILayout.BeginHorizontal();
             GUILayout.Label("Building X Num: ");
             buildingXNum = System.Int32.Parse(GUILayout.TextField(buildingXNum.ToString(), GUILayout.Width(50)));
             GUILayout.EndHorizontal();
 
             //public int buildingZNum = 2;
             GUILayout.BeginHorizontal();
             GUILayout.Label("Building Z Num: ");
             buildingZNum = System.Int32.Parse(GUILayout.TextField(buildingZNum.ToString(), GUILayout.Width(50)));
             GUILayout.EndHorizontal();
 
             //public float buildingSpan = 10f;
             GUILayout.BeginHorizontal();
             GUILayout.Label("Building Span: ");
             buildingSpan = (float)System.Double.Parse(GUILayout.TextField(buildingSpan.ToString(), GUILayout.Width(50)));
             GUILayout.EndHorizontal();
 
             GUILayout.Space(20);
 
             if (GUILayout.Button("Generate"))
                 StartCoroutine(Generate());
         }
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Uriel_96 · May 22, 2011 at 06:54 PM 3
Share

what you need to do is don't ask people to do something you have to do, This a unity answers not a unity doing your job

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Peter G · May 22, 2011 at 07:21 PM

I will not translate the code for you, that isn't how UA works, but I can give you a basic guide to converting C# to js. This is far more encompassing than what you need to translate your script.

  1. Change using to import

  2. You can either remove the class declaration, or you can do this:

        public class SimpleCity extends MonoBehaviour {
    
    
  3. Rules for Variable declarations:

    • You can leave the access modifier. public isn't needed in js but it doesn't hurt.

    • Remove the type (that is the class or struct before the name of the variable). It should be the first word after public.

    • Add var before the variable name.

    • After the variable name (but before the "=" sign) add "`: ClassName`"

    • Leave anything after the "=" sign.

    • Example: public Transform myTransform; becomes public var myTransform : Transform;

    • Example 2: private int internalValue = 128 becomes private var internalValue : int = 128;

    • I'm lying some because the compiler can guess the type on the second one, but if you follow these rules you won't have any problems.

    • Special Cases:

    • In C# true/false values are called bool. In js, you must change the type to Boolean.

    • Note the same rule applies for some other number types such as long. Remove the appended character.

    • I don't know if you can do hexadecimal literals in js. If you see one in C#, just turn it into its decimal equivalent.

    • C# generics are defined like this: System.Collections.Generic.List<T>(). In js, you need to add a period before the "

    • if there are any parameters in the function (thats the variables between the parentheses, remove the type from the beginning and put it after as above example (int damage , float range) becomes (damage : int , range : float)

    • So the full change might look like: public void ApplyDamage (int damage) { . Becomes public function ApplyDamage (damage : int) : void {

  4. Coroutines. There isn't that much to do for coroutines. Remove the StartCoroutine() from any C# script so that just the function call remains. Remove the return after any yield statements.

  5. foreach loops. The foreach becomes for and the inside is changed like every other variable. Add var before it and use a colon + the type after it.

  6. Anonymous functions. C# anonymous functions are done through lambda expressions usually.

          //C#
             Action myDel = () => Debug.Log("Anonymous Method");
             //or
             //Action myDel = delegate() { Debug.Log("Anonymous Method"); };
    
    

In js, they are done as such

         //js
         var myFunction : Function = function() { Debug.Log("Anonymous Function"); };

I think that pretty much covers all of it that you will frequently come across. I don't know how to do LINQ in js so avoid that if you can. If you come across anything else then I will help you translate it. Hopefully this can become a document for anyone who needs to translate from one language to the other.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Peter G · May 22, 2011 at 07:24 PM 0
Share

I can't seem to edit the post right, must be a bug. but let me clarify something:

 //C#
 public ClassType myProperty {
          get {
                   return someValue;
          }
          set {
                   someValue = value;
         }
 }
avatar image Uriel_96 · May 22, 2011 at 07:32 PM 0
Share

also this question/answer could be very helpful: http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

avatar image Muzz5 · May 23, 2011 at 06:53 AM 0
Share

Thanks, this is what I was looking for.

avatar image
0

Answer by gergerr · Jul 21, 2011 at 12:15 PM

Then it wouldn't be a Spam Sausage Egg Biscuit, now would it?!?

(Edited spam - Almo)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

What are the Syntax Differences in C# and Javascript? 7 Answers

How should I decide if I should use C#, JavaScript (UnityScript) or Boo for my project? 14 Answers

Is it necessary to use both Javascript and C# 6 Answers

Question regarding this line of code 1 Answer

Is there a performance difference between Unity's Javascript and C#? 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges