Design Pattern Part 2: Full Serialisable

In this part we serialise the class before by using the keyword [Serializable] in the class definition.

FullySerialized.png
What Inspector Shows
Full Serialisation2.png
What Unity Serializes

Previous: Part 1: Unserialisable Class Next: Part 3: Private Variables

Things to Watch:

  1. A class marked with [Serializable] will serialize values by “Copy”. That means that it will behave like a struct.

Problems with this Approach:

  1. All assumptions are stored inside the scene file. This inreases the game file size and the scene loading time. All those could have been “inlined” OnEnable() method like in Unserialised Pure Class Approach. Redudancy in code converted into redudancy in scene file.
  2. Scene designer has to manually bind references by hand. Resulting in loss of productivity. This is prone to error as there is no guarantee that the component will have the exact same reference, a noob client could modify it in Inspector to a wrong value. Too many needless details given to Scene Designer = Prone to mistake.

namespace Disanity.Test
{
    using System;
    using UnityEngine;

    /// Requirement: Each Type of Field, Known ahead of time.
    public class DesignPatternFullySerializable : MonoBehaviour
    {
        public float Speed;
        public fMovementDirection Move;
        public fMoveConstantVelocity Velocity;

        private void FixedUpdate()
        {
            var direction = Move.Direction() * Speed;
            if (direction.magnitude > 0f) Debug.Log("Move Direction: " + direction);
            Velocity.Move(direction);
        }
    }

    [Serializable]
    public class fMovementDirection
    {
        public Transform Transform;

        public Vector3 Direction()
        {
            //read movement input
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");

            var movementDirection = v * Transform.forward + h * Transform.right;
            Util.NormaliseIfAboveOne(ref movementDirection);
            return movementDirection;
        }
    }

    [Serializable]
    public class fMoveConstantVelocity
    {
        public Rigidbody rb;
        public bool IgnoreForces;

        /// Used to track changes to current velocity
        private Vector3 _oldVelocity;

        public void Move(Vector3 direction)
        {
            if (rb.isKinematic)
            {
                rb.MovePosition(rb.position + direction);
            }
            else
            {
                if (IgnoreForces)
                {
                    rb.velocity = direction;
                }
                else
                {
                    var newSpeed = rb.velocity - _oldVelocity;
                    rb.velocity = newSpeed + direction;
                    _oldVelocity = direction;
                }
            }
        }
    }
}
Advertisements

2 thoughts on “Design Pattern Part 2: Full Serialisable

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s