Coding Grease Monkey

Flicker Lights in Unity

Here is a snippet of code, simply place on a light to make it flicker ;)

#region Licence

 

// /////////////////////////////////////////////////////////////////////

// FlickerLights.cs

// (C)2013 CodersElite Game Development. Written by EliteMossy

// ///////////////////////////////////////////////////////////////////

 

#endregion

 

#region Imports

 

using UnityEngine;

 

#endregion

 

public class FlickerLights: MonoBehaviour

{

    #region Fields

  public bool FlickerLights;

    public float FlickerOffTime = 2f;

    public float FlickerOnTime = 2f;

    public float FlickerRandomModifier = 0f;

   private Light m_Light;

    private float m_NextFlickerTime;

 

   #endregion

 

    #region Methods

 

    private void Start()

    {

        m_Light = light;

    }

 

    // Update is called once per frame

    private void Update()

    {

        if (FlickerLights)

        {

            if (Time.time > m_NextFlickerTime)

            {

                if (m_Light.enabled)

                {

                    m_NextFlickerTime += FlickerOffTime;

                    m_NextFlickerTime += Random.Range(-FlickerRandomModifier, FlickerRandomModifier);

                    m_Light.enabled = false;

                }

 

                else

                {

                    m_NextFlickerTime += FlickerOnTime;

                    m_NextFlickerTime += Random.Range(-FlickerRandomModifier, FlickerRandomModifier);

                    m_Light.enabled = true;

                }

            }

        }

   }

}

    #endregion

Play random song/music all the time

So you want to play a random song from an array of AudioClips?

Then here is a simple soloution for this

    public AudioClip[] AudioClips;

    private void Update()
    {
        if (!audio.isPlaying && AudioClips.Length > 0)
        {
            audio.clip = AudioClips[Random.Range(0, AudioClips.Length)];
            audio.Play();
        }
    }

Then simply assign the audio clips in the inspector!

Sort Raycast Hit array in order of closest first

Here is a little snippet of code to sort a Raycast Hit array in order of closest first.

        RaycastHit[] raycast_hits = Physics.RaycastAll(
            this.transform.position, this.transform.forward, 100, layermask);
        Array.Sort(raycast_hits, RaycastSort);

and the actual snippet of code that does the magic is

private int RaycastSort(RaycastHit x, RaycastHit y) { return (int) x.distance - (int) y.distance; }

Hope you find this useful :)

Crypto Utilities – MD5 and SHA hashing

Here are 3 useful methods for calculating a MD5 or SHA hash. The class is static meaning you don’t need to instaniate the class.

#region Usings

using System;
using System.Security.Cryptography;
using System.Text;

#endregion

//(C)2013 Daniel Moss (EliteMossy)
// http://www.mrmoss.net

public static class CryptoUtils
{
    private static readonly SHA1CryptoServiceProvider s_SHA1Provider;

    private static readonly MD5CryptoServiceProvider s_MD5Provider;

    static CryptoUtils()
    {
        s_SHA1Provider = new SHA1CryptoServiceProvider();
        s_MD5Provider = new MD5CryptoServiceProvider();
    }

    public static string CalcMD5Hash(string input)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(input);
        byte[] num_array = s_MD5Provider.ComputeHash(bytes);
        return BitConverter.ToString(num_array).Replace("-", string.Empty);
    }

    public static byte[] CalcMD5HashAsBytes(string input)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(input);
        byte[] num_array = s_MD5Provider.ComputeHash(bytes);
        return num_array;
    }

    public static string CalcSHA1Hash(string input)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(input);
        byte[] num_array = s_SHA1Provider.ComputeHash(bytes);
        return BitConverter.ToString(num_array).Replace("-", string.Empty);
    }
}

The first method CalcMD5Hash calculates a MD5 hash from the given string and returns a string.

The second method CalcMD5HashAsBytes calculates a MD5 hash from given string and returns it in a byte array.

The third method CalcSHA1Hash calculates a SHA1 hash from the given string and returns it as a string.

Hope you find them useful, and keep the (C) headers in tact for me :)

Destroy GameObject after certain time

This is just a little script to destroy a GameObject after a certain time

#region Imports

using UnityEngine;

#endregion

public class DestroyAfter : MonoBehaviour
{
    public float DestroyAfterTime = 15.0f;

    private void Start()
    {
        Destroy(gameObject, DestroyAfterTime);
    }
}

C#/Unity3D – Shuffling an Array

Sometime ago i got asked to shuffle an array in Unity3D using C#.

So i thought about it and came up with this

    public void Shuffle(int[] obj)
    {
        for (int i = 0; i < obj.Length; i++)
        {
            int temp = obj[i];
            int obj_index = Random.Range(0, obj.Length);
            obj[i] = obj[obj_index];
            obj[obj_index] = temp;
        }
    }

This only works for int[], but can be easily applied to string[], object[], GameObject[] etc. :)


   public void ShuffleGameObjects(GameObject[] obj)
   {
       for (int i = 0; i < obj.Length; i++)
       {
           GameObject temp = obj[i];
           int obj_index = Random.Range(0, obj.Length);
           obj[i] = obj[obj_index];
           obj[obj_index] = temp;
       }
   }

–EliteMossy