Unity 3D

From ThinkyWiki

Jump to: navigation, search


Lessons Learned

  • It can be more convenient to use Coroutines to drive behavior instead of Update() or FixedUpdate().
  • float Random.Range(x, y) returns x through y. int Random.Range(x, y) returns x through y-1.
  • OnTrigger() receives a Collider object, not a Collision object.
  • Destroy() can apply to a component. Use gameObject.Destroy() if you mean to destroy the object and not just the component.

Saving Game State

  • Move all saveable GameObjects into a disabled GameObject. This disables them without losing their enabled state. Any coroutines running on those GameObjects are stopped.
  • When the objects are moved out of the save container their OnEnable will fire. Use OnEnable to restore coroutines based on state or other conditions.

Coordinate Spaces

GUIText and GUITexture position proportionally with screen center at 0.5, 0.5. The transform of a GUIObject is not compatible with the transform of a world object. So, to align a GUI object with a world object, we have to convert world space coordinates into some camera projection's relative space.

GUI Scaling and Positioning

GUI objects have to be scaled to fit the resolution. The positioning system makes it easier to scale objects that position relative to screen perimeters. For my Defender clone I placed all GUI objects into a single hierarchy so that I could find them all and scale them at the start of the game. Unity 5 will offer smarter automatic scaling and positioning. Unity 4 doesn't have this, so we just scale after loading levels or instantiating.

Personal tools