Skip to content

Instantly share code, notes, and snippets.

View unitycoder's full-sized avatar
‏‏‎

mika unitycoder

‏‏‎
View GitHub Profile
@unitycoder
unitycoder / ConfigurableJointExtensions.cs
Created May 25, 2020 09:33 — forked from mstevenson/ConfigurableJointExtensions.cs
Extension methods for working with Configurable Joints for Unity
using UnityEngine;
public static class ConfigurableJointExtensions
{
/// <summary>
/// Sets a joint's targetRotation to match a given local rotation.
/// The joint transform's local rotation must be cached on Start and passed into this method.
/// </summary>
public static void SetTargetRotationLocal (this ConfigurableJoint joint, Quaternion targetLocalRotation, Quaternion startLocalRotation)
{
@unitycoder
unitycoder / Vibration.cs
Created July 8, 2020 18:42 — forked from aVolpe/Vibration.cs
Vibration for Unity3d with Android native Call, with fallback to Handlheld.Vibrate()
using UnityEngine;
using System.Collections;
public static class Vibration
{
#if UNITY_ANDROID && !UNITY_EDITOR
public static AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
public static AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
public static AndroidJavaObject vibrator = currentActivity.Call<AndroidJavaObject>("getSystemService", "vibrator");
@unitycoder
unitycoder / BitmapExtensions.cs
Created August 24, 2020 14:10 — forked from nuitsjp/BitmapExtensions.cs
Convert System.Drawing.Bitmap to System.Windows.Media.ImageSource
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);
public static ImageSource ToImageSource(this Bitmap bmp)
{
var handle = bmp.GetHbitmap();
try
{
return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
@unitycoder
unitycoder / Toolkit
Created October 9, 2020 19:37 — forked from gering/Toolkit
persitent data path handling in Unity with fallback
private static string[] _persistentDataPaths;
public static bool IsDirectoryWritable(string path) {
try {
if (!Directory.Exists(path)) return false;
string file = Path.Combine(path, Path.GetRandomFileName());
using (FileStream fs = File.Create(file, 1)) {}
File.Delete(file);
return true;
} catch {
@unitycoder
unitycoder / zigzag-encoding.README
Created October 25, 2020 16:56 — forked from mfuerstenau/zigzag-encoding.README
ZigZag encoding/decoding explained
ZigZag-Encoding
---------------
Maps negative values to positive values while going back and
forth (0 = 0, -1 = 1, 1 = 2, -2 = 3, 2 = 4, -3 = 5, 3 = 6 ...)
(i >> bitlength-1) ^ (i << 1)
with "i" being the number to be encoded, "^" being
XOR-operation and ">>" would be arithemtic shifting-operation
@unitycoder
unitycoder / SpriteWaterShape.cs
Created October 29, 2020 11:15 — forked from staggartcreations/SpriteWaterShape.cs
Sprite shape animated water volume
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
[ExecuteInEditMode]
public class SpriteWaterShape : MonoBehaviour
{
public SpriteShapeController controller;
@unitycoder
unitycoder / RXLookingGlass.cs
Created December 5, 2020 10:21 — forked from MattRix/RXLookingGlass.cs
This class wraps an internal Unity method that lets you to check if a ray intersects a mesh. Place it in an Editor folder.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
using System.Linq;
using System.Reflection;
[InitializeOnLoad]
public class RXLookingGlass
{
/*
* Used for culling tiles in a map renderer I wrote.
* All tiles used geometry in the [-1, 1] range, and their mvp matrix would get precalculated.
* So it was simply a matter of seeing if their projected OBB overlapped with the screen's clip space.
* Returns true if the cube is visible. (false positives for near misses due to conservative w-value)
*/
static inline bool MapTileFrustumCull(const mat4 mvp){
// Clip space center of the cube is the last column of the matrix.
vec4 c = {mvp.m[12], mvp.m[13], mvp.m[14], mvp.m[15]};
// Clip space extents are the component wise absolute values of the first three columns.
//MIT License
//Copyright (c) 2021 Felix Westin
//Source: https://github.com/Fewes/MinimalAtmosphere
//Ported to GLSL by Marcin Ignac
#ifndef ATMOSPHERE_INCLUDED
#define ATMOSPHERE_INCLUDED
// -------------------------------------
@unitycoder
unitycoder / WorldNormalFromDepthTexture.shader
Created February 27, 2021 08:28 — forked from bgolus/WorldNormalFromDepthTexture.shader
Different methods for getting World Normal from Depth Texture, without any external script dependencies.
Shader "WorldNormalFromDepthTexture"
{
Properties {
[KeywordEnum(3 Tap, 4 Tap, Improved, Accurate)] _ReconstructionMethod ("Normal Reconstruction Method", Float) = 0
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
LOD 100