Skip to content

Instantly share code, notes, and snippets.

View mandarinx's full-sized avatar

Thomas Viktil mandarinx

View GitHub Profile
@mandarinx
mandarinx / NormalsVisualizer.cs
Last active May 3, 2024 01:53
Visualize mesh normals in Unity3D
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(MeshFilter))]
public class NormalsVisualizer : Editor {
private const string EDITOR_PREF_KEY = "_normals_length";
private Mesh mesh;
private MeshFilter mf;
private Vector3[] verts;
@mandarinx
mandarinx / ScrollRectAutoScroll.cs
Created August 22, 2017 11:30 — forked from QubitsDev/ScrollRectAutoScroll.cs
Unity3d ScrollRect Auto-Scroll, Dropdown Use: Places this component in the Template of Dropdown (with the ScrollRect component)
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[RequireComponent(typeof(ScrollRect))]
public class ScrollRectAutoScroll : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public float scrollSpeed = 10f;
private bool mouseOver = false;
@mandarinx
mandarinx / TestStateMachine.cs
Created August 24, 2018 08:42
Example of a simple state machine
using System;
using System.Collections.Generic;
using UnityEngine;
public enum State {
Idle = 0,
InMenu = 1,
InGame = 2,
InGameMuffled = 3,
EndGame = 4,
@mandarinx
mandarinx / README.md
Created August 24, 2016 06:53
Sprite outlines shader
@mandarinx
mandarinx / Outline.shader
Created March 28, 2022 07:48 — forked from ScottJDaley/Outline.shader
Wide Outlines Renderer Feature for URP and ECS/DOTS/Hybrid Renderer
// Original shader by @bgolus, modified slightly by @alexanderameye for URP, modified slightly more
// by @gravitonpunch for ECS/DOTS/HybridRenderer.
// https://twitter.com/bgolus
// https://medium.com/@bgolus/the-quest-for-very-wide-outlines-ba82ed442cd9
// https://alexanderameye.github.io/
// https://twitter.com/alexanderameye/status/1332286868222775298
Shader "Hidden/Outline"
{
Properties
@mandarinx
mandarinx / optimizations.md
Last active September 28, 2023 03:51
Unity3D optimization tips

Unity3D optimization tips

Some code allocates memory when running in the editor and not on the device. This is due to Unity doing some extra error handling so possible error messages can be output in the console. Much of this code is stripped during build. It's therefore important to profile on device, and do it regularly.

Optimizations often makes code more rigid and harder to reason. Don't do it until you really need to.

When profiling, wrap methods or portions of a method in Profiler.BeginSample(string name) and Profiler.EndSample() to make them easier to find in the profiler.

public class SomeClass {
@mandarinx
mandarinx / dod.md
Created October 26, 2016 06:47
Data Oriented Design

Data Oriented Design in game development

DOD is about

  • Understanding your data.

Mike Acton from Insomniac Games said that in order to understand the problem, you need to understand the data. If you don’t understand any of them you will never find the optimal solution.

  • Focusing on the data flow and how data is read and written.
@mandarinx
mandarinx / notes.md
Last active July 19, 2023 14:08
altitude fog shader

Copied from https://jakobknudsen.wordpress.com/2013/08/06/altitude-fog/ for archival purpose.

When you submerge an object in water you expect it to become more and more obscured as it sinks further into the depths. As far as I can see, this is usually accomplished in Unity3D with a fullscreen image-effect like the global fog standard asset. But that only works in the pro-version of Unity, and I want something that works in the free version. This post shows my solution.

In Unity3D, you can implement your own custom fog on a material by writing your own final color function in a custom shader. The final color function is applied after any lightning has been applied, so we wont have light­sources brightening up objects that are supposed to be hidden. The incredibly useful Surface Shader Examples page in the Unity documentation includes an example titled “Custom Fog with Final Color Modifier”, where a custom finalcolor function is used to apply fog based on the vertical position of the pixel in screenspace. Thi

@mandarinx
mandarinx / ScreenShake.cs
Created January 21, 2016 08:11 — forked from insominx/gist:f3acce17108d0a2e4619
Screen shake in Unity
IEnumerator Shake() {
float elapsed = 0.0f;
Vector3 originalCamPos = Camera.main.transform.position;
while (elapsed < duration) {
elapsed += Time.deltaTime;
@mandarinx
mandarinx / Unity_NewerThan2017_1.cs
Last active November 5, 2022 16:49
Unity3D: Detect when play mode changes to what
// For Unity versions newer than 2017.1
using UnityEditor;
[InitializeOnLoad]
public class DetectPlayModeChanges {
static DetectPlayModeChanges() {
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}