Haystack EU 2023: Learnings and reflections from our team
If you have built search experiences, you know creating a great search experience is a never ending process: the data ...
Senior ML Engineer
If you have built search experiences, you know creating a great search experience is a never ending process: the data ...
Senior ML Engineer
Just as with a school kid who’s left unsupervised when their teacher steps outside to deal with a distraction ...
Search and Discovery writer
Back in May 2014, we added support for synonyms inside Algolia. We took our time to really nail the details ...
Technical Writer
You’re running an ecommerce site for an electronics retailer, and you’re seeing in your analytics that users keep ...
Technical Writer
What do OpenAI and DeepMind have in common? Give up? These innovative organizations both utilize technology known as transformer models ...
Sr. SEO Web Digital Marketing Manager
As a successful in-store boutique manager in 1994, you might have had your merchandisers adorn your street-facing storefront ...
Search and Discovery writer
At Algolia, our business is more than search and discovery, it’s the continuous improvement of site search. If you ...
JavaScript Library Developer
Analytics brings math and data into the otherwise very subjective world of ecommerce. It helps companies quantify how well their ...
Technical Writer
Amid all the momentous developments in the generative AI data space, are you a data scientist struggling to make sense ...
Sr. SEO Web Digital Marketing Manager
Fashion ideas for guest aunt informal summer wedding Funny movie to get my bored high-schoolers off their addictive gaming ...
Sr. SEO Web Digital Marketing Manager
Imagine you're visiting an online art gallery and a specific painting catches your eye. You'd like to find ...
Senior Software Engineer
At Algolia, our commitment to making a positive impact extends far beyond the digital landscape. We believe in the power ...
Senior Manager, People Success
In today’s post-pandemic-yet-still-super-competitive retail landscape, gaining, keeping, and converting ecommerce customers is no easy ...
Sr. SEO Web Digital Marketing Manager
There are few atmospheres as unique as that of a conference exhibit hall: the air always filled with an indescribable ...
Marketing Content Manager
To consider the question of what vectors are, it helps to be a mathematician, or at least someone who’s ...
Search and Discovery writer
My first foray into programming was writing Python on a Raspberry Pi to flicker some LED lights — it wasn’t ...
Technical Writer
How well do you know the world of modern ecommerce? With retail ecommerce sales having exceeded $5.7 trillion worldwide ...
Sr. SEO Web Digital Marketing Manager
In a world of artificial intelligence (AI), data serves as the foundation for machine learning (ML) models to identify trends ...
Director of AI Engineering
A search engine inside a game engine? It’s an idea that took shape just before one of Algolia’s off-sprint days, an event where Algolians experiment with new concepts.
During the previous off-sprint, my colleague Samuel Bodin and I were thinking: Algolia has a .NET API Client, and Unity supports C# as a scripting language–Why not try to bring them together?
Our idea was to implement a search-as-you-type experience inside a Unity game scene. We decided to create a marketplace, a common use case for searching within a game.
The result? Success. Except for a small challenge with performance, which was solved by switching into asynchronous mode, at the end of the day we had a fully functioning Algolia search within Unity, complete with an index of game assets and images, and a full search and results UI. Let’s see how this was done!
Unity does not support the .NET package manager NuGet. To address this, you have to download the zip package of the Algolia library directly on nuget.org, and then unzip the contents in the Assets/Plugins folder of your Unity Project.
After that step, you have to add the using Algolia.Search.Clients;
statement in a C# script to start using Algolia with Unity.
Now that we have Algolia plugged into Unity, the next step is to create the game scene.
Our first task was to find game assets and a dataset to craft our game scene.
We found this excellent starter kit from Unity and used it as a foundation for our experiment. The good thing is, it was packed with a lot of UI elements–we just had to reuse them to create the marketplace scene and add a menu to access it!
Before digging into the game design, we had to create a dataset. Since science-fiction was the theme of the assets, we decided to create a dataset composed of planets. We created a thousand fake planets with a powerful dataset generator, Mockaroo. We then used CC images to illustrate the dataset. Once that was done, we were ready to go! If you want to reuse the data you can find it on GitHub.
The final and most challenging step was to create the game scene for the marketplace. We wanted it to be simple: it would be composed of a search bar and a panel displaying the results. To achieve this, we took the controls scene from the starter kit and customized it. The result—a panel for the planets—looked like this:
After scaffolding the scene, a little bit of work was required to bring it to life. We needed to script some C# to turn the empty box into planets.
Before sending requests to Algolia, we needed to catch every keystroke the user was typing. To achieve this, we attached a C# script to the InputField to catch the ValueChangeCheck() event. All good, we were able to retrieve the input keystrokes! Making a request to Algolia with the retrieved string was as simple as:
var results = await _searchIndex.SearchAsync(new Query(SearchInput.text) { HitsPerPage = 8, });
Note: we had already instantiated the AlgoliaClient and the Index object in the Start() method to avoid creating a client for each request.
We got the search working, and Algolia’s C# API returned a list of planets (List<Planet>). Now we had to display them. We had to create GameObjects.
We wrote the void LoadResults(List<planets>)
method, which creates game objects from List<Planet>. You can find the source code on GitHub. Here we will point out only a few parts. In short, the method loops on the planets to create game objects, one for each planet.
Perfect.
No, not perfect. Not yet: the display of planets was too slow, and the image refreshes were choppy.
Our first idea was to create two game objects per planet: a RawImage and a Text, and then to do some computations to position them in the scene. The search experience was OK, but it was a bit laggy.
So we looked into this and discovered a flaw in how we displayed the planets. We were loading the planets’ textures synchronously, and that’s what made it seem slow. The solution to make the search experience more fluid was to load the planets’ textures asynchronously. To achieve this, we used the Resources.LoadAsync<Texture2D>
method from Unity in our LoadTexture
IEnumerator LoadTexture(RawImage image, Planet planet) { var resourceRequest = Resources.LoadAsync<Texture2D>($"{planet.Path}"); ; while (!resourceRequest.isDone) { yield return 0; } image.texture = resourceRequest.asset as Texture2D; image.color = Color.white; }
You can see the difference in the video below:
Perfect! But not done. While the search experience was fast enough for our POC, we had not yet loaded the scene from the main menu.
To close the loop, we created a button in the main menu to access the marketplace. We attached a LoadSceneOnClick script onto the OnClick()
event of this button. This script, as its name implies, loads a scene with its index after the button is clicked:
public class LoadSceneOnClick : MonoBehaviour { public void LoadByIndex(int sceneIndex) { SceneManager.LoadScene(sceneIndex); } }
We now have all the pieces in place, so let’s see the result!
In one day, we created a simple marketplace in Unity with a search powered by Algolia’s C# API client. And we are not finished! One interesting direction is to try the same with other game engines; for example, another project would be to reproduce this integration with CryEngine, which also supports C# as a scripting language!
But the real next step is to add all of Algolia’s features into the solution. There are still a lot of possibilities for us to explore: creating a search in an inventory in MMO games, adding a UI to search servers, adding facets and filtering, deepening the dataset, adding analytics and personalization, etc. Essentially, as your game increases in complexity, your marketplace, and any search-based feature in Unity, will need to follow suit. Happy gaming!
Written in collaboration with Samuel Bodin.
It's extensive, clear, and, of course, searchable.
Powered by Algolia Recommend