Finding "Longest Common Prefix" string amongst an array of strings | C# | Unity Game Engine


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

public class Solution : MonoBehaviour
{
    public string[] strings = { "flower", "flow", "flight" };

    void Start()
    {
        Debug.Log("LCF => "+LongestCommonPrefix(strings));
    }

    public string LongestCommonPrefix(string[] strs)
    {
        if (strs == null || strs.Length == 0)
            return "";

        string prefix = strs[0];
        for (int i = 1; i < strs.Length; i++)
        {
            while(strs[i].IndexOf(prefix) != 0)
            {
                prefix = prefix.Substring(0, prefix.Length - 1);
                if (string.IsNullOrEmpty(prefix))
                    return "";
            }
        }
        return prefix;
    }
}

Adding Button in Custom Inspector | Editor Scripting | C# | Unity Game Engine


PlayerScript.cs
using UnityEngine;

public class PlayerScript : MonoBehaviour
{
    public int health = 100;
    public int maxHealth = 100;
}

PlayerScriptEditor.cs
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(PlayerScript))]
public class PlayerScriptEditor : Editor
{
    public override void OnInspectorGUI()
    {
        PlayerScript playerScript = (PlayerScript)target;
        playerScript.health = EditorGUILayout.IntField("Health", playerScript.health);
        playerScript.maxHealth = EditorGUILayout.IntField("Max-Health", playerScript.maxHealth);

        if(GUILayout.Button("Reset Values"))
        {
            playerScript.health = 100;
            playerScript.maxHealth = 100;
        }
    }
}

'Roman to Integer' 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 s = "III";

    void Start()
    {
        Debug.Log(s + " => " + RomanToInt(s));
    }

    public int RomanToInt(string s)
    {
        var map = new Dictionary<char, int> { { 'I', 1 }, { 'V', 5 }, { 'X', 10 }, { 'L', 50 }, { 'C', 100 }, { 'D', 500 }, { 'M', 1000 } };
        int num = 0;
        for (int i = 0; i < s.Length; i++)
        {
            if (i == 0 || map[s[i]] <= map[s[i - 1]])
            {
                num += map[s[i]];
            }
            else
            {
                num += map[s[i]] - 2 * map[s[i - 1]];
            }
        }
        return num;
    }
}

Use of "HelpBox" in Custom Inspector | Editor Scripting | C# | Unity Game Engine


PlayerScript.cs
using UnityEngine;

public class PlayerScript : MonoBehaviour
{
    public int health = 100;
    public int maxHealth = 100;
}

PlayerScriptEditor.cs
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(PlayerScript))]
public class PlayerScriptEditor : Editor
{
    public override void OnInspectorGUI()
    {
        PlayerScript playerScript = (PlayerScript)target;
        playerScript.health = EditorGUILayout.IntField("Health", playerScript.health);
        playerScript.maxHealth = EditorGUILayout.IntField("Max-Health", playerScript.maxHealth);

        if (playerScript.maxHealth > 0)
        {
            if(playerScript.maxHealth < playerScript.health)
                EditorGUILayout.HelpBox("Max Health is smaller than Health", MessageType.Warning);
        }
        else
        {
            EditorGUILayout.HelpBox("Max Health should be greater than 0", MessageType.Error);
        }
    }
}

'Two Sum' Problem and its Optimized Solution | C# | Unity Game Engine


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

public class Solution : MonoBehaviour
{
    public int[] nums = { 2, 7, 11, 15 };
    public int target = 9;

    void Start()
    {
        var result = TwoSum(nums, target);
        Debug.Log("Result => " + string.Join(',', result));
    }

    public int[] TwoSum(int[] nums, int target)
    {
        var lookup = new Dictionary<int, int>();
        for (int i = 0; i < nums.Length; i++)
        {
            int secondNum = target - nums[i];
            if(lookup.ContainsKey(secondNum))
                return new[] {lookup[secondNum], i};
            lookup[nums[i]] = i;
        }
        return System.Array.Empty<int>();
    }
}

Use of "DrawDefaultInspector" method in Custom Inspector | Editor Scripting | C# | Unity Game Engine


PlayerScript.cs
using UnityEngine;

public class PlayerScript : MonoBehaviour
{
    public int health = 100;
    public int maxHealth = 100;
}

PlayerScriptEditor.cs
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(PlayerScript))]
public class PlayerScriptEditor : Editor
{
    public override void OnInspectorGUI()
    {
        PlayerScript playerScript = (PlayerScript)target;
        DrawDefaultInspector();

        if (playerScript.maxHealth > 0)
            EditorGUILayout.LabelField("Health %", playerScript.health * 100 / playerScript.maxHealth + "%");
    }
}

Check if number is 'Palindrome Number' | C# | Unity Game Engine


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

public class Solution : MonoBehaviour
{
    public int x = 121;

    void Start()
    {
        Debug.Log(IsPalindrome(x));
    }

    public bool IsPalindrome(int x)
    {
        if(x<0 || (x%10==0 && x!=0))
            return false;

        int reversedNum = 0;
        while(x > reversedNum)
        {
            reversedNum = reversedNum * 10 + (x % 10);
            x /= 10;
        }

        return x == reversedNum || x == reversedNum / 10;
    }
}

Building a Custom Inspector | Editor Scripting | C# | Unity Game Engine


PlayerScript.cs
using UnityEngine;

public class PlayerScript : MonoBehaviour
{
    public int health = 100;
    public int maxHealth = 100;
}

PlayerScriptEditor.cs
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(PlayerScript))]
public class PlayerScriptEditor : Editor
{
    public override void OnInspectorGUI()
    {
        PlayerScript playerScript = (PlayerScript)target;
        playerScript.health = EditorGUILayout.IntField("Health", playerScript.health);
        playerScript.maxHealth = EditorGUILayout.IntField("Max-Health", playerScript.maxHealth);

        EditorGUILayout.Space();
        EditorGUILayout.BeginHorizontal("box");
        if (playerScript.maxHealth > 0)
        {
            EditorGUILayout.LabelField("Health %", playerScript.health * 100 / playerScript.maxHealth + "%");
        }
        EditorGUILayout.EndHorizontal();
    }
}

Two Sum 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[] nums = { 2, 7, 11, 15 };
    public int target = 9;

    private void Start()
    {
        var result = TwoSum(nums, target);
        Debug.Log("Result => "+string.Join(',',result));
    }

    public int[] TwoSum(int[] nums, int target)
    {
        for (int i = 0; i < nums.Length-1; i++)
        {
            for (int j = i + 1; j < nums.Length; j++)
            {
                int sum = nums[i] + nums[j];
                if(sum == target)
                    return new int[]{i, j};
            }
        }
        return new int[] { -1, -1 };
    }
}