Category Archives: C#

NGUI EventDelegate

I have a button whose function changes depending on which panel is currently being displayed.  When switching panels, this button stays on screen (think a NavBar).  I ran into this unexpected behavior today, that slowed me down.

If you change the contents of an EventDelegate list during a method called from that  EventDelegate list then its probable that your new method will get called right way when control is passed back to the Execute method of EventDelegate.  This looked like the OnClick method of a UIButton was not waiting for a release, so I went down that rabbit hole for a bit.

I ended up adding a short delay to the code that switches the current OnClick action to the new OnClick action, which solved the problem.

	button.onClick.Clear();
	//delay this by a frame, so it happens outside the EventDelegate processing
	callAfterDelay(0.01f,()=>{
		button.onClick.Add(action);
	});

Accessing a Struct as a SerializedProperty

I’m working on a custom editor extension that will support a palatalized color system for Unity3D UI elements (both NGUI and new Unity UI).  As part of this process I’m learning how to create custom Inspectors and PropertyDrawers.

I added a struct to one of my objects that replaced a simple Int, and I needed to be able to access the struct from an inspector.  Normally, to access the Int you’d create a SerializedProperty that would point to the public serialized int and then access the value with the .intValue property.

Once I updated the class to use a custom struct, the inspector no longer worked (obviously) and I needed to pull values from this struct instead of a primitive type.

The struct is defined as:

    [Serializable]
    public struct PBMapperIndex
    {
        public int intValue;
        public string indexName;
    }

It contains a string and an int, and most importantly, it’s marked as serializable. This means Unity will keep the struct as a serializable object.

To access it in the custom inspector, I used the following:

    SerializedProperty tintProp = serializedObject.FindProperty(tintIndex);

Where tinitIndex is declared as a public PBMapperIndex in my class.

Then to drill down and get the intValue for this property I used:

        SerializedProperty ixProp = tintProp.FindPropertyRelative(intValue);
        int  index = ixProp.intValue;

Two keys, mark the original struct as [Serializable] and then used FindPropertyRelative to pull out the ‘child’ property.

Love when things work as you expect

I always love it when you think something should work one way and that’s exactly the way it works.  Had that happen to me today with a Stack object.

I wanted to iterate over a List and remove any objects that are null. Now I know I can do a reverse iterate and remove any nulls as I come across them without breaking the List, but I wanted to try to do it differently, just because 😬.

As I iterated the List, I pushed the index of any entry that was null onto a Stack.  Then when I was done, I could use Foreach on the Stack to pull out each index and remove it from the list.  As I’d used a Stack, the entries are fetched in the order of last-in, first-out, even in a Foreach loop.  Thought that might be the case, and was happy to find out it was.

Thought I’d share.

Extending the Unity3D Color struct

I’m working on a convenience class that will allow me to specify the color used by an NGUI item by the SVG/HTML named color list (http://www.december.com/html/spec/colorsvg.html).

I was hoping I could extend the standard Unity3D Color class by adding a static method that could be used to update the color values, however as Color is a struct and not a class, you can’t update the value in an extension method as a copy is passed.

    public static void ColorWithName(this Color color,
     PBColor.ColorNames name)
    {
        color = PBColor.FromName(name);
    }

This will not work as expected.  The var color points to a copy of the calling color struct and not the actual struct, so any changes are discarded 🙁

I ended up with this:

    public static void ColorWithName(this UIWidget widget,
     PBColor.ColorNames name)
    {
        widget.color = PBColor.FromName(name);
    }

Not as convenient as I wanted, but it gets the job done as below:

    widget.ColorWithName(ColorNames.GoldenRod);

where widget is an NGUI.UIWidget object.

Unity 3D c# and lack of Tuple

Unfortunately the compiler used for Unity 3D does not support Tuples.  Tuples are a convenient way to lump a couple of types together into one object without creating a class.

So, if like me you needed a list to contain a string and a float for one entry, you could do so like this:

    public List<Tuple<string,float>> things;

Then you could access the parts of the item, add both parts at the same time, etc.

There are a bunch of class definitions available that will let you use something very similar to Tuple in Unity.  I didn’t want to go to that effort, so I ended up with using an ArrayList.  This will hold multiple objects of different types in a nice ordered array.

    public List<ArrayList> things;

Now each item in the list is an array and I can get at the contents using normal array syntax.

     ArrayList thing = things[0];
     label.text = thing[0as string;
     duration = (float)thing[1];

There’s no error checking here, but you get the basic idea.