How to do it...

The following code is for the Graph class:

  1. Create the backbone with the member values:
using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
 
public abstract class Graph : MonoBehaviour 
{ 
    public GameObject vertexPrefab; 
    protected List<Vertex> vertices; 
    protected List<List<Vertex>> neighbours; 
    protected List<List<float>> costs; 
    // next steps 
}
  1. Define the Start function:
public virtual void Start() 
{ 
    Load(); 
} 
  1. Define the Load function, mentioned previously:
public virtual void Load() { } 
  1. Implement the function for getting the graph's size:
public virtual int GetSize() 
{ 
    if (ReferenceEquals(vertices, null)) 
        return 0; 
    return vertices.Count; 
} 
  1. Define the function for finding the nearest vertex ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.