Website may be up and down over next few months. I'm currently doing a complete overhaul of everything. Going back to simple individual .htm pages, new overall site theme, sanitizing and cleaning up html of all pages and blog posts, attempting to implement a new tooling and publishing system etc etc.

/// <summary>
/// Packs color components into a <see cref="uint"/> type.
/// </summary>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
/// <param name="a">The alpha component.</param>
/// <returns>Returns a uint representing the color.</returns>
public static uint Pack(int r, int g, int b, int a)
{
    return (((uint)r & 0xff)) | (((uint)g & 0xff) << 8) | (((uint)b & 0xff) << 16) | (((uint)a & 0xff) << 24);
}

/// <summary>
/// Unpacks a <see cref="uint"/> type into a color type.
/// </summary>
/// <param name="value">The packed color value to unpack.</param>
/// <returns>Returns a color type from the unpacked values.</returns>
public static Color Unpack(uint value)
{
    var r = (byte)(value);
    var g = (byte)(value >> 8);
    var b = (byte)(value >> 16);
    var a = (byte)(value >> 24);

    return new Color(r, g, b, a);
}

If your Unity code requires conditional compilation symbols to be present this bit of code may come in handy. After unity compiles your scripts it executes any classes that have the InitializeOnLoad attribute. You can call the SetupConditionalCompilation method provided in the code snippet below to ensure that the conditional compilation symbols persist in your unity project. If a symbol was not present and was added it will write out a notification in the unity console.

ConditionalStartup

[InitializeOnLoad]
public class GridMappingSetup
{
    static GridMappingSetup()
    {
        var types = new[] { BuildTargetGroup.Standalone, BuildTargetGroup.WebPlayer };
        var toInclude = new[] { "CBXControls", "GridMapping", "QuickTools", "ToolService", "TileMaterialCreation" };

        SetupConditionalCompilation(types, toInclude);
    }
}

public static void SetupConditionalCompilation(BuildTargetGroup[] platformTargets, string[] symbolsToInclude)
{
    foreach (var type in platformTargets)
    {
        var hasEntry = new bool[symbolsToInclude.Length];
        var conditionals = PlayerSettings.GetScriptingDefineSymbolsForGroup(type).Trim();
        var parts = conditionals.Split(';');
        var changed = false;

        foreach (var part in parts)
        {
            for (int i = 0; i < symbolsToInclude.Length; i++)
            {
                if (part.Trim() == symbolsToInclude[i].Trim())
                {
                    hasEntry[i] = true;
                    break;
                }
            }
        }

        for (int i = 0; i < hasEntry.Length; i++)
        {
            if (!hasEntry[i])
            {
                conditionals += (String.IsNullOrEmpty(conditionals) ? String.Empty : ";") + symbolsToInclude[i];
                changed = true;
            }
        }

        PlayerSettings.SetScriptingDefineSymbolsForGroup(type, conditionals);

        if (changed)
        {
            Debug.Log(String.Format("Updated player conditional compilation symbols for {0}: {1}", type, conditionals));
        }
    }
}

Here is a C# script for visualizing render bounds. Just attach it to a game object and it will draw the render bounds of the game object and all it’s children. Handy for debugging!

Render Bounds Script Preview

public class RendererBoundsGizmo : MonoBehaviour
{
    public bool ShowCenter;

    public Color Color = Color.white;

    public bool DrawCube = true;

    public bool DrawSphere = false;

    /// <summary>
    /// When the game object is selected this will draw the gizmos
    /// </summary>
    /// <remarks>Only called when in the Unity editor.</remarks>
    private void OnDrawGizmosSelected()
    {
        Gizmos.color = this.Color;

        // get renderer bonding box
        var bounds = new Bounds();
        var initBound = false;
        if (CBX.Utilities.Helpers.GetBoundWithChildren(this.transform, ref bounds, ref initBound))
        {
            if (this.DrawCube)
            {
                Gizmos.DrawWireCube(bounds.center, bounds.size);
            }
            if (this.DrawSphere)
            {
                Gizmos.DrawWireSphere(bounds.center, Mathf.Max(Mathf.Max(bounds.extents.x, bounds.extents.y), bounds.extents.z));
            }
        }

        if (this.ShowCenter)
        {
            Gizmos.DrawLine(new Vector3(bounds.min.x, bounds.center.y, bounds.center.z), new Vector3(bounds.max.x, bounds.center.y, bounds.center.z));
            Gizmos.DrawLine(new Vector3(bounds.center.x, bounds.min.y, bounds.center.z), new Vector3(bounds.center.x, bounds.max.y, bounds.center.z));
            Gizmos.DrawLine(new Vector3(bounds.center.x, bounds.center.y, bounds.min.z), new Vector3(bounds.center.x, bounds.center.y, bounds.max.z));
        }

        Handles.BeginGUI();
        var view = SceneView.currentDrawingSceneView;
        var pos = view.camera.WorldToScreenPoint(bounds.center);
        var size = GUI.skin.label.CalcSize(new GUIContent(bounds.ToString()));
        GUI.Label(new Rect(pos.x - (size.x / 2), -pos.y + view.position.height + 4, size.x, size.y), bounds.ToString());
        Handles.EndGUI();
    }
}

And also the code for the GetBoundsWithChildren method.

/// <summary>
/// Gets the rendering bounds of the transform.
/// </summary>
/// <param name="transform">The game object to get the bounding box for.</param>
/// <param name="pBound">The bounding box reference that will </param>
/// <param name="encapsulate">Used to determine if the first bounding box to be calculated should be encapsulated into the <see cref="pBound"/> argument.</param>
/// <returns>Returns true if at least one bounding box was calculated.</returns>
public static bool GetBoundWithChildren(Transform transform, ref Bounds pBound, ref bool encapsulate)
{
    var bound = new Bounds();
    var didOne = false;

    // get 'this' bound
    if (transform.gameObject.renderer != null)
    {
        bound = transform.gameObject.renderer.bounds;
        if (encapsulate)
        {
            pBound.Encapsulate(bound.min);
            pBound.Encapsulate(bound.max);
        }
        else
        {
            pBound.min = bound.min;
            pBound.max = bound.max;
            encapsulate = true;
        }

        didOne = true;
    }

    // union with bound(s) of any/all children
    foreach (Transform child in transform)
    {
        if (GetBoundWithChildren(child, ref pBound, ref encapsulate))
        {
            didOne = true;
        }
    }

    return didOne;
}

xDocs finally has a download

Published 12/31/2012 by createdbyx in News
Tags: ,

I created the xDocs page July 11, 2008 and I am only now realizing that I had never provided an actual download to the control.  I r phail saucie  QQ

Jebus I’m bad for doing that! I can’t believe how much stuff I have started on this site only to have it never completed.


I have made a few minor changes to my Tile material Creation Window.

  • Provided a download link to the second release of the tool.
  • Added inset field to tweak the selection rectangle by a small amount to help clip off any unwanted pixels bleeding over from adjacent tiles.
  • Fixed a small bug where selection rectangles were not being drawn exactly where they should be

I have created a tool to assit in creating materials from tiles in a tile set. See the Tile Material Creation Window page for *.unitypackage download and more information.


You can specify the RequireComponent attribute on a class that inherits from MonoBehavior and Unity will add the specified component if it is not already present when you add the script to the game object.

[RequireComponent(typeof(RigidBody))]
public class SomeBehaviorScript : MonoBehaviour
{
}

Are you working on a large project but dislike how many objects you have to weed through in the hierarchy window? You can create editor scripts that change the objects Object.hideFlags property to hide or show the object in the hierarchy window.

var item = Selection.activeGameObject;
if ((item.hideFlags & HideFlags.HideInHierarchy) == HideFlags.HideInHierarchy)
{
                item.hideFlags = item.hideFlags ^ HideFlags.HideInHierarchy;
                item.active = !item.active;
                item.active = !item.active;
}

Notice that item.active = !item.active is specified twice. Currently in unity 3.5 you need those 2 lines of code for the game object to hide and show properly in the hierarchy window.


If your suddenly finding that Unity will instanty terminate while running your code, check if you are invoking any infinite recusion loops. This has recently happened to me and it was a bit of a pain to track down with Unity 3.5 instantly terminating. Hopfully the Unity team will handle the infinite recustion issue in a future version.


GAH! Google is such phail, Bing is no better either. Been trying to find out how to use EF + SqlCe without having to specify any settings in my web.config.

Finally I found this page on unit testing that pointed be in the right direction.

And here is how to do it in code!

    
    public class testModel
    {
        [Key]
        [Required]
        public int ID { get; set; }
        [Required]
        public int SomeID { get; set; }
    }

    public class MyClass : System.Data.Entity.DbContext
    {
        public MyClass() { }
        public MyClass(string filename) : base(filename) { }
        
        public DbSet TableTest { get; set; }
    }
   
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            var path = this.Context.Server.MapPath("~/bin/");
            Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0", path, "");
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges());
            var tmp = new MyClass("test.sdf");
            tmp.TableTest.Add(new testModel() { SomeID = 5 });
            tmp.SaveChanges();

            return this.View(); 
        }
    }

Created by: X

Just another personal website in this crazy online world

Name of author Dean Lunz (aka Created by: X)
Computer programming nerd, and tech geek.
About Me -- Resume