Call a function in Unity Editor - c#

I need a script, which takes a big amount of monobehaviors in children and arranges them to lists in parent script in a specific way to save parent as prefab. It would take a eternity doing in by hand. And i dont wanna do this in Start() on runtime, because these prefabs could be instanciated a bunch times per frame and causing mini lag spikes when searching for scripts in children. So how do i do this once in a editor to save all references to prefab? Never done something like that, but seen buttons in custom inspector that call functions for plugins. I tried [ExecuteInEditMode] which gives good result but it also continue to execute in runtime So asking what way would be simplest to just call a function in editor and make it not work in runtime?

Try something like this:
[ExecuteInEditMode]
public class YourClass : MonoBehaviour
{
void Update()
{
if (!Application.isPlaying)
{
Debug.Log("This should only run in edit mode.");
// More code
}
}
}
Depending on what you're trying to do exactly, there might be a better way to trigger this than ExecuteInEditMode, but this is the simplest way of getting the effect you requested. This won't run as long as Application.isPlaying returns true. So it will never run in playmode and never run in any builds.

Related

Use script for GameObject creation / changes outside of runtime

I have a prefab, which is used multiple times in my game (it will not be instatiated during runtime, I only use the prefab by dragging it into scene view to build my level).
Inside this prefab are many locations, where a specific text is written. This text is unique for every prefab.
So, it is like:
InstanceA
TextA (multiple times inside the instance)
InstanceB
TextB (multiple times inside the instance)
and so on ...
At the moment, I see two options to do it:
I change every text manually in every instance. This would work, but is a little annoying, also I can see me making mistakes (like a misspelling or forget to change one of the texts).
I solve it by a script and change the texts in the Awake or Start method. This would work, but it must be done every time, I start the game.
What I would hope for would be a solution like 2) but not every time (because nothing changes, it es every time the same) but only once - so it is generated by a script but after that, it will be always there. Even when I'm not in runtime and just work in the scene view.
Do you know any approach to get something done like this?
Thank you!
If you wish to run a piece of code outside the run-time, you can use one of the following two options.
OnValidate method
OnValidate is triggered when your MonoBehaviour script is loaded or an inspector value has changed.
using UnityEngine;
public class TestScript : MonoBehaviour
{
private void OnValidate()
{
Debug.Log("test");
}
}
ExecuteInEditMode attribute
Adding ExecuteInEditMode to a MonoBehaviour class will make it function both during and outside the runtime. If you do not wish the script to function during the runtime, feel free to also use Application.isPlaying.
using UnityEngine;
[ExecuteInEditMode]
public class TestScript : MonoBehaviour
{
private void Update()
{
if (!Application.isPlaying)
Debug.Log("test");
}
}

Why not finish all the functions within Update() function in unity

void Update() can perform the functions per 1 frame . So in script , e.x like
void OnTriggerEnter(){} . why don't put it in update() function .I know there's some misunderstanding here but I just can't explain it to myself .Besides , is that only void Update() , void Start(){} that functio in unity .Including some functions like void OnTriggerEnter can function as well in unity since it is built-in functions .How about those functions that written by us like public void SwitchAvatar() .Can it function if it is not referred inside void Update(){} .I know the questions above may sound stupid , but dunno why I can't tell the answers .All of your help is greatly appreciated . Thanks !
Alright, let's open pandoras box about magic methods in Unity.
First, there are two types of classes that you can inherit from, to access magic methods: MonoBehaviour and ScriptableObject. Both of them offer different things and the latter is mainly used to serialize data outside of scenes. Also, ScriptableObject has way less magic methods, compared to MonoBehaviour.
Second: A MonoBehaviour has a lifecycle. Where you are in this lifecycle determines what methods are called by the engine.
The following graphic shows you the whole lifecycle of a MonoBehaviour:
(Source: Unity - Manual: Order of Executions for Event Functions)
As you can see, the object gets instantiated and Awake is called. Before the first time Update is called, the engine calls the Start method. There is a difference between Awake and Start: Awake is comparable to a constructor. It is called without anything external being guaranteed to exist (like other components on your GameObject). When Start is called, all the other components on the object are initialized and can be accessed by a GetComponent call.
Now to Update, FixedUpdate and all the other events:
Unity has two separate cycles it iterates over. One for physics and one for everything else. Because calculating physics is expensive and needs precision, it is called in fixed, distinct time steps. You can actually set them in the project settings in the "Time" tab.
Now if you want to modify anything related to physics (like the velocity of a rigidbody), you should do that in FixedUpdate, because it runs in the same time step as the physics engine (PhysX or Box2D, depending on your usage of colliders).
Update on the other hand runs as often as possible. The current time distance between two Update calls can be observed by calling Time.deltaTime, which is the time that is passed between two Update calls. Note that Time.fixedDeltaTime is always the same, as it is the time between two physics calls.
The other event methods are called as responses to either the editor internal update loop, the rendering loop or the physics loop. Whenever an object collides in the physics calculation, OnCollisionEnter is called. Note that this can't happen out of Update, because, as we know, Update is not used to calculate physics.
Okay, a tl;dr:
Start and Update are not the only methods that exist in the MonoBehaviour lifecycle, there are plenty and each has its purpose. Physics are calculated in a different timescale as the Update method is called and thus cannot be part of Update.
And one thing to take away: You should really read the manual of the game engine you are using. There is plenty of stuff that you should know when you are writing code for a real time application and if you build onto an existing game engine, you should check the docs of that regularly. Especially the documentation of Unity is a very good read for both code and editor usage.

Unity StateMachineBehaviour : Start function called once?

i'm making my first enemy AI in unity. I'm trying to make a finished state machine with the animator controller to do it.
I just discovered the StateMachineBehaviour script which is called when the AI is in a state. It has multiple methods, including the OnStateEnter. It is called everytime the AI enter the state.
My problem is only about optimization, my AI need to get the GameObject "Player" in order to attack it. So i'm getting it in my OnStateEnter method for the moment, which i feel is bad, because i'm getting it every time the animation is called, i would like to get it only once, at the start.
I basicly need a start function but it's not working, i have made research and found nothing. I tried to watch video about people making a finished state machine but they are just getting the same GameObject multiple time ( example here : https://youtu.be/dYi-i83sq5g?t=409 ).
So, is there a way to have a start function or to get an element only once ?
I could make a bool that is called only the first time and that get the GameObject, but again that would be an "useless" if running in my function.
Any suggestions ? Thanks
No, unlike a MonoBehaviour a StateMachineBehaviour has no Start message only OnStateEnter, OnStateExit, OnStateIK, OnStateMove and OnStateUpdate.
There are also Awake and OnEnable but I'm pretty sure they are not used in the StateMachine and might not behave as expected.
You can however either use
OnStateMachineEnter
Called on the first Update frame when making a transition to a StateMachine. This is not called when making a transition into a StateMachine sub-state.
Or use a simple bool flag like
bool alreadyExecuted = false;
OnStateEnter()
{
if(alreadyExecuted) return;
// Do your stuff
alreadyExecuted = true;
}
(Just a guess)
In the Inspector you actually can enable and disable StateMachineBehaviours like components. So it might be able to do this also in script maybe the same way using
enabled = false;
but I didn't find anything about it in the API and since I'm currently on my Smartphone I can't test it.

Unity3D - GameObject.Find() vs Inspector Assignment performance

I'm making a mobile clone of pacman for android. Everything is working fine, but I am trying to optimize my game as much as possible.
Currently in some scripts I am finding GameObject's by doing GameObject.Find() in the Start() function of my scripts, like this:
void Start()
{
ghosts = GameObject.FindGameObjectsWithTag("Ghost");
pacman = GameObject.Find("Pacman");
gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
}
My question is, would performance increase if I made some of these GameObject's inspector assigned variables instead of doing .Find()?
Would that increase the performance speed or would it make no difference seen as though I do a .Find() in the Start() function?
Obviously performance would decrease if it was called in the Update() as the script would try and find a GameObject every frame, but a Start() function would only search for it once?
Performance at startup for this kind of things is totally negligible, Inspector assignment requires deserialization, while Find requires message dispatch, both are slow operations, if you think that matters, profile that.
I would anyway use
FindObjectOfType<GameManager>() as GameManager
wich is at least more typesafe, I would not use editor inspection if possible, because manual wiring is source of many little errors. So I give a human-time performance reason:
Do not use editor inspection because if you miss to wire something you will have to debug a nullpointer wich on the long run will eat hours of your time.
You have anyway to use Editor Insection in cases where you do not have just to find 1 component, no workaround for that (unless you use Svelto or Svelto-ECS wich are rather advanced topics.)

Unity game manager. Script works only one time

I'm making simple game manager. I have a script, which will be accessible from all scenes in the game. And I need to check values of its variables after loading new scene. But my code runs only once after starting the simulation while an object with this script exists in all scenes. What is wrong? Why doesn't it work after loading a new scene?
In every Unity project you must have A PRELOAD SCENE.
It is quite confusing that Unity does not have a preload scene "built-in".
They will add this concept in the future.
Fort now you have to click to add a preload scene yourself.
This is the SINGLE GREATEST MISUNDERSTANDING for new programmers trying Unity!
Fortunately, it is extremely easy to have a preload scene.
Step 1.
Make a scene named "preload". It must be scene 0 in Build Manager.
Step 2.
In the "preload" scene make an empty GameObject called, say, "__app".
Simply, put DontDestroyOnLoad on '__app'.
Note:
This is the only place in the whole project you use DontDestroyOnLoad.
It's that simple.
In the example: the developers have made a one-line DDOL script.
Put that script on the "__app" object.
You never have to think about DDOL again.
Step 3
Your app will have (many) "general behaviors". So, things like database connectivity, sound effects, scoring, and so on.
You must, and can only, put your general behaviors on "_app".
It's really that simple.
The general behaviors are then - of course - available everywhere in the project, at all times, and in all scenes.
How else could you do it?
In the image example above, notice "Iap" ("in-app purchase") and the others.
All of your "generally-needed behaviors" - sound effects, scoring, and so on - are right there on that object.
Important...
This means that - of course, naturally -
...your general behaviors will have ordinary Inspectors, just like everything else in Unity.
You can use all the usual features of Unity, which you use on every other game object. Inspector variables, drag to connect, settings, and so on.
(Indeed: say you've been hired to work on an existing project. The first thing you will do, is glance at the preload scene. You will see all the "general behaviors" in the preload scene - sound effects, scoring, AI, etc etc. You will instantly see all the settings for those things as Inspector variables ... speech volume, playstore ID, etc etc.)
Here's an example "Sound effects" general behavior:
Looks like there's also a "voice over" general behavior, and a "music" general behavior".
To repeat. Regarding your "general behaviors". (Sound effects, scoring, social, etc etc.) These CAN ONLY GO on a game object in the preload scene.
This is not optional: there's no alternative!
It's that easy.
Sometimes engineers coming from other environments get caught up on this, because it seems like "it can't be that easy".
To repeat, Unity just plain forgot to "build-in" a preload scene. So, you simply click to add your preload scene. Don't forget to add the DDOL.
So, during development:
Always start your game from Preload scene.
It's that simple.
Important: Your app will certainly have "early" scenes. Examples:
"splash screen"
"menu"
Note. Tou CAN NOT use splash or menu as the preload scene. You have to literally have a separate preload scene.
The preload scene will then load your splash or menu or other early scene.
The central issue: "finding" those from other scripts:
So you have a preload scene.
All of your "general behaviors" are simply on the preload scene.
You next have the problem of, quite simply, finding say "SoundEffects".
You have to be able to find them easily, from, any script, on any game object, in any of your scenes.
Fortunately it is dead easy, it is one line of code.
Sound sound = Object.FindObjectOfType<Sound>();
Game game = Object.FindObjectOfType<Game>();
Do that in Awake, for any script that needs it.
It's honestly that simple. That's all there is to it.
Sound sound = Object.FindObjectOfType<Sound>();
Tremendous confusion arises because of the 100s of absolutely wrong code examples seen online.
It really is that easy - honest!
It's bizarre that Unity forgot to add a built-in "preload scene" - somewhere to attach your systems like SoundEffects, GameManager, etc. It's just one of those weird thing about Unity. So, the first thing you do in any Unity project is just click once to make a preload scene.
That's it!
A Detail...
Note that, if you really want to type even less (!) lines of code, it's remarkably easy - you can just use a global for each of these things!
This is explained in detail here , many folks now use something like this, a Grid.cs script ...
using Assets.scripts.network;
using UnityEngine;
static class Grid
{
public static Comms comms;
public static State state;
public static Launch launch;
public static INetworkCommunicator iNetworkCommunicator;
public static Sfx sfx;
static Grid()
{
GameObject g = GameObject.Find("_app");
comms = g.GetComponent<Comms>();
state = g.GetComponent<State>();
launch = g.GetComponent<Launch>();
iNetworkCommunicator = g.GetComponent<INetworkCommunicator>();
sfx = g.GetComponent<Sfx>();
}
}
Then, anywhere in the project you can say
Grid.sfx.Explosions();
It's just that easy, that's the whole thing.
Don't forget that each of those "general systems" is on, and can only be on, the DDOL game object in the preload scene.
DylanB asks: "During development it's quite annoying that you have to click to the preload scene every time before you click "Play". Can this be automated?"
Sure, every team has a different way to do this. Here's a trivial example:
// this should run absolutely first; use script-execution-order to do so.
// (of course, normally never use the script-execution-order feature,
// this is an unusual case, just for development.)
...
public class DevPreload:MonoBehaviour
{
void Awake()
{
GameObject check = GameObject.Find("__app");
if (check==null)
{ UnityEngine.SceneManagement.SceneManager.LoadScene("_preload"); }
}
}
But don't forget: what else can you do? Games have to start from a preload scene. What else can you do, other than click to go to the preload scene, to start the game? One may as well ask "it's annoying launching Unity to run Unity - how to avoid launching Unity?!" Games simply, of course, absolutely have to start from a preload scene - how else could it be? So sure, you have to "click to the preload scene before you click Play" when working in Unity - how else could it be?
#Fattie: Thanks for elaborating all this, it's great! There is a point though that people are trying to get through to you, and I'll just give it a go as well:
We do not want every instantiation of everything in our mobile games to do a "FindObjectOfType" for each and every every "global class"!
Instead you can just have it use an Instantiation of a static / a Singleton right away, without looking for it!
And it's as simple as this:
Write this in what class you want to access from anywhere, where XXXXX is the name of the class, for example "Sound"
public static XXXXX Instance { get; private set; }
void Awake()
{
if (Instance == null) { Instance = this; } else { Debug.Log("Warning: multiple " + this + " in scene!"); }
}
Now instead of your example
Sound sound = Object.FindObjectOfType<Sound>();
Just simply use it, without looking, and no extra variables, simply like this, right off from anywhere:
Sound.Instance.someWickedFunction();
Alternately (technically identical), just use one global class, usually called Grid, to "hold" each of those. Howto. So,
Grid.sound.someWickedFunction();
Grid.networking.blah();
Grid.ai.blah();
Here is how you can start whatever scene you like and be sure to reintegrate your _preload scene every time you hit play button in unity editor. There is new attribute available since Unity 2017 RuntimeInitializeOnLoadMethod, more about it here.
Basically you have a simple plane c# class and a static method with RuntimeInitializeOnLoadMethod on it. Now every time you start the game, this method will load the preload scene for you.
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadingSceneIntegration {
#if UNITY_EDITOR
public static int otherScene = -2;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void InitLoadingScene()
{
Debug.Log("InitLoadingScene()");
int sceneIndex = SceneManager.GetActiveScene().buildIndex;
if (sceneIndex == 0) return;
Debug.Log("Loading _preload scene");
otherScene = sceneIndex;
//make sure your _preload scene is the first in scene build list
SceneManager.LoadScene(0);
}
#endif
}
Then in your _preload scene you have another script who will load back desired scene (from where you have started):
...
#if UNITY_EDITOR
private void Awake()
{
if (LoadingSceneIntegration.otherScene > 0)
{
Debug.Log("Returning again to the scene: " + LoadingSceneIntegration.otherScene);
SceneManager.LoadScene(LoadingSceneIntegration.otherScene);
}
}
#endif
...
An alternate solution from May 2019 without _preload:
https://low-scope.com/unity-tips-1-dont-use-your-first-scene-for-global-script-initialization/
I've paraphrased from the above blog to a how-to for it below:
Loading a Static Resource Prefab for all Scenes
In Project > Assets create a folder called Resources.
Create a Main Prefab from an empty GameObject and place in the Resources folder.
Create a Main.cs C# script in your Assets > Scripts or wherever.
using UnityEngine;
public class Main : MonoBehaviour
{
// Runs before a scene gets loaded
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void LoadMain()
{
GameObject main = GameObject.Instantiate(Resources.Load("Main")) as GameObject;
GameObject.DontDestroyOnLoad(main);
}
// You can choose to add any "Service" component to the Main prefab.
// Examples are: Input, Saving, Sound, Config, Asset Bundles, Advertisements
}
Add Main.cs to the Main Prefab in your Resources folder.
Note how it uses RuntimeInitializeOnLoadMethod along with Resources.Load("Main") and DontDestroyOnLoad.
Attach any other scripts that need to be global across scenes to this prefab.
Note that if you link to other scene game objects to those scripts you probably want to use something like this in the Start function for those scripts:
if(score == null)
score = FindObjectOfType<Score>();
if(playerDamage == null)
playerDamage = GameObject.Find("Player").GetComponent<HitDamage>();
Or better yet, use an Asset management system like Addressable Assets or the Asset Bundles.
actually as a programmer who comes to unity world I see none of these approaches
standard
the most simplest and standard way: create a prefab, according to unity docs:
Unity’s Prefab system allows you to create, configure, and store a GameObject complete with all its components, property values, and child GameObjects
as a reusable Asset. The Prefab Asset acts as a template from which you can create new Prefab instances in the Scene.
Details:
Create a prefab within your Resources folder:
if you don't know how to create a prefab study this unity document
if you don't have resources directory create a folder and name it exactly Resources because it is a unity Special folder name
create a script with contents like below:
using UnityEngine;
public class Globals : MonoBehaviour // change Globals (it should be the same name of your script)
{
// loads before any other scene:
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void LoadMain()
{
Debug.Log("i am before everything else");
}
}
assign it to your prefab
and you can make it even better:
use prefab and namespaces together:
in your prefab script:
using UnityEngine;
namespace Globals {
public class UserSettings
{
static string language = "per";
public static string GetLanguage()
{
return language;
}
public static void SetLanguage (string inputLang)
{
language = inputLang;
}
}
}
in your other scripts:
using Globals;
public class ManageInGameScene : MonoBehaviour
{
void Start()
{
string language = UserSettings.GetLanguage();
}
void Update()
{
}
}

Categories