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 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": This will bring up an "Insert" window were we can paste our code and choose our language: This will insert this code with Forum Markup that looks like this: Note: that when manually inserting code, we can choose the language by using "=CSharp" This is the final code in the post: Code (CSharp): 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); } } } 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); } } }
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): //code tags used with all lowercase letters
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...
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): // OMG for(i=0;i<Mathf.Infinity;i++){ Body.head.speak("Thanks alot"); } //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!!
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): // OMG for(i=0;i<Mathf.Infinity;i++){ Body.head.speak("Thanks alot"); } //Thank you :) 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
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
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.
_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": This will bring up an "Insert" window were we can paste our code and choose our language: This will insert this code with Forum Markup that looks like this: Note: that when manually inserting code, we can choose the language by using "=CSharp" This is the final code in the post: Code (CSharp): 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); } } } Hope this helps!
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...
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 ^^')
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"!
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
Thanks... Anyway, I'll just use the insert method. Testing: Code (JavaScript): var poopiness = 0; var desiredPoopiness = 3; if(poopiness > desiredPoopiness){goLoco();}
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.
+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?
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.
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.
It would be nice to have a closing tag validation added perhaps so we can avoid something like that... Code (csharp): function ABC() { // it actually is between code tags, just not closed properly } [code] Also more text that is not code gets added in as code too.
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
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.
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.
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.
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.
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.
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?
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!!!
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.
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
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!
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: 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:
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.
I've posted quite a lot of code, using code tags. It's technically retained: Your web browser isn't displaying the spaces since that's how HTML works. It would be possible to convert them to 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