Skip to content

Instantly share code, notes, and snippets.

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

mika unitycoder

✅
‏‏‎
View GitHub Profile
@unitycoder
unitycoder / CaptureScreenshotAsTexture.cs
Created July 14, 2021 13:24
Download PNG file blob from webgl (without server)
// https://forum.unity.com/threads/user-image-download-from-in-webgl-app.474715/#post-3698248
[DllImport("__Internal")]
private static extern void DownloadFile(byte[] array, int byteLength, string fileName);
 
var texture = ScreenCapture.CaptureScreenshotAsTexture();
byte[] textureBytes = texture.EncodeToPNG();
DownloadFile(textureBytes, textureBytes.Length, "screenshot.png");
Destroy(texture);
@unitycoder
unitycoder / Target.cs
Last active July 1, 2021 11:14
MouseOver using EventSystem : IPointerEnterHandler & IPointerExitHandler
// add Physics raycaster to main camera
// add EventSystem gameobject on scene
// attach this script to gameobject with 3D collider
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Target : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
{
@unitycoder
unitycoder / EnterPlayModeBindings.cs
Created June 30, 2021 10:49
EnterPlayModeBindings (Disable special shortcut keys during playmode, like ctrl+z, ctrl+s, using separate shortcuts profile)
// https://forum.unity.com/threads/unwanted-editor-hotkeys-in-game-mode.182073/#post-6886781
using UnityEditor;
using UnityEditor.ShortcutManagement;
 
[InitializeOnLoad]
public static class EnterPlayModeBindings
{
    static EnterPlayModeBindings()
    {
        EditorApplication.playModeStateChanged += ModeChanged;
@unitycoder
unitycoder / IMG2Sprite.cs
Created June 30, 2021 07:08
Generating sprites dynamically from PNG or JPEG files in C# (unity)
// https://forum.unity.com/threads/generating-sprites-dynamically-from-png-or-jpeg-files-in-c.343735/
using UnityEngine;
using System.Collections;
using System.IO;
 
 
public class IMG2Sprite : MonoBehaviour
{
 
 
@unitycoder
unitycoder / launch-process.cs
Last active February 9, 2022 17:33
start launch executable with parameters process
var path = Path.Combine(path, exe);
ProcessStartInfo startInfo = new ProcessStartInfo(path);
startInfo.Arguments ="";
startInfo.UseShellExecute = false;
System.Diagnostics.Process.Start(startInfo);
// launch hidden or minimized
var path = Path.Combine(path, exe);
ProcessStartInfo startInfo = new ProcessStartInfo(path);
startInfo.Arguments ="";
@unitycoder
unitycoder / MeshVolume.cs
Created June 24, 2021 14:01
calculate a 3d Mesh volume in Unity
// https://answers.unity.com/questions/52664/how-would-one-calculate-a-3d-mesh-volume-in-unity.html?childToView=52733#answer-52733
using UnityEngine;
public class MeshVolume : MonoBehaviour
{
void Start()
{
Mesh mesh = GetComponent<MeshFilter>().sharedMesh;
float volume = VolumeOfMesh(mesh);
@unitycoder
unitycoder / ConvertTexture.cs
Created June 24, 2021 09:43
Texture2D to Bitmap
// https://forum.unity.com/threads/bitmap-matrix-from-texture2d.717428/
static Bitmap_p ConvertTexture(Texture2D texture)
{
var original = texture.GetPixels32();
byte[] result = new byte[texture.width * texture.height];
for (int i = 0; i < original.Length; i++)
{
if(original[i].r + original[i].g + original[i].b < Treshold * 255 * 3)
{
@unitycoder
unitycoder / CompileTime.cs
Created June 10, 2021 07:23
measure compile time unity editor
// https://forum.unity.com/threads/compile-times-twice-as-long-then-previous-versions.1123711/
using UnityEngine;
using UnityEditor;
using System.Collections;
class CompileTime : EditorWindow {
 
    bool isTrackingTime;
    double startTime, finishTime, compileTime;
    [MenuItem("Window/Compile Times")]
    public static void Init() {
@unitycoder
unitycoder / SetAORadius.cs
Created June 9, 2021 07:43
Set Post Processing Ambient Occlusion Radius from Script
var volume = go.GetComponent<PostProcessVolume>();
AmbientOcclusion ambientOcclusionLayer = null;
volume.profile.TryGetSettings(out ambientOcclusionLayer);
var ff = ambientOcclusionLayer.radius;
ff.value = ambientOcclusionLayer.radius / (float)scale;
// http://answers.unity.com/answers/1483341/view.html