Skip to content

Instantly share code, notes, and snippets.

@GeorgiyRyaposov
GeorgiyRyaposov / KeyValueAttribute.cs
Created October 9, 2020 04:32
Change displayed name of serialisable item in list
using UnityEngine;
namespace Assets.Game.Utils
{
public class KeyValueAttribute : PropertyAttribute
{
public readonly string PropertyName;
public KeyValueAttribute(string propertyName)
{
@GeorgiyRyaposov
GeorgiyRyaposov / UnityToCubeHex.cs
Created November 18, 2020 06:57
Convert unity hex coords to cube hex coords
private Vector3Int UnityCellToCube(Vector3Int cell)
{
var yCell = cell.x;
var xCell = cell.y;
var x = yCell - (xCell - (xCell & 1)) / 2;
var z = xCell;
var y = -x - z;
return new Vector3Int(x, y, z);
}
private Vector3Int CubeToUnityCell(Vector3Int cube)
@GeorgiyRyaposov
GeorgiyRyaposov / SnapCurve.cs
Created September 23, 2021 04:26
When you need to snap value, e.g. each 45 from 0 to 360
private AnimationCurve GetSnapCurve(float min, float max, float snapAt, float snapAmount, WrapMode wrapMode = WrapMode.Clamp)
{
var keyFrames = new List<Keyframe>
{
new Keyframe(0, min),
new Keyframe(1, max)
};
for (float i = snapAt; i < max; i += snapAt)
{
@GeorgiyRyaposov
GeorgiyRyaposov / Debug.cs
Created September 30, 2021 09:27
Debug capsule or sphere even from not monobehaviours
private List<GameObject> _debug = new List<GameObject>();
private void ClearDebug()
{
for (int i = _debug.Count - 1; i >= 0; i--)
{
GameObject.Destroy(_debug[i]);
}
_debug.Clear();
}
private void Debug(Vector3 pos, Color color)
//To get the difference C between quaternions A and B you do this:
C = A * Quaternion.Inverse(B);
//To add the difference to D you do this:
D = C * D;
/////////////////
//World vs local
//An easy way to keep track of the order of operations for quaternions is to think of it in terms of world and local rotations.
//When multiplying a quaternion the world rotation is on the left, and the local rotation is on the right, like this:
@GeorgiyRyaposov
GeorgiyRyaposov / PathFinding.cs
Created February 23, 2023 02:10
SquareGrid + path finding
using System.Collections.Generic;
using Unity.Mathematics;
namespace Grids.Square
{
public class PathFinding
{
private const int MOVE_STRAIGHT_COST = 10;
private const int MOVE_DIAGONAL_COST = 14;
@GeorgiyRyaposov
GeorgiyRyaposov / ScriptableObjectIdAttribute.cs
Last active April 16, 2025 07:35
Generate unique id for scriptable + duplicate check
using System;
using UnityEngine;
namespace Code.Scripts.Utils
{
public class ScriptableObjectIdAttribute : PropertyAttribute { }
#if UNITY_EDITOR
[UnityEditor.CustomPropertyDrawer(typeof(ScriptableObjectIdAttribute))]
public class ScriptableObjectIdDrawer : UnityEditor.PropertyDrawer
@GeorgiyRyaposov
GeorgiyRyaposov / SquareGrid.cs
Created January 14, 2025 11:17
Simple square grid: converts position to index; keep flat list of grid items
using Unity.Burst;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Events;
namespace Grids.Square
{
[BurstCompile]
public class SquareGrid<T>
{
@GeorgiyRyaposov
GeorgiyRyaposov / QuadTree.cs
Created January 26, 2025 02:31
Generic 2d quad tree implementation
using System.Collections.Generic;
using UnityEngine;
namespace Code.Scripts.Utils
{
public class QuadTree<T> where T : Component
{
private const int Capacity = 4;
private readonly Rect _bounds;
@GeorgiyRyaposov
GeorgiyRyaposov / HexDirection.cs
Last active March 11, 2025 10:59
Unity hexagon grid helpers
namespace Code.Scripts.Data.Hexes
{
public enum HexDirection
{
NE,
E,
SE,
SW,
W,
NW