Tag Archives: CSharp

Quick Tip: Stay in Unity Play Mode while Coding

If you aren’t familiar with Unity, it’s a free game engine with a built-in game editor. I use Unity almost every day, and as you can imagine, I’m coding a lot. Now, when you are writing the majority of code for a game, you are testing things out. Perhaps you want to see how fast is fast enough, or how far is far enough. It can be a pain to enter and exit play mode constantly, but wait!

Unity has a nifty little feature that will recompile code and execute it, even while in play mode. You can easily switch over to Visual Studio (or MonoDevelop) and save your changes. Then you can “Alt + Tab” back over into Unity and quickly see your changes. The only downside to this is that, with large projects (10,000+ lines of C#), this can take a few seconds–even on a really fast machine. However, it’s invaluable when testing things, as you never have to leave your spot in play mode. 😉

Happy coding!

Get some color back in Visual Studio 2012 icons!

If you are still using Visual Studio 2010 (or older?), then you can just ignore this. Apparently, Microsoft’s UI design team decided to change the icons to a more “monochrome” color palette in VS 2012, so what you get is essentially gray icons for everything. These icons are a far cry from what we have come to know as save, open file, open folder, etc. However, I personally don’t really care to be honest, unlike most developers. If you seriously use Visual Studio 2012, then there is no reason you shouldn’t do these two things:

  • Learn those shortcuts! This is a must. Visual Studio has so many shortcuts that most actions take two keys plus a modifier! It sure beats having to look through context menus that are so big, they fill up more than 1,080 pixels vertically…
  • Customize the layout where everything is somewhere you can find immediately. Re-arrange the toolbars and memorize the icons’ respective locations. Even if you know all the shortcuts (unlikely), there are still some things I prefer doing with the mouse–namely opening files, as it’s easier to use the mouse to browse through the files.

If this still isn’t enough, there is always the NiceVS Visual Studio extension, which appears to bring back the colored icons so many people have come to miss so much. Use at your own risk, however, as I didn’t even bother to test this plugin. If Microsoft’s made a UI decision that coincides with the Metro UI (which everyone also seems to hate), it isn’t going away easily. There’s no sense in installing a plugin to try and undo what Microsoft has done, in my opinion.

Getting all Objects With the Same Script Attached in Unity

I’ve been working a lot in Unity lately, so I thought I should probably do a few tutorials on scripting in the game engine. One of the biggest helps I’ve gotten in scripting for the game engine (in which I use C#) is a nifty API function Unity provides that allows me to get all the objects with a specific type, i.e. game objects with a specific script attached. Since this is a quick tip, lets skip the behind-the-scenes and logic, and get to the code already!

MyScript[] myScriptObjects =
FindObjectsOfType(typeof(MyScript)) as MyScript[];

This specific function returns a System.Object array of all the Game Objects which have the script MyScript applied. We then cast it so we can get a strongly typed array from the get-go. From here you can access each of these objects in a loop, store them in a generic, etc. Very useful indeed!

Basically the FindObjectsOfType is the same as FindObjectOfType, however, it is probably more useful in most cases I could see it being used since it works with array’s. A good use of this is to perhaps obtain values from each script and add them to a dictionary or list so you can use them, since performing operations en masse could be done just as easily with tags, etc.

Quick Tip: Remove items with a Specific Value from C# Generic

My last quick tip covered a way to remove items in a generic based on numerical range. In this tutorial I’ll modify it to remove items based on a value–in this case, a string. So without further ado, let’s get to the code!

        // Define our list generics
        private static List<string> _listOfStrings = new List<string>();
        private static List<string> _listOfStringsCopy = new List<string>();

        static void Main(string[] args)
        {
            // Populate values
            PopulateList();

            // Copy so we have something to show what we've done.
            _listOfStringsCopy.AddRange(_listOfStrings);

            PrintListsFormatted();

            int itemNumberToRemove = 2;
            string removeString = "Item " + itemNumberToRemove;
            _listOfStrings.RemoveAll(s => s == removeString);

            Console.WriteLine();
            PrintListsFormatted();

            // Pause app so we can see everything
            Console.ReadKey();
        }

This program is basically the same as our old one, except for lines 15-17, where we decide what to remove. In this case, we are removing the item that is equal to the string we setup. This is very useful for day-to-day tasks that you use string Lists for. To get a better understanding of what’s going on in the lambada, simple remove the equals operator and replace it with a not equals operator. Then it removes everything but the removal string.

That’s it for this quick tip! Hope this can help you with your day-to-day coding! Below are the functions used in the main function:

        private static void PopulateList()
        {
            for (int i = 1; i <= 5; i++)
            {
                _listOfStrings.Add("Item " + i);
            }
        }
        private static void PrintListsFormatted()
        {
            Console.WriteLine("Working List with " + _listOfStrings.Count + " elements: ");
            foreach (string s in _listOfStrings)
            {
                Console.Write(s + " ");
            }

            Console.WriteLine();

            Console.WriteLine("\nCopied List with " + _listOfStringsCopy.Count + " elements: ");
            foreach (string s in _listOfStringsCopy)
            {
                Console.Write(s + " ");
            }

            Console.WriteLine();
        }

Quick Tip: Remove items within a Range from C# Generic

Recently I’ve just been so busy I’m finding it hard to simply sit down and write a tutorial. To get back into the swing of things, I just made a new project (I was already in Visual Studio…) and put this little bit together. It’s actually a technique I found on my own yesterday while working in an actual application. I just knew that there must be a better way to remove values within a set of parameters! For this quick example, I’ll use a list generic of type int where I populate with natural numbers up to 20.

Lets start with the main function, as that’s where most of the code is:

        // Define our list generics
        private static List<int> _listOfInts = new List<int>();
        private static List<int> _listOfIntsCopy = new List<int>();

        static void Main(string[] args)
        {
            // Populate values
            PopulateList();

            // Copy so we have something to show what we've done.
            _listOfIntsCopy.AddRange(_listOfInts);

            PrintListsFormatted();

            // Remove all numbers less than or equal to 15, while also being
            // greater than or equal to ten. (Removes 6 numbers)
            int max = 15;
            int min = 10;
            _listOfInts.RemoveAll(i => i <= max && i >= min);

            Console.WriteLine();
            PrintListsFormatted();

            // Pause app so we can see everything
            Console.ReadKey();
        }

To break it down a little, what we are essentially doing is on line 19. Notice the handy expression that really neatens things up for us. If you aren’t familiar with the C# lambda operator, we are basically just taking all the values and removing them if they within certain parameters. Think of the lambada as more of a boolean expression in this case. Picture an if statement if you are still having trouble realizing what this means.

Well, that’s it for the quick tip! Hope you find it useful! Below are the functions that aren’t in the main code block above:

        private static void PopulateList()
        {
            for (int i = 1; i <= 20; i++)
            {
                _listOfInts.Add(i);
            }
        }
        private static void PrintListsFormatted()
        {
            Console.WriteLine("Working List with " + _listOfInts.Count + " elements: ");
            foreach (int i in _listOfInts)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine();

            Console.WriteLine("\nCopied List with " + _listOfIntsCopy.Count + " elements: ");
            foreach (int i in _listOfIntsCopy)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine();
        }