Object Class

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 10

Object 1

Singleton?

If you don’t know what a Singleton is, you probably should. A Singleton is a script
that cannot be an Instanced or ‘Duplicated’. It is, well… Single.
I recommend using Singletons for things that do not need to be copied multiple
times during a game. Such as an Inventory System. Typically, the player only needs
one Inventory, so we don’t want to call Instances of it, we only want one. And when
we call it, we want to make sure it doesn’t get duplicated.

Delegate and Event?


A Delegate can be thought of as a reference pointer to an object/method. When it
gets called, it notifies all methods that reference the delegate.
So, first things first…

Define a Delegate and the method that gets called when it fires…

The delgate called ‘OnClickEvent’ passes a ‘GameObject’ that we can use to define
what game object it came from. Then, we defined an ‘event’ OnClick that gets called
when the delegate is called.
Now, in the same script, we need to call the delegate and pass it our GameObject.
I’ve done this through a Raycast…

public class Clicker : MonoBehaviour {


// Event Handler
public delegate void OnClickEvent(GameObject g);
public event OnClickEvent OnClick; // Handle our Ray and Hit
void Update () {
// Ray Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
// Raycast Hit
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100)) {
// If we click it
if (Input.GetMouseButtonUp(0)) { // Notify of the event!
OnClick(hit.transform.gameObject);
}
}
}
}

As you can see, if the Ray has contact an Object in the scene and we Left-Mouse-Click,
we call the event and pass the GameObject.
The last thing we must do is reference the delegate from our other scripts that are
listening to the call. For this I’ve created a class called ‘GoldPile’.

public class GoldPile : MonoBehaviour {


// Awake
void Awake () {
// Start the event listener
Clicker.Instance.OnClick += OnClick;
}
// The event that gets called
void OnClick(GameObject g) {
// If g is THIS gameObject
if (g == gameObject) {
Debug.Log("Hide and give us money!");
// Hide
gameObject.active = false;
}
}
}

What is Batching and what is the use of Batching?

Static batching allows the engine to reduce draw calls for geometry of any size
provided it shares the same material, and does not move. It is often more efficient
than dynamic batching (it does not transform vertices on the CPU), but it uses more
memory.
Difference between Start and Awake Unity Events
Quoting from the docs:
The difference between Awake and Start is that Start is only called if the
script instance is enabled. This allows you to delay any initialization code,
until it is really needed. Awake is always called before any Start functions.
This allows you to order initialization of scripts.
•Update
•LateUpdate
•FixedUpdate
LateUpdate is called after all Update functions have been called. This is
useful to order script execution. For example a follow camera should always
be implemented in LateUpdate because it tracks objects that might have
moved inside Update.
Also note that LateUpdate is called after animations are applied - this means you can
implement procedural animation in LateUpdate which modifies the pose sampled by
the animation system.
FixedUpdate is called every fixed framerate frame, if the MonoBehaviour is
enabled. FixedUpdate should be used instead of Update when dealing with
Rigidbody. For example when adding a force to a rigidbody, you have to
apply the force every fixed frame inside FixedUpdate instead of every frame
inside Update.
I hope that helps. You can read more about overridable functions on the scripting
reference page for MonoBehaviour.

What is the use of AssetBundle in Unity?


Specifically, an AssetBundle is a collection of assets and/or scenes from a project
saved in a compact file with the purpose of being loaded separately to the built
executable application.AssetBundles can be loaded on demand by a game
orapplication built in Unity.
What is the use of deltatime?

Time.deltaTime should be used, when you want movement or any other


constantvariable change, to be the same speed, no matter the framerate. DeltaTime
is the time between the last and current frame.
It's the time passed since the last updated frame expressed in seconds. It's used to
move things with a constant velocity for example and is crucial for timers, animations
and such. Because frames ain't fixed-timestep (meaning one frame maybe take 0.1
seconds to process when another frame maybe take ...

Is this possible to collide two mesh collider,if yes then How?

Add convex mesh colliders to them both. Add a rigidbody to at least one of them.
Difference between Static and Dynamic Batching.

What do you mean by Inheritance ? Explain with example.

Inheritance is an Object Oriented Programming concept (OOP) used to access and


reuse the properties or methods of one class from another.
The class being inherited from is known as the ‘base’ class, where the one inheriting
from the base class is known as the ‘derived’ class.

Let’s think of it like this:

John (derived class), is a Human, consequently he has Inherited certain abilities, he


can Jump, Dance, SpinKick etc,
Zoltron however is an Alien, so he cannot Dance, but he has inherited the ability to
Teleport. Both John and Zoltron are unique, yet they are both Sentient beings (base
class), so they both share certain properties such as health and age.

difference between abstract class and interface in c#


An interface is not a class. ... The main difference between them is that a class can
implement more than one interface but can only inherit from oneabstract class.
Since C# doesn't support multiple inheritance, interfaces are used to implement
multiple inheritance
DIFFERENCE BETWEEN AN ABSTRACT CLASS AND AN INTERFACE:
When we create an interface, we are basically creating a set of methods
without any implementation that must be overridden by the implemented
classes. The advantage is that it provides a way for a class to be a part of two
classes: one from inheritance hierarchy and one from the interface.
1.
When we create an abstract class, we are creating a base class that might have one
or more completed methods but at least one or more methods are left uncompleted
and declared abstract. If all the methods of an abstract class are uncompleted then it
is same as an interface. The purpose of an abstract class is to provide a base class
definition for how a set of derived classes will work and then allow the programmers
to fill the implementation in the derived classes.
article along with the demo project discussed Interfaces versus Abstrac
public abstract class A {
public string sayHi() { return "hi"; } // a method with code in it
public abstract string sayHello(); // no implementation
}

public class B
:A
{
// must implement, since it is not abstract
public override string sayHello() { return "Hello from B"; }
}
Interface is more like a protocol. A list of methods that a class implementing that
interface musthave. But they don't do anything. They have just method prototypes.
public interface A
{
string sayHi(); // no implementation (code) allowed
string sayHello(); // no implementation (code) allowed
}

public class B
:A
{
// must implement both methods
string sayHi() { return "hi"; }
string sayHello() { return "hello"; }
}
2.
Difference between overloading and overriding

Overloading occurs when two or more methods in one class have the same method
name but different parameters. Overriding means having two methods with the same
method name and parameters (i.e., method signature). One of the methods isin the parent
class and the other is in the child class.

The Model-View-Controller (MVC) Pattern


The Model-View-Controller pattern (MVC) splits the software into three major
components: Models (Data CRUD - create, read, update and
delete. ), Views (Interface/Detection) and Controllers (Decision/Action). MVC
is flexible enough to be implemented even on top of ECS or OOP.

Linked List vs Array


Both Arrays and Linked List can be used to store linear data of similar types, but they both

have some advantages and disadvantages over each other.

Following are the points in favour of Linked Lists.

(1) The size of the arrays is fixed: So we must know the upper limit on the number of

elements in advance. Also, generally, the allocated memory is equal to the upper limit

irrespective of the usage, and in practical uses, upper limit is rarely reached.

(2) Inserting a new element in an array of elements is expensive, because room has to be

created for the new elements and to create room existing elements have to shifted.

For example, suppose we maintain a sorted list of IDs in an array id[].

id[] = [1000, 1010, 1050, 2000, 2040, …..].


And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move

all the elements after 1000 (excluding 1000).

Deletion is also expensive with arrays until unless some special techniques are used. For

example, to delete 1010 in id[], everything after 1010 has to be moved.

So Linked list provides following two advantages over arrays

1)Dynamic size

2) Ease of insertion/deletion

Linked lists have following drawbacks:

1) Random access is not allowed. We have to access elements sequentially starting from the

first node. So we cannot do binary search with linked lists.

2) Extra memory space for a pointer is required with each element of the list.

3) Arrays have better cache locality that can make a pretty big difference in performance.

What is an Abstract Class?

An abstract class is a special type of class which acts as a base of other


classes and cannot be instantiated. The implementation logic of an
abstract class is provided by its derived classes. To make a class abstract,
the “abstract” modifier is used which means some missing
implementation needs to be implemented in the class derived from it. It
contains both abstract and non-abstract members. An abstract class is
intended to provide basic functionality which can be further shared and
overridden by multiple derived classes. It is useful to avoid any kind of
code duplication. They look very much like interfaces but with added
functionality.

What is an Interface?
An interface, on the other hand, is not a class which contains only the
signature of functionality. It’s a pattern with no implementation.
Conceptually speaking, it’s just the definition of methods which contains
only the declaration of members. It’s an empty shell which does not
contain the implementation of its members. It’s like an abstract base
class which only contains abstract members such as methods, events,
indexers, properties, etc. It cannot be instantiated directly and its
members can be implemented by any class. Additionally, multiple
interfaces can be implemented by a class, however, a class can only
inherit a single class.

he correct execution order of these event functions when an


application closes is as follows:
Awake()
OnEnable()
Start()
Update()
LateUpdate()
OnGUI()
OnApplicationQuit()
OnDisable()
OnDestroy()

Delegate and Event?


A Delegate can be thought of as a reference pointer to an
object/method. When it gets called, it notifies all methods that reference
the delegate.

1. Use skyboxes to “fake” distant geometry.


2. Learn benefits of Occlusion culling and use it to reduce amount of visible
geometry and draw-calls in case of complex static scenes with lots of
occlusion. Plan your levels to benefit from Occlusion culling.
3. Do not use fog when it is not necessary.
4. Use compressed texture formats when possible, otherwise prefer 16bit
textures over 32bit.
5 Do not use dynamic lights when it is not necessary – choose to bake
lighting instead.
6 Do not use Pixel Lights when it is not necessary – choose to have only a
single (preferably directional) pixel light affecting your geometry.
7 Set Static property on a non-moving objects to allow internal optimizations
like static batching.
8 Keep the number of different materials per scene low – share as many
materials between different objects as possible.
9 If you’re using built-in shaders, pick ones from Mobile or Unlit category.
They work on non-mobile platforms as well; but are simplified and
approximated versions of the more complex shaders.
10 Keep vertex count below 200K..3M per frame when targetting PCs,
depending on the target GPU
11 Use pixel shaders or texture combiners to mix several textures instead of a
multi-

12. Audio Settings –


13. background Music – de-select preload audio data
1. Select Override for android
2. Load Type – Streaming
Audio Clip – SFX
Load Type – Decompress on load

Delegates and Events is very powerful feature of C# programming language,


it helps to write efficient and clean code in unity.

Delegate :
A Delegate is a reference pointer to a method. It allows us to treat method as
a variable and pass method as a variable for a callback. When it get called , it
notifies all methods that reference the delegate. The basic idea behind them
is exactly the same as a subscription magazine. Anyone can subscribe to the
service and they will receive the update at the right time automatically.
I know at this time everything seems very confusing, just wait everything will
be clear when start doing coding.
Vertex Shader vs Surface Shader

The Vertex Shader is a program that runs on each vertex of the 3D


model. Quite often it does not do anything particularly interesting. Here
we just transform vertex position from object space into so called “clip
space”, which is what’s used by the GPU to rasterize the object on screen.
We also pass the input texture coordinate unmodified - we’ll need it to
sample the texture in the fragment shader.

The Fragment Shader is a program that runs on each and every pixel that object
occupies on-screen, and is usually used to calculate and output the color of each pixel.
Usually there are millions of pixels on the screen, and the fragment shaders are
executed for all of them! Optimizing fragment shaders is quite an important part of
overall game performance work.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy