# Quiz Generator Unity Asset

- Link: https://saguiitay.com/unity/quiz-generator/
- Published: 2018-06-08T07:48:05+03:00
- Author: Itay

## Overview

[Quiz Generator](https://assetstore.unity.com/packages/slug/113294?aid=1101lS3S)
is a system to generate an unlimited number of random questions. Quiz Generator 
can be used to generate questions for a unique and complete Trivia/Quiz game (such
as Geography Trivia and [The Beatles Trivia](https://saguiitay.com/games/the-beatles-trivia/)).
It can also be embedded inside another game and be exposed as a mini game.

### Features

 * Fully commented C# code.
 * Perfect for creating trivia games
 * Contains a fully working example!
 * Contains randomization engine, that ensures unique questions!
 * Works on all platforms, PC, Mac, iOS, Android, etc.
 * Easy integration with [Puppeteer’s Triva Quiz Game](https://assetstore.unity.com/packages/templates/systems/trivia-quiz-game-template-45521?aid=1101lS3S)(
   [READ DOCS](https://saguiitay.com/quiz-generator-and-trivia-quiz-game/))

**[Purchase Quiz Generator from the Unity Assets Store here >>](https://assetstore.unity.com/packages/slug/113294?aid=1101lS3S)**

## Screenshots

![Quiz Generator Screenshot 1](https://i0.wp.com/www.saguiitay.com/wp-content/uploads/
Screenshot1-18-1024x485.png?ssl=1 "Quiz Generator Unity Asset 1")

![Quiz Generator Screenshot 2](https://i0.wp.com/www.saguiitay.com/wp-content/uploads/
Screenshot2-18.png?ssl=1 "Quiz Generator Unity Asset 2")

![Quiz Generator Screenshot 3](https://i0.wp.com/www.saguiitay.com/wp-content/uploads/
Screenshot3-17-1024x479.png?ssl=1 "Quiz Generator Unity Asset 3")

## How to use Quiz Generator

### Define your object model

Decide on 2 or more objects that will be the base of your data set and define the
relations between them. For example:

 * Actor
 * Character
 * Movie

And their relations:

 * An Actor portrays a Character
 * An Actor appears in a Movie
 * A Character appears in a Movie

And of course, you have their completing relations:

 * A Character is portrayed by an Actor
 * A Movie features an Actor
 * A Movie features a Character

### Create your questions

Implement a class that implements the `IQuestionGenerator` interface for each question
type. For example:

    ```notranslate
    public class WhichActorAppearsInMovie : IQuestionGenerator
    {
        public QuizQuestion GetQuestion(IQuiz quiz)
        {
            var actor = quiz.OfType("Actor").RandomSingleSample();
            var companies = quiz.Relationships<ActorAppearsInMovie>(actor, "Movie");

            // Select a random movie
            var movie = quiz.OfType("Movie").RandomSingleSample();
            // Find all the actors that appears in the movie
            var actors = quiz.Relationships<MovieFeaturesActor>(movie, "Actor");

            // Select a random actor as the "correct answer";
            var oneActor = actors.RandomSingleSample();
            if (oneActor == null)
                return null;

            // Find 3 random actors that **DON'T** appar in the movie
            var wrongActors = quiz.OfType("Actor")
                .Where(p => !actors.Contains(p))
                .RandomSample(3).ToList();
            // If we don't have 3 wrong answers, we don't have a question
            if (wrongActors.Count < 3)
                return null;

            // Define the question
            var question = new QuizQuestion(
                string.Format("Which of the following appears in {0}?", movie.name),
                oneActor,
                oneActor.ToList());
            return question;
        }

        public int GetPossibleSubjects(IQuiz quiz)
        {
            return quiz.CountOfType("Movie");
        }

        public int QuestionTypeLevel { get { return 1; } }
    }
    ```

### Create your Quiz object

#### By loading from JSON (Recommended)

Quiz Generator comes with a `QuizBuilder` helper class, that can load the data from
a JSON stream:

    ```notranslate
    var qb = new QuizBuilder<MoviesQuizData>(new[] { new WhichActorAppearsInMovie() });
    using (var nodesStream = File.OpenRead("Assets\\Data\\Movies.json"))
    {
        qb.Load(nodesStream);
    }
    ```

The JSON stream should be the JSON representation of the provided `IQuizData` object–
in the above case – `MoviesQuizData`.

#### By Code

Creating the dataset by code consists of just creating a collection of `QuizObject`
and `QuizRelation` objects:

    ```notranslate
    var actors = new List<QuizObject>();
    actors.Add(new QuizObject { Id = "Actor1", Name = "Tom Hanks", Type = "Actor" });
    actors.Add(new QuizObject { Id = "Actor1", Name = "Angelina Jolie", Type = "Actor" });

    var movies = new List<QuizObject>();
    movies.Add(new QuizObject { Id = "Movie1", Name = "Forrest Gump", Type = "Movie" });

    var relations = new List<QuizRelation>();
    relations.Add(new ActorAppearsInMovie(actors[0], movies[0]));

    var quiz = new Quiz();
    foreach (var actor in actors)
        quiz.AddObject(actor);
    foreach (var movie in movies)
        quiz.AddObject(movie);

    quiz.AddRelations(relations);
    ```

**Note: It is your responsibility to add all relations, including their completing
relations to the quiz.**

### Generating questions

Create an instance of the `QuestionGenerator` class, and provide it with all the
questions you created in the previous step:

    ```notranslate
    var questionsGenerator = new QuestionsGenerator(quiz);
    questionsGenerator.Generators = new List<IQuestionGenerator>
    {
        new Questions.WhichActorAppearsInMovie(),
        new Questions.WhichMovieFeaturesActor(),
        new Questions.WhichMovieFeaturesCharacter(),
    };
    questionsGenerator.Init();
    ```

You can now use the questionGenerator to generate 10 questions:

    ```notranslate
    questionsGenerator.GetQuestions().Take(10);
    ```

## FAQ

### Where can I see samples of using the system?

Several Trivia games already use the system:

 * [The Beatles Trivia](https://saguiitay.com/games/the-beatles-trivia/)
 * [Geography Trivia](https://saguiitay.com/games/geography-trivia/)

### How can I use the Quiz Generator system?

You can create your own Trivia game from scratch, and make it look exactly the way
you want. You can also embed the Quiz Generator system as part of a bigger game 
and use the generated questions as a mini game.

You can also use any of the existing Trivia game templates in the Assets Store –
I strongly recommend [Trivia Quiz Game Template](https://assetstore.unity.com/packages/templates/systems/trivia-quiz-game-template-45521?aid=1101lS3S)
by [Puppeteer](https://assetstore.unity.com/publishers/4255?aid=1101lS3S). You can
read my [tutorial on how to use Quiz Generator with Puppeteer’s Trivia Quiz Game](https://saguiitay.com/quiz-generator-and-trivia-quiz-game/).

### I need more complex data in order to generate my questions…

You can implement your own classes of `QuizObject`, which add additional properties.
In the provided sample, there are examples of implementations – `SeaQuizObject` 
adds an “IsOcean” property, which can be used when generating questions.
