I am using the 3rd party library Google VR in Unity.
Within one C#-source file of that lib, I add a static method call to a class LibClass like this:
void onPrerender() {
MyOwnClass.staticMethod();
(continue with original method call...)
}
But every time I update the library, the call to MyOwnClass.staticMethod() is lost and I have to re-add it manually.
I can not subclass LibClass, because the library would always call LibClass and not LibSubClass.
Is there a way to programmatically add the call to staticMethod() to LibClass? Or any other way?
Edit:
I've been asked to describe the problem better:
OnPreRender is a method of the MonoBehaviour class of Unity.
This method is called before the image of the camera is rendered.
My problem is I need Google VR, which handles 2 separate cameras for each eye, to draw one of the camera images slightly different than the other.
Since OnPreRender is called for each of the cameras, I used this as an entry point for calling a static method in my own class (static only because that way I don't have to instantiate my own class in Google VRs class).
This works fine for many months now, but the Google VR lib is constantly updated and each time I do that, the method call to my own class is lost.
So I was wondering wether it is possible to kind of programmatically say "You there, OnPreRender() in that external class, when you are called by the library, jump over here and do some extra work."
Does that make sense?
You can try using extension methods. Bare in mind this work only for the static calls to object functions.
You can find more:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
Related
Is there a way to check what methods exactly are called on scene's start? And which objects called them? I want to achieve something similar to adding Debug.Log(); to every single method but without doing it. I'd like to know what's going on in a project when I press play.
A scene consists only of game objects (there's nothing else in Unity).
GameObjects may have 1 or more Components attached.
When you click Play ...
It looks like you already know that, the calls such as
void Start()
(if they exist in the script) are called - automatically, by Unity - when each Component on each GameObject is launched with the scene.
It's quite common that inside each Start, you just add something like Debug.Log("starting, script name, on .. " + transform.name) which will simply let you know that that component on that game object has started.
Maybe that's what you need.
Obviously you will have added many functions to your own scripts
void SomeFunction() {
Are you saying you want to know when those functions run?
If so the only way to do that is simply manually add a Debug line, inside each function.
(Regarding functions which you add yourself, example SomeFunction, they are not in any way called automatically. They are only called if you call them (say from Start or similar). You can be sure they are only called if you call them.
I have almost identical classes, PaddleLeft and PaddleRight. I am tired of calling both of those classes when I need something done, I rather them be done all at once. For example, here is what I have to do:
public void pause() {
GameObject.Find("Paddle Objects/paddleRight").GetComponent<Paddle>().setIsPaused(true);
GameObject.Find("Paddle Objects/paddleLeft").GetComponent<Paddle>().setIsPaused(true);
}
And here is what I want to do:
public void pause() {
GameObject.Find("Paddle Objects/paddles").GetComponent<Paddle>().setIsPaused(true);
}
This seems unnecessary, however, in my game, there are times where the same line of code are copied and adjusted to up to ten similar objects.
Question Is there a way to make a super class like in Java for these objects. I have searched the internet and have found info, however I can't seem to understand how to make it work because I can't extend MonoBehavior and a superclass in Unity.
Thanks in advanced!
I have almost identical classes, PaddleLeft and PaddleRight
But your code is totally saying different stuff
GameObject.Find("Paddle Objects/paddleRight").GetComponent<Paddle>().setIsPaused(true);
GameObject.Find("Paddle Objects/paddleLeft").GetComponent<Paddle>().setIsPaused(true);
Maybe you want meant to write the code below?
GameObject.Find("Paddle Objects/paddleRight").GetComponent<PaddleRight>().setIsPaused(true);
GameObject.Find("Paddle Objects/paddleLeft").GetComponent<PaddleLeft>().setIsPaused(true);
I will assume the second code is what you meant to write.
When you have multiple GameObjects or Scripts with similar actions, you should create a central manager script that will make it easy to communicate with a those GameObjects or classes.
Since both your classes are called PaddleRight and PaddleLeft, You can simply call this class PaddleManager.
Don't forget that, of course, PaddleManager is just a script, it's not a "thing" in Unity. Naturally you will attach PaddleManager to some game object. You might ask "where should I attach it?" In a simple game, you might attach it to your camera, say. (Since you always have a camera, other developers working on your project always know to "look n the camera" for odds and ends like sound-effects, managers like this and so on.) Alternately, say that physically all your paddles are associated with (for example) an object that is the ping pong table. Then, a good place to attach PaddleManager.cs would be on the ping pong table. It doesn't matter where you attach it, so long as it is tidy. Some people like to make simply an empty object (you can refer to an empty object as a "marker"), just make an empty object named say "manager holder", put it at 0,0,0, and you can add scripts like PaddleManager.cs to that object.
Your PaddleManager.cs script:
public class PaddleManager : MonoBehaviour
{
private PaddleRight rightPaddle = null;
private PaddleLeft leftPaddle = null;
//Initialize variables
void Start()
{
//Get reference/Cache
rightPaddle = GameObject.Find("Paddle Objects/paddleRight").GetComponent<PaddleRight>();
//Get reference/Cache
leftPaddle = GameObject.Find("Paddle Objects/paddleLeft").GetComponent<PaddleLeft>();
}
//Call to pause and unpause
public void pause(bool pausePaddle)
{
rightPaddle.setIsPaused(pausePaddle);
leftPaddle.setIsPaused(pausePaddle);
}
}
Now, you can access both of your Paddles from one script, in another script.
public class YourOtherScript : MonoBehaviour{
PaddleManager paddleManager = null;
void Start()
{
//Get reference/Cache
paddleManager = GameObject.Find("GameObjectPaddleManaerIsAttchedTo") .GetComponent<PaddleManager>();
//To pause
paddleManager.pause(true);
//To un-pause
paddleManager.pause(false);
}
}
By doing this, you will avoid using static variable and also avoid using GameObject.FindGameObjectsWithTag("paddles")) in foreach loop like mentioned in the other answer. GameObject.Find... functions should NOT be used in the middle of the game because it will slow down your game. You need to use it once and cache the GameObject in the Start function, then you can re-use it without slowing down your game.
First of all paddle right and left are game-objects and not classes , the class name is paddle , and if the same script is on both the objects , the most simplest way would be to put it on another empty object and call it once and the code will work on all game-objects that the script is attached to . BUT! that is only regarding to what I cuold make out of your question , Here is what I really recommend , as you said "because I can't extend Mono-behavior and a super-class in Unity" . Ok so you have class A inheriting monobehavior , and you make class's B and C, then when you inherit them from A you will get all the abilities of a monobehaviour in class B and C and you can attach them to game objects , you can even make start and update functions as vrtual and override them in B and C and you can even call A's function use the keyword Base , So read on it , it will take time but in the long run makes you a better coder
Mm, wait. There is something wrong with your question:
YouPaddleLeft and PaddleRight are not classes. They are GameObjects existing in Unity scene. Class is Paddle to which you get reference by GetComponent<Paddle>() .
Now if you have a variable/function that affects all the instances of the class the same than you shell make them static. (Google static variables and functions if you don't know what they are).
So go to your Puddle class and change the declaration of setIsPaused(bool val) to this:
public static void setIsPaused(bool val) { /* implemenetation */ }
and then make a call to it via class token, not object:
Paddle.setIsPaused(true/false);
note that if the static function has references to class variables all those variables should be marked static as well. (e.g. if you have a bool isPaused than mark it static because it should be the same for all the objects all the time anyways)
I am trying to implement this tween class for my custom game framework, I don't exactly know how to use it.
(The framework is pretty similar to XNA).
This tinyTween class seems very complete, but I cannot quite understand it.
https://gist.github.com/liaoguipeng13/717f83f4971230e70d7e
http://theinstructionlimit.com/flash-style-tweeneasing-functions-in-c
Should I instantiate the tweening class? or can I use it without instantiating it?.
Also for moving sprites I can set in the update my functions
SetVelocityX, SetX, SetAccelX etc...
I am interested in making a sprite move from point (100,150) to (400,600) with a nice moving effect...
Should I instantiate the tweening class?
According to the Tween class, yes, you need to instantiate the class. Though as you can tell by all the classes that inherits from it, you can most likely use one of those for whatever you're planning on using it for, instead of making your own implementations.
I am interested in making a sprite move from point (100,150) to (400,600) with a nice moving effect...
Based on the Tween class, it seems very simple. All you gotta do is call Start with the start and end position, a duration, and a formula for movement. The Tween class should handle the rest, so long as you remember to call Update every frame.
I'm pretty new to C#, coming from a strong background in Java, python, and some "web" stuff. So I'm probably just missing some knowledge here, that hopefully somebody can fill in for me.
Going through the Unity Scripting API, I noticed a lot of methods, listed under "Messages", which is my first point of confusion. What exactly is the difference between a "Message" and a "Method", to me they seem like they are the same thing, but the terminology is throwing me off.
My second point of confusion, is that when I implement a "Message" like Update() in my derived classes, I don't have to use the override keyword. Which is bugging me, because wouldn't I just be hiding it? In which case, wouldn't the update loop just know about MonoBehaviour and not my derived class, and call Update() in MonoBehaviour, instead of my class? This is throwing me for a loop. I'm amusing this is something going on behind the scenes?
I've been getting along really well with Unity, and C#, this is just a sticking point for me, where I feel like I'm missing some knowledge.
One thing to remember is that the Unity team didn't necessarily design their scripting API (including the MonoBehaviour class) to promote good software development principles; they designed it for accessibility and ease of use (especially for non-programmers). This is slowly changing, but there's still a lot of inertia toward "what's easy" instead of "what's right."
That said, let's get to answering your question. Regarding "messages," check out what the docs say about Component.SendMessage:
Calls the method named methodName on every MonoBehaviour in this game object.
When you look at the documentation for, say, Collider and see the methods listed under the Messages section, that's an indication that if you implement those methods, they will be called (via SendMessage) in response to some event or action (OnCollisionEnter, for example, "is called when this collider/rigidbody has begun touching another rigidbody/collider."). I might have implemented them as event delegates, but the Unity team chose to implement their own messaging system using strings, again, in the name of "ease of use."
To that same effect, if you walk up the inheritance chain for a MonoBehaviour class, you'll notice that nowhere in the chain do you actually find declared methods named Update or Start or the other lifecycle events of your scripts. Unity is treating these as magic methods and calling them (again via SendMessage, it appears) if they exist in the implementing class. That's why it doesn't matter if your Update method is public or protected or even private... it'll still get called.
The way they are doing it under the hood is by using reflection to get the method that matches, which is why
void update(){}
void Update(bool inVariable){}
won't get called and
void Update() {}
will
They look for a method that matches both name and parameter signature (in this case does not take in an argument). The first two have an issue with either capitalization or parameter input.
By using reflection you dont need to have a function be public in order to call it but it doesn't hurt it if it is.
They use the same type thing for the message system, which is why you can call any function, though they limit to only taking in one variable with the message.
I'm working on an Unity3D application that basically fetches data from a server and attempts to create objects at runtime. When I'm trying to create this objects, via a constructor on some classes I have defined, I get the following error:
get_name can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function
I can't move this to either Awake or Start since I need some feedback from my GUI (user credentials) before I run the mentioned code.
Any ideas/suggestions?
You can't create objects in your constructor or you will get that error. In fact, you should eschew constructors in general with Unity and favour Awake/Start/etc.
I don't know what you're doing, but there's no reason why you can't Instantiate() the object somewhere in your code, set it up properly on the next lines of code, and then let it's Awake()/Start() take place after that, letting it be fully initialized.
I was able to make it work. Here's a summary of what I did:
Made the class instantiating the classes at run-time, referred as Creator, extend ScriptableObject. This allows to start the Creator class on demand using CreateInstance.
Change the Creator class methods, variables and the class itself to static.
When needed intantiate the Creatorclass via CreateInstance.
Make sure to call the methods that do the class instantiating from Start, Awake or Update as appropriate. In my case it was Update.