r/Unity3D – Telegram
r/Unity3D
265 subscribers
12.7K photos
15.6K videos
14 files
48.1K links
News about the Unity engine and project showcases from Reddit. Made possible with @reddit2telegram (@r_channels).
Download Telegram
Some of the environments and characters we created for our Unity game Go Home Annie. CPU light baker still sucks, hope we can get Bakery to work as intended before release.

https://redd.it/1gkvrvd
@r_Unity3D
help with character rotation

So i need to create a game like asteroid for a school project but what i want to add is a system where you can aim with your mouse AND the character change sprite depending of where it face like in enter the gungeon when your mouse is at the top of the screen you see the back of your character and when its at the botom you see the face of your character it goes on with 8 possible direction preferably, so i already know how to make the character rotation follow the mouse thanks to a tutorial but i really don't know how to change the sprite of the character depending of where the mouse is so if you could help with that i would be very gratefull

https://redd.it/1gkwc8r
@r_Unity3D
This media is not supported in your browser
VIEW IN TELEGRAM
A planet made entirely out of text symbols. The entire screen image is drawn symbol by symbol. Each symbol's position is calculated in real time. There’s only one texture with symbols and a lot of sprites on the screen. Created a text editor on a sphere to make planets for my game Effulgence.

https://redd.it/1gl0atx
@r_Unity3D
Efficient way to cycle through sprites in a texture?

I have have a texture with multiple sprites like this:

https://preview.redd.it/0kdpez14sazd1.png?width=245&format=png&auto=webp&s=198405b4bc607a8d66f68d633c86c028675a741b

What I want to do is basically at certain points switch between different sprites in the texture (in the case, it is a crop that is growing with different stages). What I have come up with so far is this:

using ProjectEdg.CoreModule;
using UnityEngine;

namespace ProjectEdg.TextureModule {
public class MultiTextureComponent : MonoBehaviour {
[SerializeField]
private SpriteRenderer _spriteRenderer;

[FoldoutGroup("Debug")]
[SerializeField]
[ReadOnly]
private Texture2D _texture;

[FoldoutGroup("Debug")]
[SerializeField]
[ReadOnly]
private MultiTextureData _multiTextureData;

private float _updateArea = 1f;

private int stage;

private string[] _spriteNames = new string[] { "ParsnipPlant1", "ParsnipPlant2", "ParsnipPlant3" };

#region lifecycle

private void Start() {
_multiTextureData = ResourceManager.Instance.GetTextureData("FarmingCrops");
_texture = ResourceManager.Instance.GetTexture("FarmingCrops");

SetCroppedArea(_spriteNames[stage % 3]);
}

private void Update() {
if (_updateArea > 0) {
_updateArea -= Time.deltaTime;

return;
}

stage += 1;
SetCroppedArea(_spriteNames[stage % 3]);
_updateArea = 1f;
}

#endregion

private void SetCroppedArea(string spriteName) {
var spriteData = ResourceManager.Instance.GetTextureSpriteData("FarmingCrops", spriteName);

if (spriteData == null) {
return;
}

var startingPosition = new Vector2Int(spriteData.StartX, spriteData.StartY);
var size = new Vector2Int(spriteData.Width, spriteData.Height);
var centerOffset = new Vector2(
spriteData.CenterOffsetX != -1 ? spriteData.CenterOffsetX : _multiTextureData.DefaultCenterOffsetX,
spriteData.CenterOffsetY != -1 ? spriteData.CenterOffsetY : _multiTextureData.DefaultCenterOffsetY
);

if (startingPosition.x + size.x > _texture.width || startingPosition.y + size.y > _texture.height) {
LogManager.LogError("the attempted crop area is out of bounds of the texture");

return;
}

var croppedPixels = _texture.GetPixels(startingPosition.x, startingPosition.y, size.x, size.y);
var croppedTexture = new Texture2D(size.x, size.y);

croppedTexture.SetPixels(croppedPixels);
croppedTexture.filterMode = FilterMode.Point;
croppedTexture.Apply();

_spriteRenderer.sprite = Sprite.Create(
croppedTexture,
new Rect(0, 0, size.x, size.y),
new Vector2(centerOffset.x / spriteData.Width, centerOffset.y / spriteData.Height),
_multiTextureData.PixelsPerUnit
);
}
}
}

While this works, my concern with this approach is that each time I am changing the sprite to render, I am creating a new `Texture2D` and `Sprite` and was wondering if there is a more efficient way to do this. For example in Godot, I can have a sprite use the main texture and then just set the region rect for the sprite without creating this extra data (at least explicitly, I don't know what is happening under the hood) and was wondering if there is something like that in Unity?

The best approach I can think of is when loading the main texture, I create and cache the individual textures / sprites in the texture and then just reference those cached items when I need to (like my current example is doing for the main texture). This has the downside that I have to store
all the textures / sprites in memory even when I am not using them.

Is this a good approach? Is there something else already built-in that would handle part of this for me?

https://redd.it/1gl0r3m
@r_Unity3D