Sunday, January 03, 2016

Notes on DragAndDrop in the Unity Editor

The documentation for the Unity DragAndDrop class is a little lacking, so here are the notes from my experiments into how the whole things works and integrates with OnGUI.

* If the current event in your OnGUI function is MouseDrag, first call:

DragAndDrop.PrepareStartDrag ()

then assign a value to DragAndDrop.objectReferences or DragAndDrop.paths and then call:

DragAndDrop.StartDrag(string title)

* If the current event is DragUpdated, you need to check if the drag operation can succeed or fail, then set:

DragAndDrop.visualMode = DragAndDropVisualMode.*

to an appropriate value. Important:If the drag operation could be accepted at this point, call:


which will allow the DragPerform event to occur correctly if the mouse button is released.

* If the current event is DragPerform, the drag operation has succeeded and you can perform whatever logic is needed. No further DragAndDrop calls need to be made.

* If the current event is DragExited, you know that the drag operation has ended and you can perform any cleanup needed. Note: This event always occurs, even when DragPerform has completed.

Wednesday, December 09, 2015

Implicit typing in C# using "var"

I use the var keyword everywhere. Someone told me today that this is a bad practice, because "sometimes you can't tell what type it is".

Well, I disagree. I believe we generally don't care what type a variable is, we care about how we use it, the interface it provides to the programmer. For example:
foreach(var i in myListOfThings) {
I don't care what typeof(i) is, I just want to know it has a .name attribute. If I change the type later, I probably still want it to have a .name attribute, and the var keyword lets this code keep working. Also:
var v = new Vector3(0,0,0);
Vector3 v = new Vector3(0,0,0);
In this case, the Vector3 type declaration is redundant, it is simply repeating information already on the same line. Remember, repeating yourself in code is a Bad Thing. Later on if I realise my Vector3 variables need to be Vector2 variables, I need to change the type information in 2 places for each variable. Yuck.

Finally, var lets me write less characters, which often means less backspacing and retyping!
var now = System.DateTime.Now;
System.DateTime now = System.DateTime.Now;

This well written post describing more advantages of implicit typing. To Summarise:
  • It induces better naming for local variables.
  • It induces better API.
  • It induces variable initialisation.
  • It removes code noise.
  • It doesn't require the using directive.

Monday, November 23, 2015

MOG Server & Client for Unity.

Sometime ago I put my generic multiuser game server (NetWrok) on GitHub. It's written in Python3, uses websockets, postgresql and runs on most platforms.

I have now released the Unity NetWrok Client code.

Wednesday, November 11, 2015

I made a Mosasaur.

Wings3D for modelling. Cheetah3D for UV unwrap and rigging. Substance Painter for Texturing.

by simonwittber
on Sketchfab

Tuesday, October 27, 2015

Python on Hardware

Just bought a MicroPython pyboard. Plugged it in, and I have a python prompt. Never thought this day would come. Amazing.

Popular Posts