Build Your First Game in Wolfram Language with Unity Game Engine: Live with the R&D Team

Alex Schellerbauer from Wolfram Research shows how to build games by connecting Wolfram Language with Unity. He demos a physics simulation, a playable piano with generated audio, and a dodge game, then answers questions about mesh conversion and integration limits.
Questions this video answers
March 29, 2023
Unity Editor 2019
82 keys total — 52 white keys and 36 black keys
PhysX (industry-standard physics engine)
Chapters
All right, let's get started. So, hi everyone, My name is Alex Schellerbauer. I'm a developer on the Discrete Computation team here at Wolfram Research. And today I'm going to be talking about building your first game with the Wolfram language and Unity game engine. In today's talk, we're going to start with a brief introduction of Unity. So don't worry if you've never used it before. Following that, we're going to dive into some hands on demonstrations. We'll have three of them here today. The first will be a wrecking ball simulation. We'll get to see how to use physics in the Uni engine. Then we'll have a playable piano where we'll touch on audio interaction. Lastly, we'll have a game where you try to dodge falling objects. At the very end we'll have some time for some questions, so if you have any questions during the stream, feel free to post them in the chat and I'll try to get to them at the end. All right, so what is Unity? Unity is a world leading game engine used by millions of developers. It's responsible for a lot of very popular games. Here are just a few listed here. It supports both 2D and 3D games and can build for pretty much any platform that can run games, whether it be Xbox, PlayStation, Switch, PC in your browser, whatever have you. If they can run a game, Unity can probably build to it. Yeah, so it's very popular. It's also particularly popular among kind of more mid tier or indie games and especially the mobile market. So yeah, with that in mind, what does Unity provide, especially as a Wolfram language user? Well, for one, it provides an industry standard physics engine called PhysX. PhysX is really nice because out of box you get a lot of colliders from both 2D and 3D. Things like sphere colliders, box colliders, capsules, even arbitrary mesh colliders. So you can just kind of automatically apply those to objects and then you can use a rigid body physics system to add forces and automatically handle collisions between those. Next up we also have lighting and rendering. Sunity has what's called a scriptable render pipeline, so you can target both low or high end platforms to get the sort of however high fidelity or low fidelity graphics you want. They also allow you to sort of bake lighting effects so you can have some high quality rendering techniques without having like poor performance. And then they also have a kind of newer way to author shaders using a graph UI so you can avoid any complicated shader code. So that can all be very handy. And lastly here, Unity has a lot of extensive built in utilities. These can be things like terrain creation or automatically navigating with agents in the scene. There's also a Unity asset store where any third party developer can just create their own asset and upload there. Whether it be models for, let's say like a village scene, or it could be tools that you could use yourself. These may be free or paid, but there's a kind of large library of those for you to choose from. So there's a kind of nice ecosystem to back you up whenever you use Unity engine. All right, so on the flip side, what does the Wolfram language provide to Unity? So one of the big ones is a scripting environment. While Unity does have their own scripts in C, it's a little difficult to build editor tools to actually do things while you're creating a game. Whereas with, well, for my own gym athletic, you have a nice notebook interface. You can just kind of quickly prototype things without having to worry about creating a proper GUI and their whole editor environment. And also you want to take a step further, you can start automating whole parts of your process, whether it be kind of creating projects, actually kind of putting it together, building your scenes, adding functionality, and the final building and deploying to different platforms. You kind of automate that whole POP pipeline just from the Wolfram language. And then the other nice advantage here is content creation. So Wolfram language provides a lot of ways to create content, whether it be audio, textures or geometry. So you can easily create those in something like Mathematica and then easily import them into Unity directly without having to worry about saving the right file format and putting in the right place. We kind of handle that for you. There's also the kind of extensive Wolfram knowledge base. So anything from like Geodata, Astro, data, elevations, all that can be easily kind of taken and pulled into Unity if you want to use that in your game. All right, so before we dive into Unity, I just want to cover the kind of, what I consider four key topics of Unity. So here they are. The first one is scenes. A scene is the 3D environment that contains everything you see from either UI or the actual 3D objects. This can be like terrain, buildings, players, what have you. So that scene contains game objects. GameObject exists within a specific scene and acts as a container for components. So on its own doesn't really do anything, but it kind of holds a lot of components and those components actually add the behavior to your Game. What's going on? We have our game object components. These control things like lighting, physics, audio rendering, even custom behaviors you can write yourself. Those are all done through components, and a component must be attached to a game object. All right, and lastly here we have assets. So an asset is any file in your game project. There's a specific folder called the Assets folder, where all these live and these assets can be referenced by components in your game. So this can be stuff like 3D models, audio files, images, all stuff like that. But here's kind of a visual representation of where this stuff lives. So here's screenshot of the Unity editor we have. At the bottom here we have the assets. This is the folders where all of our project files are. In the center here, we'll have the scene view. This is kind of a 3D view of our scene that's currently open. On the left here, we'll have the list of game objects in our currently open scene. And then on the right here in orange, we'll have the components on the currently selected game object. All right, with all that out of the way, we can kind of get started here. So if you haven't used Unity before, you'll first want to download it from the official Unity website. So you actually download this application called the Unity Hub. This is kind of a helper application that will handle all the different editor installations and if you have your own projects kind of managing those. So once you have that downloaded, you can choose a version of Unity that you want to install. Go ahead and just install it. As a side note, Unity link, that's kind of the packlet that supports this integration with Unity from the Wolfram language that currently only supports the 2019 version of unity Editor, but we are working to update it to the modern version. All right, so once you have Unity installed, you can just load the Unity link packlet using this need statement here. Now let's take a few seconds to load. Then once that loads, it'll add all the Unity symbols to our namespace. We can then, for example, here call Unity Open, which I can use to create a new project. So I just pass it the file path of where I want the project to be stored, automatically set up creating it and then actually make some room here on the screen. Show Unity when it opens. Now, the first time you create a project, it might take a minute or two for it to open. Has to load a lot of files. Then, while we wait and go ahead and describe our first demo that we're going to have today, we're going to do a wrecking ball simulation. The idea here is we'll create a wall of cubes that will build up and then we're going to have a wrecking ball come in and swing through them and kind of crash in and knock them everywhere. So those kind of require a lot of basic stuff like creating scenes. Game objects and assets also require us to add and modify components on those game objects. And lastly, we will be able to use UD's Physics Engine PhysX to kind of handle all those collisions and all the destruction there. All right, so should this be another 20 seconds or so for this to open? I'll just wait a moment. All right, almost open. And there we go. Select this. Only four. So here's the Unity editor. We have kind of these four different areas at the bottom here we have all of our assets. Right now it's pretty empty. We just have this Wolfram asset we which allows this kind of connection. So Mathematica and Unity can talk back and forth to each other. In the center here we have our scene view, which is also pretty empty at the moment. We just have these two game objects of the camera and light which we see over here on the left. And if I select one of these objects, we can see its components listed here on the right. Ok, so before we try to make the full wrecking ball example, let's start with a kind of basic test. So we're going to just want to make a cube and have it fall down and land on a platform. So to do that we can start by creating a cube which you see here. So let's call this Create Unity Cube. This will create Unity's default cube primitive game object and add it here. So here we now have the cube and it automatically added these components to allow us to render and have it collide with stuff. We can actually view those components on the welcome language side by just taking that game object reference and asking for its components. And we see those four here. So the transform is a default component added to all game objects. It determines the position and orientation of the object in space. Then we have the mesh filter which stores the mesh that we want to render or collide with. Here we have a box collider, so I'm just going to do box collisions. And last we have the mesh renderer which actually render the mesh in our mesh filter. All right, so we want to go ahead and actually make this be able to kind of move around and apply forces to this cube. So we do that by adding a rigidbody to our Game object here. Let's use Create a Rigid Body. And if we go back over here, we'll see it has now added this rigid body component. It has a lot of settings we can adjust, but for now the defaults are fine. Next, we'll want something for the cube to actually land on. I can go ahead and create this plane here. And we see the plane primitive was added to our scene. And then right now it's a little awkward because the cube is intersecting that plane. So we want to kind of lift it up and maybe just make it interesting, add a little bit of rotation to it. We can do this by updating the properties on our game object. The kind of syntax for this is shown here, where you kind of have your object reference, and you can use this part syntax with the name of the property you want and just assign its value here on the right. In this case, we're going to take our cube, move its position to be 8 units high, and give it a kind of unique rotation here. If I run those. Look back over here, we see the cube was lifted in the air and now it's rotated. And go ahead and enter play mode to see our cube fall. So right now we've been in edit mode. By doing this, we'll enter play mode, which will actually kind of run the simulation. So we'll see what happens. There we go. We have our cube, and we see it fall down and land on our platform and go ahead and use Unity. Stop here to stop that. And now we're back into edit mode. All right, so it all went pretty well. So before we kind of go on to the full Wrecking Ball example, let's get practice to try to keep your project organized. So we can do that here by first creating a lot of directories to hold stuff like our scenes or scripts. So if I go ahead and do that, you can see it added all these directories here to the Assets folder. We then want to create a new scene and save it. So so far we've been using an unsaved untitled scene, which basically means all our changes won't persist. So here I can go ahead and create a new scene called Wrecking Ball, which you see here actually stored in this scene directory. And now anytime we save the scene, it'll be stored here on the disk. And lastly, just do this ahead of time. I'm going to create some material assets. So materials are kind of the equivalent of graphics directives in Unity. They're how you actually style objects when they're rendered in 3D. So I can create three simple materials here just by passing a color and giving the name of the material. Obviously, it creates these three material assets in Unity. I have like blue, gray, and red. With that done, now we can actually start building the wall here. So to do that, we're going to want to do what's called creating a prefab. So it could just build that cube game object and copy a bunch of times to build our wall. However, it's good practice to, when you're creating identical game objects, to use a prefab, which just a way to store a game object as an asset. And then once you have that asset stored, you can just easily copy it as many times as you want. And if you ever want to change a property about it, to kind of change each individual property, you can just change the prefab and have it automatically propagate that change to all instances in the scene. All right, so to get started with that kind of like before, we'll create that cube here, and then we'll add that rigid body to it so that it will be affected by forces. We see our rigid body. Now we can add that blue material we created earlier. Now we have a nice blue cube here, and then that's actually all we need for our cube. So go ahead and create that prefab using this Create Unity prefab function. Give it the name, location to where to store it as an asset, and just pass it the game object, and I'll go ahead create that cube prefab, put it in our prefabs directory here. And now we have this cube. We can preview it here. And so that's all the components saved with it. With the prefab, we no longer need this original, so we just go ahead and delete it. And now our scene's back to the way it was. All right, so to actually kind of build the wall here, it'll be pretty simple. First we just specify all the positions that we want our cubes to be in, and then we'll iterate over those to actually create those cubes and place them in the correct spot. So to start, we can just have a table here where we just kind of iterate and create all these positions. We can kind of do a quick preview here in Mathematica using graphics, let's place a cube at each of those positions. One thing to note here is that in Unity, the axes are a little different. So instead of Z being vertical, it's the Y axis that's vertical. So if you ever pass any positions or directions manually you probably want to switch those, however, for stuff like importing meshes. We take care of that for you automatically switch the Y and Z axes. All right, so this is what we want to build with our wall. This is going to be over 100 cubes, which will kind of clutter up our hierarchy here in our scene. So it's good to just kind of package them all underneath a single game object. So we can do that here by just creating a plain old empty game object called wall. And then we can go and iterate over all of our positions. I'll start here. So for each position, we'll create a cube, move it to that position, and then make its parent be that wall. So it's going to kind of take a second. All right, there we go. So now we see it's kind of recreated our wall here in Unity. We can select all the cubes, and we see all those cubes over here, and they're all children of this wall game object. All right, and in order to prevent our cues from just kind of floating in the void here, we'll put a floor underneath them, make it a little bigger than default, and then set its material to be gray. All right, so that's looking pretty good. So next up, we just gotta make the wrecking ball. So to start, I want to make the wrecking ball use the spiky mesh. This will be a little more interesting. So I can just easily create a spiky mesh using our polyhedron data, create a mesh region from it here. Then I can create a mesh asset in unity by just passing that mesh to create a mesh here. Let me see. Now, if I go to the meshes directory, we have our spiky here in unity. And then here I can go ahead and create a wrecking ball object using that mesh automatically, add all the correct components to render it, and have collisions. If I go to here, we now see it and hide everything else. Yep, we have a spiky game object. And I go ahead and add that rigid body to give it a physics here unhide. All right, so I add a rigid body to it. I'm going to do something different here. I'm actually going to update the mass to be 100 instead of 1. This will make our wrecking ball a lot heavier than the default cubes. So really kind of plow through them when it swings through the wall. One side note is when you add a rigid body to a mesh collider like this, you kind of need to set it to convex in order to make sure collisions Work properly. All right, so now we have our wrecking ball here. It has mass. It can be affected by physics. However, it won't actually swing into the wall yet. So we're gonna get that nice kind of arcing swing. We'll use what's called a hinge joint. So to do that, we can kind of first move our wrecking ball up through this corner here. We can then add a hinge joint component, and then we can update the anchor of that joint to be right above our wall. If I go back here, it's a little hard to see, but there's a little kind of bar here now that represents where it will swing around. So it's going to kind of come up here and then swing down right into our wall. All right, to make it a little more obvious, I can add a cylinder to act as a kind of pole for a visual aid. And then I can go ahead and add materials to both our wrecking ball and that pole. All right, so now we're kind of ready to actually run the scene. However, you'll notice our camera in our scene is in the best spot to kind of view the destruction. So we want to kind of change that by first grabbing a reference to that camera using this Find Unity component to select the camera component on our main camera game object. From there, we can change its position and rotation here. So now we get a nice side view. And then to simplify things a bit, let's change that background from the default skybox to a solid gray color. All right, so now we're all ready. So we can go ahead and just save our scene and then hit play. Take a second to switch in the play mode. But once it does, we should see the wrecking ball coming from the right of the screen and smash through our wall. There we go. And nice. We get some good destruction. All the cubes fall down and tumble as expected. And we still have our swinging wrecking ball going back and forth. All right, let's go ahead and stop that simulation. And there we go. So that's the first demo for today. So next up, we're going to have a piano demo. So in this one, it'll have a lot of kind of similar things that we'll be doing as the wrecking ball demo. The two main things are going to be creating these audio assets. So, like when you have piano notes, we want to pass those to Unity, and we also want to be able to add a custom script in order to interact with the piano. In order to do that. First, we'll do some scene setup here. So this has been saved so we can create a new scene for our piano. And if we ever want to go back to the old scene, it's still in our assets here, so we can easily go to that. We can adjust our camera and the lighting. We can't always see anything now, but once you add the piano, it'll be more visible. And then we go ahead and generate these material assets ahead of time so we can use them later for our piano keys. It'll just be a plain white and black material. All right, like I said, we're going to be using scripts for this in order to have some interactivity. So in Unity, you can write custom components using scripts, where a script is a C class that can add unique behaviors to a game object. So I've already gone ahead and written the script in C. It's called pianokey. And what it's going to do is listen for the user to click. And when a user clicks, it'll check to see if they clicked on one of the piano keys. And if so, it will go ahead and play an audio clip and short animation to kind of press the key down and raise it back up. So we can sort of take that script text here and just manually import it into our object. So add it to our object under the scripts directory. You'll notice now. So whenever you update a script, either create a new one or just modify one in Unity, it will automatically detect that and try to recompile all your scripts. So it just did that there. So now we have our piano key script, and it's ready to be added as a component. However, in order to do that compiling, I had to kind of click back over to Unity. If you want to just stay in a notebook without clicking back over, you can use this kind of unitoken execute function to automatically refresh the assets and force that recompile to happen. All right, so before we kind of make the actual music notes, I'll just do some very quick discussion about what a piano is and how it works. So piano has 82 keys. 52 of them are white, and the other 36 are black. So you kind of see those laid out here in this diagram. You can kind of divide a piano into octaves, which are groups of 12 keys, kind of color coded. Here you see, kind of each octave has the same notes. It'll just be kind of shifted up or down. Yeah. For this demo today, we're just going to be focusing on what's called the C major scale, which is this Octave here with the extra C, you can see that isolated here. So we're just going to have these basically 12 notes that we want to create and be able to play in Unity. In Wolfram language, generating these notes are actually very easy. We can use this function called soundnote. Let's pass the name of the note we want it to generate the duration for how long it should play, and then the instrument we want it to play with. So if I generate a piano Note here for C4, go ahead and just play that. And that sounds good. Then we can do the same for all of the white keys we want here and play them all together and of course, do the same for all the black keys. Yep, that all sounds good. All right, so now that we can make those audio for the notes, we can go ahead and add those to Unity. So in order to do that, we need to create what's called an audio clip asset. So it needs to kind of live in this asset directory, and it can be referenced by game objects to be played in our scene. So we'll start by just defining all of our notes here from that scale. And we're just going to iterate over all those notes. For each note, we'll create that kind of Wolfram audio clip, and we'll just pass that to this Create Unity audio clip function, and it'll automatically create that note for us. I can do that here. If we look over in the audio directory, we see them kind of being imported in, and if I click on one of these, I'm going to go ahead and play it in Unity and we see it was imported correctly. And just you can find here. I'm also just kind of keeping track of which clip corresponds to which note as. I'll use that later. All right, so now let's create kind of the actual geometry for our keys. So we could do this with just kind of base primitive cubes and just kind of rescale them to kind of look like keys. But I figured it'd be nicer to actually make some proper meshes here. So kind of just gone and defined the dimensions of our keys and kind of just manually specified the coordinates for polygons, kind of writing out on a piece of paper. And that gives us these kind of five base key shapes. I can go ahead and take those and just extrude them upward pretty easily with Region Product to get our meshes for each key. And now that we have those meshes, we can do like what we did before with the spiky, where we create mesh assets from them, we're just iterating over that list. And now those will be in our mesh directory here. We'll have all five keys and kind of give a preview of them in this window here. All right, so we have made the audio, we have made the meshes and materials, and we've added the script. So now with all that, we can put it all together and actually make our piano. So we could do one key at a time and kind of do everything line by line. However, for stuff like this, I find it easier to kind of just define the settings for each key and just run through it in a loop and just create them all automatically. So here I define the settings. So for our keys, we'll define which of those meshes we want to use, which kind of music note we want to play. And then we also here have the option for the keyboard key to kind of link it to. So I can actually play it on my mechanical keyboard here. It'll be something similar. For the black keys, let's go ahead and define those. Like the wall, we want to create a parent object to hold all our keys. So I'll create this one called piano scale here. And I'll just iterate through first of all, the white keys. So for each of those keys, we'll create a game object using the proper mesh, set its position and parent, and then we'll add that piano key script and go ahead and set the key and then the audio clip to use on that script. So if I go here, we now see, we have all the white keys added. Here are those objects, and they each have this piano key script with the key and clip set properly. All right, so now go ahead and do the same thing for the black keys. Add those in. However, they still have the default material. So let's go ahead and kind of automatically select all the wire black keys and update their materials like so. So there we go. Now we have the keys being the proper color, and with that, we can actually start playing the piano. I'll just save our scene for good measure and then switch to play mode. All right, let's give it a second here. And there we go. So now we have this piano. If I click it should. I can play keys with my mouse here and do a little roll. I can also use my keyboard here to control it with the kind of QWERTY keys. All right. And I can also play the black keys here. There we go. So now we have a fully working, at least partial piano in unity. So that's kind of cool. Yeah, that Kind of concludes the second demo here. So now we have one more. So I figured now we actually make a somewhat proper game. So we had first simulation we had playing a piano. And I want to create a very simple game where you play as this little spiky that rolls around on the ground and tries to avoid these falling objects that will be kind of randomly spawning from the sky and falling down on you. So to start this here, we'll also do some basic scene setup. We'll create a new scene here. We'll create a four using a plane and make sure it's pretty big, just so it covers the entire play area here. We'll go ahead and move our camera and just slide again just to have kind of a better view. Have a top down viewing our floor above and then come over here. So in this game we're going to use two different scripts. The first one here is the player controller. This will do two main things. The first one is when I kind of use my WASD keys on my keyboard, it will apply physical force in that direction to the player rigid body. This will cause it to kind of roll around on the ground. The second thing it will do is it will check if the player has lost and if so, it will kind of pause the game and show a game over message. Here's a little snippet of that script here. And then the second script we'll be touching on here is the following object script. So this one will kind of take its object and place it randomly in the sky and have it drop down whenever it reaches a certain kind of height threshold. So it gets too low, it will respawn back on top in a random position. So then we just had this kind of infinite loop of them spawning on top, falling down and being reset up above in a new position. And then lastly here it'll check if it collides with the player, in which case it will tell the player that hey, you lost. And then the player will handle all the in game logic there. All right, so those two scripts, we can go ahead and add them like we did the piano key. Let's add them here. And then once again it'll take a second for uni to recompile all the scripts. You see it doing that now? Once it recompiles, we'll be able to add them as components like we did before. Yep, there we go. Go into the scripts folder. We now have the following object player controller scripts. Okay, so all that's left is kind of build up our scene here. So I'll start with the player. So I want the player to also be a spiky. So I'll just go ahead and use that spiky we made earlier. I can find it by using this find during the asset function. Just give it the name spiky. So there we go. It found it. We get a nice little preview of the mesh. I can then create a new game object of the player using that spiky mesh. We see it there right now. It's a little bigger than I want, so I can just change its position and scale so it rests on top of our platform and it's a little smaller. I can kind of enable physics on it by adding that rigid body component, setting it to be a convex collider like we did before as well. And then we'll go ahead and add that player controller script we made to our player object. So you see, it now has this player controller script on it that'll control the interactivity whenever I use the keyboard keys to move it around. And then go ahead, add that red material we already have to our spiky. And lastly, this is something a little new. We're going to change the tag on our player. So every game object can have a tag kind of shown up here. I don't know if you can see that there, this tag. So a tag determines mostly, or it's mostly used for physics collisions, where if you have two objects collide and you want to know like, what the object collided with, you can easily check the tag to see, like, oh, is this a player? Is this like ground? Is it water? Whatever you want. In this case, we just want to mark this as the player. So when our falling objects collide with it, they can know, hey, I hit the player. I should end the game. So I hit set that tag. And now we see it's marked as player and that covers the player. So next up, you want to create those falling objects just for fun. I'm going to make them also polyhedrons or polyhedra. So go ahead. And I just kind of selected some random polyhedra here. I'll use that polyhedron data to pull the meshes from them. So here we go. We have a nice selection here. I'll go ahead and create meshes from those polyhedra using the same create any mesh function. Now we see they're added under our meshes directory here. Then we can go ahead create the first falling object here, which is this kind of spiky as well. And then we can add a yellow material to it, like so and then we can create its rigid body. We're going to change the drag on this rigid body. So drag will just kind of make it slow down whenever it's moving. So since we're going to kind of spawn these up above and have them fall down, this drag will just make it fall a little slower. So we have a little more time to kind of avoid it as a player. And then like before, we'll set its collider to convex. And then something important here is we want to set its collider to be a trigger. Now, any collider can be made a trigger, and what that means is it won't actually kind of physically interact with other objects. However, it'll still trigger an event whenever it does collide with something. In this case, we don't want our falling objects to actually hit the play. We want to just kind of clip through it. However, we still want it to kind of trigger an event whenever the object touches the player. All right, now we have that. We can add that falling object script we made to our falling object here. So you got added as a component on the right. And then with that, our falling object is actually done and ready to be kind of put in the game. So in order to make all the other objects, we're just going to iterate over that list of polyhedron meshes. And for each one, let's create a new game object and add it to our scene here. You could potentially use a prefab in this case as well, but since each one will have a different mesh, it's just kind of easier in this case to make its own game object from them. All right, and with that, we're ready to play our game here. Go ahead and hit play switch over here. You notice they're all currently kind of clustered around the origin, which is fine, because when the game starts, it'll automatically spawn them in the sky, which we'll see in a second. All right, so here we go. I'm spiky. I can roll around with my keyboard controls. And as I roll around, we see the objects spawn randomly in the sky and slowly drop down, kind of see their shadow to get a kind of hint of where they're going to land. Yep. So I can just keep avoiding objects like this. However, if I'm not careful and I let object land on me like this, we see the game pauses and we get this nice game over text up above. All right, so that's kind of our simple game there. Right now, it's all been just in Unity editor or if you wanted to actually kind of build it to deploy as a standalone application like you would. If you're actually making a kind of real game, you can use this function called Unity Build. When you run this, it'll automatically kind of build a game for your current platform. In this case it would be Mac os. So we can do that. I've actually done that ahead of time because it does take a little while, especially on your first build, to compile everything and do all the setup. Let's go ahead. And I already made this example here. Go ahead and open this application. And now we have this standalone app that is a game we just made. I can still control it, roll around, and if I ever get hit by one of these falling objects like this, we see I get that game over. And for your. When building, you can choose whatever target you want. So another example here is that Piano demo I showed. I actually wrote a blog post on this a little while ago. And in that blog you can scroll to the very bottom and we actually have built it for WebGL, which allows us to embed it in web pages. So if I scroll down here, this is actually the game here, so I can actually play it live in the browse. Yeah, and that's one of the real advantages of Unity, is that it's very easy to kind of build it once and deploy it to multiple platforms. There might be a little bit of things you have to tweak, but overall it's a pretty easy process. Yeah. With that said, that's kind of all the content for my talk. If you want to learn more about UnityLink, there's documentation pages as well as more kind of samples and different features for stuff you can do with it. I've also had a couple of previous talks already that are on YouTube and yeah, with that I'll go ahead and switch over to Questions. So I'll go ahead and start reading through some of the ones I've been asked in the chat here. All right, so one here is. Hi, is there any particular reason for choosing integration with Unity over Unreal Engine here? We chose Unity because we already had an underlying framework to work with, the. NET framework. So integration was a little easier to do. It can already speak Unity's language in a sense. That's why we chose Unity to do the integration with first. In the future, we might consider adding it for Unreal. However, that would kind of take a little more effort there. All right, let's see. Does it always use physx or is there a way to use a different Engine like Havok. I don't have a lot of experience trying to switch out other physics engines as far as I know just uses PhysX. But there is probably a way to kind of get go under the hood and change which physics engine you're using. Yeah, I'm not sure on that one. Let's see. Someone asks is the panel on the left sending input to Unity on the left? Here I have just a normal Mathematica notebook. Whenever I evaluate different code here, I can basically reach out to Unity and execute code on the Unity side to do whatever we want. I can also read the data back in order to display it here in the notebook. All right, all right. Someone asked, I'm using the properties, why am I using the part notation instead of the just kind of regular association notation? So by that what do they mean? Okay, for example, can find my game object here, I'll just grab, let's say the floor. So I've been using the part specification here like this. You get a position. They're asking why you can't just use the single bracket version. So you actually can for reading data. However, if you wanted to assign data to it, you have to use this kind of part notation. So if I wanted to change this, I have to use the part notation. That's why we tend to prefer the part notation just because you can read and write to it. However, this one is just limited to reading. Okay, Is it possible to solve differential equation by ndsolve and the same time pass the solution to unity for some object's position will change according to the solution? I believe yes, that should be possible. So you can just run whatever calculation or evaluation you want on the Wolfram Engine side and then once you have the answer, you can send that to Unity and move whatever game objects around that you want to be in the proper positions. So yes, I believe that should be possible. Let's see, how is Wolfram language passing meshes into unity? Yeah, so there's a good question. So in orphan language we have a pretty flexible mesh framework where you can have kind of cells in any dimension. So you can have points, lines, polygons and polyhedra for 3D. However, in unity it only supports triangle meshes, so it's a little more restrictive. So whenever you pass an object over, we automatically kind of do some conversion behind the scenes, convert it to a triangle mesh. That's why over here you can see all these objects. You know they're like polyhedron which should be kind of volumes. They are just kind of Triangle meshes that you can look inside and they have no sort of interior. And also just look at the actual mesh themselves and you see it just lists the vertices and triangles. So, yeah, so we just convert to a triangle mesh. And also, like I mentioned before, there's that little step over. We need to flip the Y and Z axis because the axes are different. So, yeah, we can also do that automatically for you as well. All right, can you write a function in the Wolfram language as a script and then pass it to Unity to control positions and rotation of objects? Currently, we don't have a simple way to do that. We have considered working on a version where you can kind of almost compile the code and embed it into Unity. However, it's not a super simple way to do that at the moment, but it's something we'd like to be able to add more going forward. Something you can do is do it the opposite direction. So, so far I've been focusing on this talk where you're in a notebook and you're interacting with Unity from there. However, you can also write in a C Sharp script, make calls to the Wolfram engine itself, either like a local kernel or the cloud. So you could write a function in there and for example, have it called the cloud, do that calculation and then read the data back and then use it in the game. All right, let's see. People seem to like the Piano example. So happy you enjoyed that. Let's see. All right. Someone asks, is there feedback, as in using Wolfram to get analytics from play mode? Yes. So you can, for example, when you're in play mode, just read data back like you would in the scene. So go ahead. For example, right now, enter play mode, Which will take a second. I can go ahead once again, read the floor, get addition. We see it's position, 1, 2, 3. So I can read all the object data like you could normally in the edit mode. One caveat is that when you switch between modes, because the way Unity works, we actually lose the reference object. For example, if you made this game object and found a reference to it in the scene during edit mode and then switched to play mode, that reference no longer exists. So you have to refind it. This is a side effect of how Unity rebuilt itself when you switch to play mode. All right, All right, let's see. Someone asked how you pronounce my name. My last name is Shuttlebauer, so. Yep, that's how you say it. Let's see, is there anything else? Someone's asking if you could interface toll language with other APIs like WebGL or the military monogame. I'm not familiar with that. However, not currently aware of our other kind of integrations. We have other pack lists for integrating with different applications and Systems. You mentioned WebGL there. In which case UniCam build for WebGL. So this could kind of be a way to create like a WebGL game. Like we saw that piano embedded in the browser. I don't know if there's a kind of direct connection there. Yeah, that's all the questions here. So, yeah, with that, thank you for sticking around till the end here. And yeah, have a good day and goodbye.
More from Guy

Cheryl Strauss Einhorn | The Human Edge: Smarter Decisions in the Age of AI | Talks at Google

Rachel Glennerster | Innovation and the Center for Global Development | Talks at Google

Rebecca Winthrop | Rethinking the Purpose of Education in the Age of AI | Talks at Google

Dr. Robert Wachter | A Giant Leap: How AI Is Transforming Healthcare... | Talks at Google






