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

Official Using code tags properly

Discussion in 'Scripting' started by Adam-Buckner, Jul 16, 2012.

Thread Status:
Not open for further replies.
  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    If you see someone who needs to know about code tags, please link them to this post:

    Please use code tags when posting code.

    You can "tag" your code by typing Screenshot 2015-03-16 10.33.58.png around your code.

    There is no overt "Code" tag button that automatically tags a selection as code.

    That being said, however, we can Insert code directly into the post using the "Insert Code" button:


    This will bring up an "Insert" window were we can paste our code and choose our language:
    Screenshot 2014-06-04 12.39.53.png

    This will insert this code with Forum Markup that looks like this:
    Screenshot 2014-06-04 12.43.25.png
    Note: that when manually inserting code, we can choose the language by using "=CSharp"

    This is the final code in the post:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Explode : MonoBehaviour {
    5.  
    6.     public GameObject explosion;
    7.     public ParticleSystem[] effects;
    8.  
    9.     void OnCollisionEnter2D (Collision2D collision) {
    10.         if (collision.gameObject.tag == "Hat") {
    11.             Instantiate (explosion, transform.position, transform.rotation);
    12.             foreach (var effect in effects) {
    13.                 effect.transform.parent = null;
    14.                 effect.Stop ();
    15.                 Destroy (effect.gameObject, 1.0f);
    16.             }
    17.             Destroy (gameObject);
    18.         }
    19.     }
    20. }
    21.  
    Note: the code is linked to the documentation with hyperlinks, so if you need to find out more about any piece of pasted code, follow these links to the documentation.

    Hope this helps!

    -

    If you don't use code tags, the code can be difficult to read:

    using UnityEngine;
    using System.Collections;

    public class Explode : MonoBehaviour {

    public GameObject explosion;
    public ParticleSystem[] effects;

    void OnCollisionEnter2D (Collision2D collision) {
    if (collision.gameObject.tag == "Hat") {
    Instantiate (explosion, transform.position, transform.rotation);
    foreach (var effect in effects) {
    effect.transform.parent = null;
    effect.Stop ();
    Destroy (effect.gameObject, 1.0f);
    }
    Destroy (gameObject);
    }
    }
    }
     

    Attached Files:

    Last edited by a moderator: Mar 4, 2022
  2. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    I think maybe a bbcode button for the code tags should be added to the quick reply it would make things alot easier.. but also the code tags can be written in all lower case such as this(Of course without the quotes)
    "["code][/code"]"
    so people could also hand write them..

    Code (csharp):
    1. //code tags used with all lowercase letters
     
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Having code tags in the quick reply is on my features request list. I hope the webdev team will have time when Unity Developer Network settles in...

    /me wonders if it is worth a point on feedback...
     
    MBYahooo and brian-wanamaker like this.
  4. Jacksendary

    Jacksendary

    Joined:
    Jan 31, 2012
    Posts:
    408
    FINALLY!!! people lately have been spammed code blocks all over the threads.... good post ^^
     
  5. Meppiel

    Meppiel

    Joined:
    Jul 26, 2012
    Posts:
    1
    Excellent post!
     
  6. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    Yeah!!! We need it. i don't want to switch to Advance to just to add CODE..
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can just type the code tags yourself; you don't need to switch to anything.

    --Eric
     
    jordangrant3d and _Prism_ like this.
  8. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    hmmm.. Thanks for Trick... Eric. I'll do.
     
  9. ForUrSoul

    ForUrSoul

    Joined:
    Oct 25, 2013
    Posts:
    1
    Code (csharp):
    1. //  OMG
    2.  
    3. for(i=0;i<age;i++){
    4.      Body.head.speak("Thanks alot");
    5. }
    6.  
    7. //Thank you :)
     
  10. AlanGameDev

    AlanGameDev

    Joined:
    Jun 30, 2012
    Posts:
    437
    I echo that... Code tag shortcut on quick reply please! I don't want to type bb tags any longer, that's sooo 2000's :).

    Fixed that for you
    Code (csharp):
    1. //  OMG
    2.  
    3. for(i=0;i<Mathf.Infinity;i++){
    4.      Body.head.speak("Thanks alot");
    5. }
    6.  
    7. //Thank you :)
    Infinite thanks that is.

    OFF:
    Also, the "+ Reply to Thread" button should be gone when you click it... I've clicked that many times thinking it was the "Post Reply" button and BAM! reply is gone! BAM, BAM, BOO!!
     
    coatline likes this.
  11. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    It would be really nice if the line-spacing issue was fixed when copying and pasting source code from code boxes (copied from above):
    Code (csharp):
    1.  
    2. //  OMG
    3.  
    4.  
    5.  
    6. for(i=0;i<Mathf.Infinity;i++){
    7.  
    8.      Body.head.speak("Thanks alot");
    9.  
    10. }
    11.  
    12.  
    13.  
    14. //Thank you :)
    15.  
    It inserts extra new lines.

    These extra new lines do not occur when copying and pasting code from the printable version of threads:
    http://forum.unity3d.com/printthread.php?t=143875&pp=20&page=1

    Also, I think you guys should replace the default logo in the printable version with the Unity logo ;)
     
  12. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    The new forum theme is really nice but where did the CODE tag button go ? It's most of the time already hard enough to read people's code here but without it easily accessible it's a pain in you know where.

    Would be nice if everyone could have the ability to edit a post but only to add CODE tags when missing :D
     
  13. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This is a question I was just asking myself today.

    They can be manually added, at this point, but I'll try to find new info and post it here.
     
  14. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    _met44:

    (afaict) There is no overt "Code" tag button, which automatically tags a selection as code.

    We can still manually insert out code tags:


    BUT!

    That being said, we can Insert code directly into the post using "Insert/Code":
    Screenshot 2014-06-04 12.36.27.png

    This will bring up an "Insert" window were we can paste our code and choose our language:
    Screenshot 2014-06-04 12.39.53.png

    This will insert this code with Forum Markup that looks like this:
    Screenshot 2014-06-04 12.43.25.png
    Note: that when manually inserting code, we can choose the language by using "=CSharp"

    This is the final code in the post:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Explode : MonoBehaviour {
    5.  
    6.     public GameObject explosion;
    7.     public ParticleSystem[] effects;
    8.  
    9.     void OnCollisionEnter2D (Collision2D collision) {
    10.         if (collision.gameObject.tag == "Hat") {
    11.             Instantiate (explosion, transform.position, transform.rotation);
    12.             foreach (var effect in effects) {
    13.                 effect.transform.parent = null;
    14.                 effect.Stop ();
    15.                 Destroy (effect.gameObject, 1.0f);
    16.             }
    17.             Destroy (gameObject);
    18.         }
    19.     }
    20. }
    21.  
    Hope this helps!
     
    _met44 likes this.
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    On a side note, if you copy the code and paste it:

    It pastes into Monodevelop without line numbers or added spaces between lines.
    It pastes into TextEdit on the Mac with line numbers and coloured text with the hyperlinks to the docs intact!
    It pastes into Mac Mail.app with line numbers and coloured text with non-functional hyperlinks...
     
    _met44 likes this.
  16. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Thank you very much Adam, you must be busy and still took the time to check it, it's very nice.

    I would never have found it... However, I didn't stop using the tag myself because of the hiden button, I simply wrote it manually. I was more worried junior users of the forum would never find it... (would be best as a red blinking CODE button actually ^^')
     
  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I do see your point _met44! I gather Aurore did try hard to get an easy "code tag" feature, but this new software package wasn't that forgiving. At least we do have "Insert Code"!
     
    petipois26 likes this.
  18. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Only if you use rich text; if you use plain text, then it gets pasted as normal without line numbers. It would be nice if there was a "select all code" button though.

    --Eric
     
  19. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    Thanks...

    Anyway, I'll just use the insert method.

    Testing:

    Code (JavaScript):
    1. var poopiness = 0;
    2. var desiredPoopiness = 3;
    3.  
    4. if(poopiness > desiredPoopiness){goLoco();}
     
    Last edited by a moderator: Jun 9, 2014
  20. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
  21. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    Mod or somebody changed my post, I was saying the same thing. Hey guys! Change the original post! :p
     
    Magiichan likes this.
  22. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664

    I am not sure why this isn't working for you as it seems to work for us:
    Screenshot 2014-06-20 14.09.04.png
     
  23. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    The CODE image doesnt show for me either in Chrome (v 35.0.1916.153 m), but does in Firefox, IE and Safari. If I open the TinyGrab URL in a new tab it works in Chrome, but for some reason doesn't want to embed.

    Edit: Testing this
    Yup works. Seems that directly linking the image works on Chrome, but resolving the TinyGrab URL is the issue. Odd one, but at least you can work around it.
     
    Last edited: Jun 26, 2014
    sbroscious and Magiichan like this.
  24. sbroscious

    sbroscious

    Joined:
    Jun 25, 2014
    Posts:
    4
    QFT
     
  25. zXSmashXz

    zXSmashXz

    Joined:
    Nov 2, 2014
    Posts:
    15
    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.        if(input.getkeydown(keycode.space))
    5. }
    6.  
    Just testing this out :)
     
  26. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    +1 for something like this. It pastes the extra lines and line numbers into Visual Studio, which really makes it hard to try and help some folks with their code issues even when they posted it nicely in code tags. A simple copy to clipboard without the line numbers would be very helpful.

    @Adam Buckner Any chance of getting that enhancement?
     
  27. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This is on the roadmap, but I'm unclear when the web-dev team will get to it.

    I've been pestering them regularly, but no promises.

    Cleaning the text by pre-pasting it as plain text somewhere, and then re-copy / pasting it to the destination is the best I can suggest.
     
  28. _Evasi0n

    _Evasi0n

    Joined:
    Apr 13, 2015
    Posts:
    9
    i don't get it , can you do this in MonoDevelop
     
  29. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    If you are using Google Chrome then Ctrl+Shift+V will paste as plain text.
     
  30. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Request: can we have a button in the UI that spits out a "please use code tags" message? I'm getting tired of writing it all the time. Mabye a [CodeTagMissing] macro we could post that just generates a link to this thread with a nice message?

    Also,
    PureText is a nice little utility for Windows that does this for you - you get a "paste without formatting" hotkey, and an icon you can click to strip formatting from the text in the clipboad.
     
    gorbit99 likes this.
  31. IndieForger

    IndieForger

    Joined:
    Dec 31, 2012
    Posts:
    92
    It would be nice to have a closing tag validation added perhaps so we can avoid something like that...

    Code (csharp):
    1.  
    2. function ABC() {
    3.     // it actually is between code tags, just not closed properly
    4. }
    5. [code]
    6.  
    7.  
    8. Also more text that is not code gets added in as code too.
     
  32. IndieForger

    IndieForger

    Joined:
    Dec 31, 2012
    Posts:
    92
    There is obviously an Edit button but since I have seen that in quite few places I thought it would be good to flag it out.

    Thanks for the post @Adam-Buckner
     
  33. herosf2006

    herosf2006

    Joined:
    Nov 9, 2016
    Posts:
    4
    Code (CSharp):
    1.  
    2. function abc {
    3. int a = 0;
    4. }
    5.  
    why not change CSharp to C#?
     
  34. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Probably because the language to format is specified in the tag as "CSharp" and you can't use symbols in a tag. The display just shows the name of the language being formatted.
     
  35. Jasper15

    Jasper15

    Joined:
    Oct 10, 2016
    Posts:
    1
    Code (csharp):
    1.  
    2. //thank you
    3.  
     
  36. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    629
    Nice work.
    But why not more languages?
    There are needs for languages like PHP and JavaScript and some other languages related to Unity like Java, so it would be a good addition to Code Tags.

    Thanks.
     
  37. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Because you can't write Unity code in PHP, JavScript, or Java :)
     
    LaneFox likes this.
  38. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    629
    But We use them, for example when we want to share a Java code that is used for creating plugins and importing to Unity, we need to have a syntax highlighting for this language.
    And same for others, there are many syntax highlighters available out there that support tens of languages.
    This would be a good addition to Code Tags.

    Thanks.
     
  39. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The number of posts that use these languages and aren't completely unrelated to Unity (ie - "I'm posting to my PHP page with Unity why doesn't the PHP work?" isn't a Unity question, it's a PHP question and doesn't belong in this forum) are very, very few. And in terms of Java plugins for Android - again, you're writing an Android plugin and using it in Unity. If you have questions about the plugin code itself you'd probably be better served in an Android community.

    Given that, I honestly don't see a justification for the time expenditure and maintenance hassle of supporting it. It's time that could be better spent doing more worthwhile things in my mind - like making the links to the documentation inside code blocks more reliable and prevalent.
     
    hasanbayat likes this.
  40. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Also C# highlighting for Java would be 99% correct. It's not going to give you a nice color for @override... oh well. It's still readable.
     
  41. Deleted User

    Deleted User

    Guest

    Any idea why indentation is not implemented when we post C# code here without using the code tags? Would it be difficult to add this to the editor?
     
  42. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    You shouldn't be posting code here without code tags. :)
     
    coatline likes this.
  43. Deleted User

    Deleted User

    Guest

    Don't tell me, tell all the people who post code without code tags, which is about 99 % of the people posting code here.

    This is why indentation should be automatic; we wouldn't spend our time posting links to this thread and reminding people that THEY.MUST.USE.CODE.TAGS!!!
     
    Dave-Carlile likes this.
  44. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Well actually, logically, Anne is correct. Machines should correctly identify the intended purpose and present the information. However, this looks like a big job to do for it to behave reliably. Agree with the thought though.
     
    Deleted User likes this.
  45. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There's more to it than just indentation. But if people post code without indentation, that's how they wrote it. If you're asking the forums to properly format code, well, no, that's not going to happen. (Just to start with, try to define "properly", and immediately have everyone fighting about what that actually is.)

    --Eric
     
    SparrowGS likes this.
  46. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    No, if you copy-paste properly indented code into the forums, it gets de-indented. For some reason the forum software strips out all whitespace from the start of all lines.

    I wrote this line with four spaces in front of it
    I wrote this line with eight spaces in front of it.
    Very many spaces! This is supposed to be entirely to the right of the line above!
     
    Deleted User likes this.
  47. Deleted User

    Deleted User

    Guest

    Exactly what @Baste says below; it happens to him, it happens to me and it doesn't happen to you? Or you never posted code here?

    For example, here is what this simple script looks like in MonoDevelop:

    Capture.JPG

    and what it looks like if I post it here without code tags:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class ExampleBehaviourScript : MonoBehaviour
    {
    Renderer rend;

    void Start ()
    {
    rend = gameObject.GetComponent <Renderer> ();
    }

    // Update is called once per frame
    void Update ()
    {
    if (Input.GetKeyDown(KeyCode.R))
    {
    rend.material.color = Color.red;
    }
    if (Input.GetKeyDown(KeyCode.G))
    {
    rend.material.color = Color.green;
    }
    if (Input.GetKeyDown(KeyCode.B))
    {
    rend.material.color = Color.blue;
    }
    }
    }

    The indentation is respected in the editor but it disappears as soon as I click on "post".

    Image of the pasted script in the editor:

    Capture2.JPG
     
    Last edited by a moderator: Dec 21, 2017
  48. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It works fine if you post into code block though...
     
  49. Deleted User

    Deleted User

    Guest

    Same thing as using code tags: people do not use them. They paste their code, it looks fine in the editor, it no longer looks find once posted. There is something that must be fixed here.
     
  50. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I've posted quite a lot of code, using code tags. ;)

    It's technically retained:

    Screen Shot 2017-12-21 at 4.30.39 PM.png

    Your web browser isn't displaying the spaces since that's how HTML works. It would be possible to convert them to &nbsp; but as I mentioned, indentation is only part of it; it should really be posted as proper code.

    UnityAnswers will format copy-pasted code automatically, but only if every line is indented. The code you posted above would start out as regular text, then switch to code partway (which I guess is better than none?). But it also has a separate code tag icon in the editor, so it's more obvious; it's kind of buried here. It also has a live preview, which probably helps. I looked at all the questions that were on the front page just now, and of the ones that contained code, all but one had code posted properly. So unless that's a fluke, it seems to be working there.

    --Eric
     
    razamgar likes this.
Thread Status:
Not open for further replies.