Editing unity scene files post-compile - c#

I'm working on a mod engine for a unity game. It is 2D based and stores all the level data in separate files ("level1", "level2") in the game data folder (I believe this is standard for unity). I am looking to be able to edit these in post to add/remove game objects in the scene. I wish to be able to do this programmatically in C#.
I've already had a look at the file in the hex editor, and it seems like this is possible (I can see basic game object data).
Currently im loading the scene then moving all objects around or instantiating new ones, but this is proving to be unstable because of the way the game handles objects.
If anyone could point me in the direction of how i would go about this, it would be greatly appreciated.
Update for those that are asking for additional info: Yes, by levels I mean scenes, unity saves them as “level0”, “level1”, etc
I am not the author of the game, the game was not designed with changing the scenes in mind, almost all of the interacatble objects have special riggers crafted to them, so in order to move them it requires me to be extremely careful or the game crashes.

From what you write it seems your separate files are actual Unity3D scenes. You should only have once scene and a system in place that reads and loads your data, for example from text files, then programmatically instantiate those object at run time.

Related

Prevent Changes on GameObject from being saved

I'm currently working on an Editor Script in Unity that disables specific GameObjects in a multi scene setup to make the Editor more responsive. I'm working with a setup that has up to 4 Scenes loaded at the same time. Since all these Scenes have many GameObjects in them (I'm talking thousands or even tens of thousands of Objects) the Unity Editor is getting pretty unresponsive and sloppy to use when these Scenes are loaded. Playing and Builds are fine btw, its only the Editor that is unusable.
So I'm working on a simple Editor Tool, that disables specific Parent Objects in all loaded Scenes so the Editor is usable again. This Tool works fine, but I want to prevent the changes on these GameObjects from being saved. I intentionally didn't mark the GameObjects as Dirty so Unity doesn't realize when I disable the Objects. Problem is, when I actually work on a Scene and save my work, the disabled GameObjects will be saved aswell, because at this point the Scene will be marked as Dirty. I've already looked into HideFlags but the problem there is, that I can't make Unity only ignore the changes that were made to an Object. HideFlags will only prevent the Object itself from being saved, which is not what I want.
If somebody knows a simple way to make Unity ignore changes on specific GameObjects that would be extremly helpful :)
Alternativly I would be interested in a way that enables me to run custom Code while Unity is saving the Scenes. That way I could make sure, that these GameObjects will be enabled, before Unity saves the Scene.
Thanks in advance
Don't know about preventing dirty objects from saving, doubt it is possible, but to run custom code before Unity saves a scene - easy.
Example editor script (remember it should be placed under Editor/ dir to work as any other editor script)
using UnityEditor;
[InitializeOnLoad]
static class EditorSceneManagerSceneSaved {
static EditorSceneManagerSceneSaved () {
UnityEditor.SceneManagement.EditorSceneManager.sceneSaving += OnSceneSaving;
}
static void OnSceneSaving (UnityEngine.SceneManagement.Scene scene, string path) {
UnityEngine.Debug.LogFormat ("Saving scene '{0}' to {1}", scene.name, path);
/* Do your magic here */
}
}
Credit for the example goes here, more info on the API here

Augmented Images in ARCore - How to use more prefabs and to update trackables, in an easier way?

I'm trying to make a simple AR Images app, for students to use but, since I don't have much programming experience, I'm trying to just use and change the example files as the base for the app.
Until now, after many hours of trying, and reading about it, I still couldn't make it work with my own images and prefabs... Using AR Images should be the easiest thing in ARCore, right?
Could someone please help, by uploading a working base project, that is independent of the ARCore example files? Or at least the main scripts with these improvements:
using several images and prefabs, like 15 or 20 max.
an easier way, like a drag and drop prefab list, of corresponding the images number or name with the correct prefab...
...maybe like shown in ARCore + Unity + Augmented Images - Load different prefabs for different Images
an easier way of updating trackables - their state, adding and removing prefabs when visible or they get out of sight
I have seen some answers to individual problems, but it's hard to put all that in the scripts, without messing something else... at least for me it is ;-)
I think this could help a lot more people besides me, by updating this problem and putting several answers in one place. Thank you!
Using: Unity 2019.2 beta - ARCore 1.10 for Unity

Unity is possible to add a Game Object in hierarchy but not instantiate it?

I'm not sure if it is possible to create a Game Object in the scene (so it appears in the hierarchy) but I would like Unity to not instantiate (or rather remove it) such Game Object when I press play.
The reason why I want to do that. I'm creating Game Objects as 'enemy spawners' in my game. But I don't want to have those transforms or Game Objects actually in memory while the game is running. So I've created an editor script that search for all the spawner entities, retrieve the information that I need and creates a binary file with it that later the enemy manager reads in run time during the initialization process.
If this is not possible, do you guys have any recommendations on how I could implement something similar?
Many thanks in advance.
You are looking for scriptable objects.
Here are some URL's
ScriptableObject Tutorial from Unity
ScriptableObject documentation
ScriptableObjects can be accesed at any time and don't have to be instantiated before you can use them. So they won't have a transform.

Unity 3D - forcing GI re-bake after runtime

I am working on a procedurally generated art project. The idea is that after runtime several clay pots/vases are procedurally generated from a lathe/line and rotation script. The pots are located on a circular platform and the camera rotates around this platform.
Pots are generated just in front of the cameras field of vision. Pots are deleted after the pass out of the cameras field of vision. At any given time, there are about 30 vessels.
The script is being changed so that the generation and deletion happens in a large batch while the camera movement is stopped. I would like to force Unity to re-bake GI at this time, but I can't figure out how.
Since none of the objects are moving and there will be a complete pause in all movement (the viewer will not know that the CPU is consumed) I'd like to avoid using realtime GI. Baked GI will allow me to use area lights and more expensive GI settings, but the objects generated after runtime need to be accounted for.
I hope this makes sense. I don't think I am using Unity in a typical fashion, so there is not a lot of documentation about this.
Also, if anyone knows a scripting API to access Lighting -> Object -> Important GI [x] that would be immensely helpful
I was able to use GameObjectUtility to make all of vessels Lightmap Static, but they need to be GI Important (they generate on top of each other, inside each other, etc.. and they have a slightly specular material)
The best solution seems to be a piece-mealed procedurally generated scene. This is briefly mentioned in the introduction to Global Illumination.
You might look into 5.3's scene manager, which allows for making multiple scenes and piecing them together. If you baked in such a scene, I imagine you could combine them together as you wished.

Creating Checkpoints in XNA

I'm making an XNA 4 game, and I want to create checkpoints for certain areas in my stage. I figured I could create a "ghost" object of the player object that would be created whenever the player reaches a checkpoint. And when you want to reload that checkpoint, you'll start to where that ghost player is.
As for now, I thought this would be an easy way to achieve this (although I think XML may be a better solution, but I've no idea on how to use that, yet). But the player has too many variables (health, stamina, ammo, bleeding timer, silver keys, golden keys, coins, infection, position, speed, angle, states [involve dying, infected, bleeding, burning, dodging, etc], I just thought that statemets like "continuep1 = p1;" (both are instances of the same object) would re-assign all the variables to the values that the other class contains, but when I tried to re-load "p1 = continuep1" it wouldn't work. It doesn't seem to do anything.
So I'm wondering, do I have to re-assign ALL the variables one by one? Should I start using XML? Or is there a way to assign all the variables without having to do it one by one? (I'm not asking for code, unless the last question is possible)
In my opinion I would say yes, start using xml. Don't worry about using xml, it's pretty simple once you get your head around it. A great way to learn would be to see your characters information displayed in xml format.
I think the most ideal option would be to serialize your game. So this involves storing the state of your character, the position it is at, the direction it is facing(which texture is currently loaded) and the stats(health, staming, bleeding etc).
With this you could reload your game from the last save when the player dies or when the game is next played. This would solve both cases.
This tutorial on making a top-down RPG game in Xna 4 is very good in my opinion and goes into good detail. Many of the techniques in this tutorial apply to more than just this genre and style of game.
http://xnagpa.net/xna4rpg.php
Part 11A(Game Editor) is the first part of a tutorial guiding the reader in making an editor for their game. With this Winforms project items, classes, quests etc can be added to the game and are stored as Xml files. At the bottom of page 6, and pages 7 and 8 outlines methods to serialize and deserialize your game.
In Part 11C(Game Editor), particularly at the bottom of page 9 and page 10. A save game method is introduced which serializes the game. You can also look at the new game and open game methods too.
These methods work with DataManager classes which store the items, classes etc in Dictionary's. This may be how you want to model your game information, if not the code in these guides may have to be altered to work with your solution.
I thought it best to provide my source for learning on this subject, rather than regurgitating it. I would recommend reading around this, not just the pages I've pointed to. Furthermore, in a later tutorial the serialization goes a step further and the files are stored in .xnb, which is a much more secure way to store information.
I hope this has helped and that you appreciate the guide in the right direction as opposed to just being given the answer. Besides, this isn't a task that would be solved in one method anyway.
Let me know how you get on and I'd be happy to help more.

Categories

Resources