Sorting an Array using "Selection Sort" | C# | Unity Game Engine


Solution.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Solution : MonoBehaviour
{
    public int[] numbers = { 78, 55, 45, 98, 13 };

    void Start()
    {
        SelectionSort(numbers);
        Debug.Log("Sorted numbers : "+string.Join(",",numbers));
    }

    public void SelectionSort(int[] nums)
    {
        for (int i = 0; i < nums.Length - 1; i++)
        {
            int smallestNumIndex = i;
            for (int j = i + 1; j < nums.Length; j++)
            {
                if(nums[j]<nums[smallestNumIndex])
                    smallestNumIndex = j;
            }
            if(smallestNumIndex != i)
            {
                int temp = nums[i];
                nums[i] = nums[smallestNumIndex];
                nums[smallestNumIndex] = temp;
            }
        }
    }
}

Menu Items - Priority | Editor Scripting | C# | Unity Game Engine


MenuItems_Priority_Editor.cs
using UnityEngine;
using UnityEditor;

public class MenuItems_Priority_Editor
{
    [MenuItem("Priority Menu/Option 1", priority = 1)]
    public static void Option1Action()
    { }

    [MenuItem("Priority Menu/Option 2", priority = 101)]
    public static void Option2Action()
    { }

    [MenuItem("Priority Menu/Option 3", priority = 51)]
    public static void Option3Action()
    { }

    [MenuItem("Priority Menu/Option 4", priority = 2)]
    public static void Option4Action()
    { }
}

"Integer to Roman" Problem and its Solution | C# | Unity Game Engine


Solution.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Solution : MonoBehaviour
{
    public int number = 1;

    void Start()
    {
        Debug.Log("Roman => "+IntToRoman(number));
    }

    public string IntToRoman(int num)
    {
        var lookup = new Dictionary<int, string>
        {
            {1000, "M"},{900, "CM"},{500, "D"},{400, "CD"},{100, "C"},{90, "XC"},
            {50, "L"},{40, "XL"},{10, "X"},{9, "IX"},{5, "V"},{4, "IV"},{1, "I"}
        };

        string result = "";
        foreach (var pair in lookup)
        {
            if (num <= 0)
                break;
            int quotient = num / pair.Key;
            for (int j = 0; j < quotient; j++)
            {
                num -= pair.Key;
                result += pair.Value;
            }
        }
        return result;
    }
}

Menu Items - Hotkeys | Editor Scripting | C# | Unity Game Engine


MenuItems_Hotkeys_Editor.cs
using UnityEngine;
using UnityEditor;

public class MenuItems_Hotkeys_Editor
{
    //Hotkey Shift-Ctrl-Alt A
    [MenuItem("Hotkeys Menu/Create Cube #%&a")]
    public static void CreateCube()
    {
        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    }

    //Hotkey A
    [MenuItem("Hotkeys Menu/Option 2 _a")]
    public static void Option2Action()
    { }

    //Hotkey Left
    [MenuItem("Hotkeys Menu/Option 3 _Left")]
    public static void Option3Action()
    { }

    //Hotkey F1
    [MenuItem("Hotkeys Menu/Option 4 _F1")]
    public static void Option4Action()
    { }

    //Hotkey Home
    [MenuItem("Hotkeys Menu/Option 5 _Home")]
    public static void Option5Action()
    { }
}

''Container With Most Water" Problem and it's Solution | C# | Unity Game Engine


Solution.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Solution : MonoBehaviour
{
    public int[] heightArray = { 1, 8, 6, 2, 5, 4, 8, 3, 7 };

    void Start()
    {
        Debug.Log(MaxArea(heightArray));
    }

    public int MaxArea(int[] height)
    {
        int leftIndex = 0;
        int rightIndex = height.Length - 1;
        int maxArea = 0;

        while(leftIndex < rightIndex)
        {
            int area = Math.Min(height[leftIndex], height[rightIndex]) * (rightIndex - leftIndex);
            maxArea = Math.Max(maxArea, area);
            if (height[leftIndex] < height[rightIndex])
                ++leftIndex;
            else if (height[rightIndex] < height[leftIndex])
                --rightIndex;
            else
            {
                ++leftIndex;
                --rightIndex;
            }
        }

        return maxArea;
    }
}

Creating Menu Items | Editor Scripting | C# | Unity Game Engine


MenuItems_Editor.cs
using UnityEngine;
using UnityEditor;

public class MenuItems_Editor
{
    [MenuItem("Control Menu/Play")]
    public static void Play()
    {
        EditorApplication.EnterPlaymode();
    }

    [MenuItem("Control Menu/Stop")]
    public static void Stop()
    {
        EditorApplication.ExitPlaymode();
    }

    [MenuItem("Assets/Custom/Clear PlayerPrefs")]
    public static void DeleteAllPlayerPrefs()
    {
        PlayerPrefs.DeleteAll();
        EditorUtility.DisplayDialog("Cleared", "PlayerPrefs Cleared", "OK");
    }

    [MenuItem("Assets/Create/Cube at Center")]
    public static void CreateCenterCube()
    {
        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.transform.position = Vector3.zero;
    }
}

"Valid Parentheses" Problem and its Solution | C# | Unity Game Engine


Solution.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Solution : MonoBehaviour
{
    public string input = "()";

    void Start()
    {
        Debug.Log(IsValid(input));
    }

    public bool IsValid(string s)
    {
        var valid_pairs = new Dictionary<char, char> { { '(', ')' }, { '{', '}' }, { '[', ']' } };
        var openingBrackets = new Stack<char>();
        foreach (var _char in s)
        {
            if(valid_pairs.ContainsKey(_char))
            {
                openingBrackets.Push(_char);
            }
            else
            {
                if(openingBrackets.Count == 0 || _char != valid_pairs[openingBrackets.Peek()])
                    return false;
                openingBrackets.Pop();
            }
        }
        return openingBrackets.Count == 0;
    }
}