Skip to content

Instantly share code, notes, and snippets.

View MattRix's full-sized avatar

Matt Rix MattRix

View GitHub Profile
@MattRix
MattRix / FGScreenScaler
Last active August 18, 2016 17:23
FGScreenScaler - scale a transform so that you can have a world space transform act like it's in screen space
using UnityEngine;
using System.Collections;
using System;
[ExecuteInEditMode]
public class FGScreenScaler : MonoBehaviour
{
//when desiredHeight is 1080, this acts as if you had the camera set to orthographic with size at 540 (half of 1080)
//except this works with a perspective camera
@MattRix
MattRix / SimpleGraphic.cs
Last active December 21, 2025 17:29
How to draw a custom mesh in a UI.Graphic
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
[RequireComponent(typeof(CanvasRenderer))]
public class SimpleGraphic : MaskableGraphic //could extend Graphic if you don't need it maskable
{
public Texture texture;
public override Texture mainTexture
@MattRix
MattRix / ObjExporter.cs
Last active April 4, 2025 21:48
Exports Unity meshes to an obj file
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using System.Text;
public class ObjExporterScript
{
private static int StartIndex = 0;
@MattRix
MattRix / RXStockpile.cs
Last active April 7, 2017 01:31
Stockpile of random items that can adjust their weights as items are picked
using System;
using System.Collections.Generic;
public class RXStockpile<T>
{
//make a stockpile of equal value
static public RXStockpile<T> CreateEqual(params T[] things)
{
RXStockpile<T> stockpile = new RXStockpile<T>();
@MattRix
MattRix / ScreenScaler.cs
Last active December 19, 2016 20:48
Script for scaling a Canvas to match with a perspective camera
using UnityEngine;
using System.Collections;
using System;
[ExecuteInEditMode]
public class ScreenScaler : MonoBehaviour
{
//when desiredHeight is 1080, this acts as if you had the camera set to orthographic with size at 540 (half of 1080)
//except this works with a perspective camera
@MattRix
MattRix / StringDict.cs
Last active December 22, 2016 02:08
Unity serializeable StringDict
using UnityEngine;
using System;
using System.Collections.Generic;
[Serializable]
public class StringDict : Dictionary<string,string>, ISerializationCallbackReceiver
{
[SerializeField] List<String> keys = new List<string>();
[SerializeField] List<String> vals = new List<string>();
@MattRix
MattRix / VertBend.cs
Created December 30, 2016 02:37
Example of doing a vertex modifer for Unity UI stuff
using UnityEngine.UI;
using System.Collections.Generic;
using System;
using UnityEngine;
public class VertBend : BaseMeshEffect
{
public override void ModifyMesh(VertexHelper vh)
{
if(!IsActive()) return;
public void Update()
{
if(Time.time % 5f < Time.deltaTime)
{
Debug.Log("go!");
}
}
if(watchThis != null)
{
var valueStore = watchThis.GetComponent<ValueStore>();
var theField = valueStore.GetType().GetField(field);
if(theField != null)
{
int prevValue = (int)theField.GetValue(valueStore);
theField.SetValue(valueStore,prevValue + value);
}
@MattRix
MattRix / SpriteSource.cs
Last active January 6, 2017 16:53
A thing that lets you load sprites by name (though usually it's better to just use Resources)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[CreateAssetMenu]
public class SpriteSource : ScriptableObject
{
public Sprite notFoundSprite;