 Hey guys, it's crypto grounds here. Welcome back to another unity auto game tutorial video this episode 4.2 And today we're gonna be working on the import and export system for our game And if you end up enjoying this video, make sure you please leave a like button as it helps out the videos Subscribe my channel if you're new and turn on the bell for future notifications for videos and live streams Let's hop right into it. So in the previous episode, we worked on Preparing a setting screen so we can throw everything in here. So I'm going to actually prepare the UI first and Then we can get on with the code. So let me finish that and I'll get back to you guys All right, so I have made the little interface for clicks. So let me explain what I've done So I have created two empty game objects that Surrounds everything for the import and export. So for the import will actually for both We have a text much pro input field So I prefer using these because they can hold way more characters than a normal one does and sometimes your saves Make it really long. So yes, use the text much pro input field. So that's this big box here We then have an import button There's this will be an export button. Let me fix that real quick Export and then we're gonna have a pace So it's gonna paste whatever you have copied and then you have a clear text just in case you want to clear this box So for the export we have the export button So it's gonna export your save into this input field You can copy the clipboard or you can clear the text. So we have six things will technically five things We need to get working. So we have the import export Paste the clipboard copied clip for it and we also have the clear text. All right, so let's get start with the coding So first we're gonna open up our safe system script. All right, so let's start with our user interface So we're gonna do an all of our objects into our system class So first we're going to have so these are actually not gonna be static because we're gonna be referencing them inside of Inspector, so we can't do that. They're static. So just be aware of that. So first we're gonna add our Input field. So it's gonna be a TMP underscore input field and Make sure you have the using TM pro at the top. Okay, so I have that for my import and my export I have also added a copy and paste button as an image So when we press copy or paste it will turn green temporarily and we'll also have the text because it's gonna show that it's Pick a copy or paste the text just so the user knows so I'm gonna show you how to do that as well All right, so for the first part of this we're gonna be making our import system. So this starts with one method import This will be a void method and this will be public because we're gonna be accessing it With a button in unity. So make sure it's non-static and it's a public void So in this import method, we're pretty much gonna be writing to our original file and we're gonna be overriding it So we're gonna be doing that and then we're gonna actually be Reloading that file like we do in the load data method. So let's start with the writing process So we're pretty much gonna be doing the same thing as up here in the save data method So let's create this using statement real quick. Okay, so I'm at a halt right now We don't have a file name. So let me show you guys something real quick. So let me get it This is idle researches. So basically I have Split my import system into 10 different files. So what I do is that I have a save system string array I access it and then I Basically loop through all of these all the safe files I save it into all and then I load them all so if that makes sense So basically I am rewriting all the files and then I'm reloading all of them individually So I'm gonna be doing this very basic because we only have one safe file So let's just keep it that way So what we're gonna do is add our original File name which is in controller. It is player data underscore tutorial We're going to be pasting that in here and then adding our file type Now what we can do in here is to create a constant string. So I'm gonna do that So we're gonna have a private constant string And this will be our file path And we're gonna set it to our player data underscore tutorial So then we can replace this with our new constant Okay, and we can also convert this into string interpolation Which is basically just doing dollar sign Parentheses really braces and then put our variable in for each of them. So it looks just like that So now we're basically writing straight to the file. So how do we do that? Well, it's pretty easy We do the same thing up here writer dot right line So we're gonna do writer dot right line and then put in our import field dot text And just to be safe, we're gonna close our writer and We should probably do the same thing up here as well. Just just to be safe sweet So our import is now done now once we import we need to kind of reload everything So once we load our data, we're gonna have to load So we're gonna load our data and then we're gonna have to run our start upgrade managers again just in case So what we can do is access our controller. So we're gonna do controller dot instance That starts. Okay. So the problem here is that our start is not public. It is private So I'm gonna make this method public and then I'm gonna go back to our save system class and Then now you're able to call the start method So it's basically just gonna reload everything from scratch and one last thing we need to do is to create our directories So every time we access a directory, well, I want to make sure it actually exists So we want to do the same thing directory dot create directory save path and backup path just to be safe And honestly, we really only need to do our normal save path because we're not doing anything with our backup path So again, make sure you have directory dot create directory and then put your save path in there All right on to part two, which is the export system. So for export system, it's gonna be a public avoid method It is not static. We're gonna be reading directly from the file and then throwing it into our export field So we're gonna be creating a directory again as I forgot earlier and make sure you pass in your safe path as an argument Okay, so next we're gonna be using We're gonna be creating another using statement. So We're gonna be doing the same thing as our load, which is going to be our Stream reader. So let's do that. Okay. So now what we're gonna do is We're gonna access our export field and we're gonna change the text We can do dot set text and now what we're gonna do is we're going to set our export field We're gonna set it to reader dot read file to end So this is gonna read the entire file and it's going to copy as a string and then set it to our export field And we're gonna do reader dot close just to be safe And one last thing is just in case we want to save our game right before we export it because we want the most recent version of our save so let's head to our controller class and We don't have a save method So I'm gonna create one or click and I'm going to move this save system save data into our save method So I'm gonna do that and then I'm gonna call our save method inside of this this This timer right here and then what we can do is go back to our save system class and call it inside of our export There we go So now it's gonna save and then we can export it and that's it on the part three which is the copy button so this is going to require two things the first one is a Public void method which we're gonna call copy and then we're also gonna have a public I enumerator and this we can basically We're gonna put a timer in here so we can Determine when we want to put our our buttons back to normal So this will be for our copy and our paste button So we will have a total of three methods after we do the copy and paste system And what we can do with I enumerators is that we can create a co-routine inside of our copy method So then we can create delays and stuff in here now I am not a professional I enumerator So if you want to do some more additional research you can check out the unity forms and the Microsoft Docs So for our copy, we're gonna access an object called the GUI Utility and then there's a variable in here called system copy buffer So this is basically your clipboard. So we're going to set that to our Export field because when we export we want to copy that so we set it to that and there we go We have copied our text. It's the same thing as doing control or command C So after that we want to change our copy button. We want to set that color to Green and eventually we're going to want to make that button back to normal So we're gonna grab our copy button and we're gonna change the color to a new color Whatever color you want it to be so in my case, it's this number It's what makes this dark gray that I have in unity, but it's this dark gray So this is gonna turn green and then it's gonna go back to dark gray. So in here. I'm just going to Add this color. So we know it's gonna change it back to normal And then we want to do the same thing for our copy text So our copy text is going to say copied and then we're gonna return it back to copy to clipboard Okay, so just like that now when we copy this We are gonna change this stuff and then return it back to normal So in order to call this we need to do something differently. We can't just do copy paste buttons normal We have to do something called a start co-routine. We're creating a co-routine to call this So this is our routine here and it's basically gonna start doing this stuff until it's finished So it's finished after we do all this. However, we're not done yet So we need to do a yield return new wait for seconds And then we're gonna put in a timer So let's say I want to go back after two seconds for example, so you put in a float here So it's basically gonna stop for two seconds and then it's going to come back to this So there's a lot of new terms here yield. I enumerator. I get it could be confusing It's definitely a really weird concept to get around So another thing I want you guys to know is that this is specifically a unity thing You can't do this and just plain C sharp. So this is a Unity built-in thing if you want to do more research Google is your friend But yeah, so basically we're going to be waiting for two seconds And then we're gonna be doing this stuff and for our copy. We want to make sure we actually have stuff to copy So if this text is empty We're just going to ignore it because we're not really copying anything and I don't want to copy something empty And then my clipboard is just gone. So we're just going to return it if it's empty So for our pace, this is part four. It's going to be almost the same thing Except we're doing it in reverse. So we pretty much just copy all this and just change some of the stuff around so our copy button and our text will become our paste and This copy will be pasted instead and then we're still going to call this coroutine and Then instead of setting our system copy buffer We're actually going to be setting our import field as our system copy buffer. So we can get rid of this and It should be good and then for our copy paste buttons normal We're gonna be doing the same thing for our paste button and our text. Alright, so we're all good to go Let's put it all together. Alright, so inside of our game. We need to add our save system to the script So let's create an empty object save system and then drag our save system script into here So now we have all this to assign. Let's start with our import field So we have import and we have export and then our copy button Copy oh So for me, it's actually selected. So this is actually my image here. So I am just going to drag those instead So we have our copy button, which is to selected paste and then my copy button text so we have our copy button text and Then our paste button text. Oops. I accidentally dragged the wrong one So now let's assign the method. So I'm going to be selecting all of these the export copy paste clear all that stuff and I'm gonna be dragging the save system into the on click Component right here, and then I'm gonna be assigning each individual method. So you have our clear which will be oh We actually haven't made that yet. So let's do that real quick. So our clear is very simple It's just a public void clear. I'm gonna put in a string Parameter in here just to determine what type it is. So if the type is equal to export It's going to clear the export and then just return it So we're gonna have our export field and we're just going to leave it blank And then return otherwise, we're just going to clear the import field just like that very easy So for the clear button I'm going to assign the save system clear and then we have Export import. So our export will just say export and then our import version will just say import So for a copy button, we're going to be assigning the copy method paste now have the paste method our export will have the export method and Import last but not least will be the import and that is all we have to do here guys So let's give this a shot. All right. So we have some progress already. Let's head to our settings and Here we go. So let's do export All right, so you can either click this and it'll highlight all of it you can copy it and if you go to notepad My computer's being very slow. I'm sorry. And if you paste it, there you go There it all is so we can also do the same thing copy to clickboard and it should go away Cool. Oh, that's this paste clipboard accidentally mix up the wrong text there that you can see it's the exact same thing except There's a little bit change because I exported later, but to fix that we have our paste button text I see. All right. So we know the mistake there Now let's do import. So if we get some progress here after it's done importing my scripts Let's see. We have 54 flash. So this one should have roughly around 40 ish so let's copy this and We can paste it in here Or we can click this paste clipboard button and you can see it's pasted and then it's good Oh, the text is broken as well. I guess But you see we have 68 if we import We have an index out of range But it looks like it imported for the most part. Let's see what the issue is All right So what I'm gonna do instead instead of calling the start method because that's where the errors are coming from I'm just going to go to controller.instance and well, we're pretty much just going to load this ourselves So we can just copy this from our start method inside controller and paste it in here So our it'll be our file path. Also, I just realized that I call this file path instead of file name my mistake. It's all right We're pretty much just going to load the data inside here and just go with it So i'm not really sure what's causing the errors. That's probably something else I'm gonna have to check in the future, but this should work just fine All right, let's give it another shot. So we have our save If we import. Oh, it doesn't do anything Oh, I have the wrong path. So let's copy this Copy clear it and then so you see we have 77 Paste imports There you go It goes back to what it was before this is the export import system I hope you guys all enjoy it and learn something new today And if you did make sure you leave a like as it supports the video and subscribe to my channel If you're new around here turn on the bell for future notifications of videos and live streams Check out the join button below if you want to become a member of the channel or check out the patreon Which is in the description below now side note. I've been working really hard on idle research lately So if you guys haven't seen any of the previews yet, make sure you go check them out They're on my channel and you can wishlist the game on steam as well Thank you guys for watching and i'll see you guys in the next one, which will be episode 4.3 And that will be web gl support. So just a heads up. We're gonna be doing something very different there Well, not really very different, but web gl and no normal standalone and mobile They're they're a little different when it comes to the important export system So I'll be sure to show you guys in the next video. So be sure to stay tuned anyways. Thanks again. Peace