- Home /
Modify a mesh- or just a txt file- in real time?
I am trying to make a game with deformable terrain, but I do not want to use Unity's terrain engine but rather a mesh. Is there a way to access and modify a file in the game files while running the game at a text based level?
A simple way to put this is if I were to have a .txt file read out its text and display it, could I have the text in the .txt file edited and then changed in real time?
.Obj files are read in plain text as display the coordinates of the points followed by the connection of the points as faces or lines. That's easy enough to code something to modify the mesh in-game. A simple square plane looks like this in an obj file:
v 1.000000 0.000000 -1.000000
v 1.000000 0.000000 1.000000
v -1.000000 0.000000 1.000000
v -1.000000 0.000000 -1.000000
usemtl (null)
s off
f 1 4 3 2
My second question, if the first can be answered, is if it is possible to also modify the texture in real time.
Please let me know anything you can, even if it's about modifying a text file in real-time directly from the game! Thanks!
By the way, I would prefer Javascript if at all possible, but if all you know the answer for is in C# then please share that too. :P
Answer by TaintedLemon · Jul 06, 2011 at 01:26 AM
Modifying text can be done by using a TextAsset
Modifying the mesh can be done by editing the vertices as shown in the example on the Mesh reference page.
Modifying a texture can be done by changing the pixel values on the existing Texture2D by using SetPixels().
Thanks for your quick reply! I'll check into those. I gave you a thumbs up, but I'll wait a little while longer before giving you a best answer to encourage other answers.
About editing the texture by setting the pixel values, that might not work... I am making a 3D $$anonymous$$ing game, somewhat like $$anonymous$$inecraft, but that uses true terrain and not a ton of blocks. I will have the world generated in a text file, kinda block based, but when you dig, the world will change, but if you dig into a different thing, ie. rock from dirt, then the texture will need to change around your tunnel. If you understand what I mean, do you think "changing the pixel values of an existing Texture2D" is the best approach? Or is there anything more appropriate available? Because, lets say you have a flat piece of ground with a texture. Now you try digging a tunnel into it. You will need a new texture file to add to that surface because it wasn't originally there but was added in a separate spot. Thanks again!
You should definitely have a look at Voxelform then: http://cyclopsframework.org/voxelform/. Costs $100 but might be exactly what you want.
Wow, Voxelform looks like a pretty fun piece of software for Unity. I've been looking for something that can make non-hightmap terrains. But unfortunately that's not 100% what I want, because I also need to have a texture applied somehow, and I'll want to know my own code for that. Oh, and I don't want to have to pay $100 for it. But again, thank you for the link. It was fun playing with the demo!
@$$anonymous$$eavon, even if you don't want to pay $100, perhaps you could tick this excellent answer and it remains the best answer after some 5 years :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612133613im_/https://answers.unity.com/themes/thub/images/avi.jpg)