Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  6. Unity 2017.2 beta is now available for download.
    Dismiss Notice

Using code tags properly

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

  1. Adam-Buckner

    Adam-Buckner

    Unity Technologies

    Joined:
    Jun 27, 2007
    Posts:
    5,661
    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 which automatically tags a selection as code.

    That being said, however, 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.  
    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);
    }
    }
    }
     
    Last edited: Mar 16, 2015
  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
     
    jtsmith1287 likes this.
  3. Adam-Buckner

    Adam-Buckner

    Unity Technologies

    Joined:
    Jun 27, 2007
    Posts:
    5,661
    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...
     
    brian-wanamaker likes this.
  4. jackie0100

    jackie0100

    Joined:
    Jan 31, 2012
    Posts:
    389
    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:
    290
    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:
    31,311
    You can just type the code tags yourself; you don't need to switch to anything.

    --Eric
     
  8. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    290
    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 :)
     
    klausbous likes this.
  10. AlanGameDev

    AlanGameDev

    Joined:
    Jun 30, 2012
    Posts:
    436
    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!!
     
  11. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    938
    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:
    503
    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

    Unity Technologies

    Joined:
    Jun 27, 2007
    Posts:
    5,661
    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

    Unity Technologies

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

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

    We can still manually insert out code tags:
    [​IMG]

    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

    Unity Technologies

    Joined:
    Jun 27, 2007
    Posts:
    5,661
    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:
    503
    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

    Unity Technologies

    Joined:
    Jun 27, 2007
    Posts:
    5,661
    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:
    31,311
    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,784
    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:
    372
  21. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,784
    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

    Unity Technologies

    Joined:
    Jun 27, 2007
    Posts:
    5,661

    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,462
    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 [​IMG]
    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:
    368
    +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

    Unity Technologies

    Joined:
    Jun 27, 2007
    Posts:
    5,661
    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:
    938
    If you are using Google Chrome then Ctrl+Shift+V will paste as plain text.
     
  30. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    2,438
    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:
    40
    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.  
    6.  
    7. Also more text that is not code gets added in as code too.
     
  32. IndieForger

    IndieForger

    Joined:
    Dec 31, 2012
    Posts:
    40
    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:
    935
    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.