Skip to content

Instantly share code, notes, and snippets.

View unitycoder's full-sized avatar
✅
‏‏‎

mika unitycoder

✅
‏‏‎
View GitHub Profile
@unitycoder
unitycoder / Metronome.cs
Created February 16, 2020 18:48
Metronome OnAudioFilterRead
// The code example shows how to implement a metronome that procedurally
// generates the click sounds via the OnAudioFilterRead callback.
// While the game is paused or suspended, this time will not be updated and sounds
// playing will be paused. Therefore developers of music scheduling routines do not have
// to do any rescheduling after the app is unpaused
// https://forum.unity.com/threads/do-people-not-realize-how-bad-audiosource-dsptime-is-can-someone-explain-how-it-works.402308/#post-5486028
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
@unitycoder
unitycoder / DecodeAudioFromTexture.cs
Created February 15, 2020 15:25
Encode/Decode Audio to/from PNG Alpha Channel
// https://unitycoder.com/blog/2020/02/15/encode-decode-audio-to-from-png-alpha-channel
// Decode Raw audiodata from PNG alpha channel
// Usage: Assign source texture and audioclip
// Note: You need to use same seed and samplerate that was used in encoding
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
@unitycoder
unitycoder / DeShuffle.cs
Last active February 11, 2020 22:00
Shuffle Array
void Shuffle<T>(int seed, ref T[] array)
{
Random.InitState(seed);
int n = array.Length;
//Debug.Log("len = " + n);
int rr = 0;
while (n > 1)
{
int k = Random.Range(0, n--);
//int k = rand[rr++];
@unitycoder
unitycoder / usability.md
Last active January 8, 2023 16:49
Usability Notes UX UI
@unitycoder
unitycoder / latency.txt
Created February 11, 2020 10:57 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@unitycoder
unitycoder / xr-interaction-toolkit.md
Last active May 20, 2024 03:42
Unity XR Interaction Toolkit Notes

get raycastinteractor UI hit position from script

  • rayInteractor.TryGetCurrentUIRaycastResult(out res))

right controller ray, cannot click UI elelemts

  • check that rayinteractor has UI layer enabled (in raycast configuration, raycast mask)

Reset Draggable object, unless its placed on socket

xr ray interactor, hover and trigger select on box

@unitycoder
unitycoder / Texture2Texture2D.cs
Created February 10, 2020 11:14
Texture to Texture2D using blit
// http://baba-s.hatenablog.com/entry/2018/02/26/210100
using System.IO;
using UnityEditor;
using UnityEngine;
public static class Texture2Texture2D
{
[MenuItem( "Tools/Texture2Texture2D" )]
private static void Texture2Texture2D()
{
@unitycoder
unitycoder / LookAt.cs
Last active December 3, 2024 11:03
transform.LookAt to opposite direction
// transform.LookAt to opposite direction
transform.rotation = Quaternion.LookRotation(transform.position - target.position);
// or
transform.LookAt(2 * transform.position - target.position);
// transform.LookAt Y axis https://answers.unity.com/questions/36255/lookat-to-only-rotate-on-y-axis-how.html
Vector3 targetPos = new Vector3(target.position.x, transform.position.y, target.position.z);
transform.LookAt(targetPos);
// rotate transform.forward ?
@unitycoder
unitycoder / GameObjectMouseOver.md
Created February 4, 2020 08:43
Using IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler on a regular GameObject

checklist:

  • Added EventSystem game object to scene (Create -> UI -> Event System)
  • Camera has a Physics Raycaster (Select Main Camera, Add Component -> Event -> Physics Raycaster)
  • Selectable object is a MonoBehavior-derived class that implements IPointerClickHandler, IPointerDownHandler, and IPointerUpHandler (see accepted answer).
  • Selectable game object includes selectable object MonoBehavior script.
  • Selectable game object includes a collider (box, mesh, or any other collider type).
  • Check Raycaster Event Mask vs. game object's Layer mask
  • Verify no collider (possibly without a mesh) is obscuring the selectable game object.
  • If collider is a trigger, verify that Queries Hit Triggers is enabled in Physics settings.
@unitycoder
unitycoder / MouseRaycast.cs
Created February 4, 2020 06:44
Raycast Physics with Mouse with layermask
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseRaycast : MonoBehaviour
{
public GameObject prefab;
public LayerMask layerMask;
RaycastHit hit;