Unity - How to change player spritesheet while runtime? - unity3d

I'm currently working on a 2D pixel Jump'n'Run. I want the player to be able to "buy" new skins for the player-character. I have multiple sprite-sheets. They all have the same structure. I'm using sprite animations.
How can I change the sprite-sheet at runtime? I found the following solution, but it's very resource intense: https://youtu.be/HM17mAmLd7k?t=1818
Sincerly,
Julian

The reason it's so resource intensive in the video is because the all the sprites are loaded in each LateUpdate(), which is once per frame. The script looks like it's grabbing all the sprites in the sprite-sheet and loading them every frame so that if the spriteSheetName ever changes, it will update the renderer on the next frame.
I don't believe that's necessary and in the video he mentions that it's just being used as an example. What I'd do is move it out of the LateUpdate() method and into its own method that can be called only whenever the user wants to change the sprite-sheet. So instead of mindlessly loading the sprites from the sprite-sheet each frame, you'll only load them whenever the user selects it.
That should accomplish drastically cutting down the intensity of this script because you're not loading all the sprites in a sprite-sheet and looping through each of their renderers on every single frame.

Related

Why do certain sprites ,look as if they're further back in the window?

I am learning how to make games using unity, i have used free sprite packs from unity's store.
i have selected each individual sprite and changed its z position to 0, but when the game runs it looks completely different to how it looks in the editing window.
please help.
Here is a video I recorded.
https://youtu.be/2hb9m01PQBk
Are your sprites on a prefab, or children of another game object? The Transform of the parent object affects the transform of all children.
It looks like this is a camera problem. Your camera is set to perspective, it is possible to do parallax backgrounds with a perspective camera and you may have already accomodated for it, but that is the first thing I would check.
You can modify this by going to your Main Camera and selecting Orthographic from the Projection drop-down menu.

Get Current Animation Frame or Sprite Name

In Unity 2D, is there a way to get the current sprite used either by a SpriteRenderer or an Animation? The idea I have is to have multiple GameObjects to use for a player character, and from the main one which receives animations and the script, edit the others based on the sprite currently used.
Now, I know that to get the current sprite name I can do this:
GetComponent<SpriteRenderer>().sprite.name
But that will only return the sprite used for the main GameObject's SpriteRenderer, and the name of it would not change even as animation changes it in Game Mode
GetComponent<SpriteRenderer>().sprite.name
This 100% does work , just tried it now and it is changing the name for each current sprite that the animator is changing.
You need to give more information and maybe a video since there is a problem somewhere else.

Unity: Get joint rotations of each frame from FBX file

I have some animations stored in FBX files that I need to analyse. I need to know the localRotations and localPositions of each joint for each frame.
My current approach is to use Unity and assign the FBX file to a state in an animation controller and play it. Then I assign a script to the root joint and in each Update() call I recursively read the localRotation of each joint and save them to a list.
My questions are now:
How can I guarantee that I get each frame from the FBX exactly once? Should i use FixedUpdate instead of Update? I want this script to be not influenced by any load caused by other programs that run in parallel.
How can I get the current frame number that is played? How can I detect if the animation has ended or is just being looped?
I need this only to get the joint rotations from the animation. It is not relevant that the animation is actually played on the screen. If there is an easier way, I would also be interested. I want to extract this information from around 200 FBX files that have a length of 2-10 seconds.
I only want to use Unity to process the files, everything else will be done in Python as I have almost no Unity experience.

Does disabling graphics game objects prevent them from being affected by scripts?

I'm using frames (bounding boxes) to disable all game objects within an area when the player is not in that area. I want to know if parallax scripts will affect a disabled frame of objects. I don't want the parallax of background and foreground layers to be off when the player enters a new frame.
Slightly off-topic, but still relevant enough: I'm building the frames to be seemless transitions; is there a way to prevent parallax overlap at the transition point?
Scripts on disabled gameobjects do not recieve Update calls, as you've noticed. There are a few things you can do:
Not disable the entire gameobject when its outside the camera frame, instead only disable the renderer attached. This way your parralax script is still updated. Though its questionable if there are performance benefits to this, Unity does Frustrum culling by default.
Dont have individual parallax scripts, instead have one ParallaxManager in which you register all objects. This manager is never disabled, as an added benefit there is less overhead calling update.
Instead of update start some coroutine on another object, that isnt disabled. For example, in Parallax you can start a coroutine from the Camera gameobject. That way it keeps going, eventhough the parallax is disabled.
Try to calculate the right position in the OnEnable. That way you can still disable objects, and they appear at the right position when they are enabled again.

Is there a method or way to mask one ui image from multiple images?

Currently, i am working on a game project as a hobby and have been trying to make some game mechanics that i havent really made before. I am currently working on an inventory system for the player that displays the player's weapon collection as well as weapon parts.
I am currently working on making the weapon inventory. My current problem is rendering the weapon models as icons per inventory slot.
The current method that I am planning on rendering any 3D asset is by using a camera with a render texture and assigning that texture to a RawImage Component. I was planning to do the same for the inventory slots. However, I realised that i would need a camera per slot with its own render texture which would be both time consuming to create as well as making the game less performant (since there would be a lot of cameras).
I have searched if there is a way to mask a texture from multiple objects so that I can skip the part of creating 100 different cameras and instead use one. So far, nothing has come up in my findings that could remotely help me.
So, as stated in the title; Is there a method or way to mask one ui image from multiple images? If not, is there an alternative solution to my problem?