Skip to content

Instantly share code, notes, and snippets.

View cubed2d's full-sized avatar

Louise James cubed2d

  • Generic Evil Business
  • London
View GitHub Profile
@cubed2d
cubed2d / ReadOnlyPropertyDrawer.cs
Created July 25, 2016 05:39
Read Only property drawer extension for unity
// NOTE put in a Editor folder
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
using UnityEngine;
using System;
using System.Collections;
public class SineSynth : MonoBehaviour
{
[Range(100,1000)]
public double Frequency = 440;
[Range(0.05f, 0.3f)]
public double Gain = 0.05; // Amplitude?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public static class TransformExtentions
{
public static Transform FindInChildren(this Transform t, string name)
{
// Heres a smart linq version. i dont like it.
@cubed2d
cubed2d / SpriteImporter.cs
Last active June 8, 2023 20:48
A Unity asset preprocessor script to automatically set up imported textures as sprites. Put into a file called SpriteImporter.cs in a folder called Editor in your project
using UnityEditor;
public class SpriteImporter : AssetPostprocessor // AssetPostprocessor gives us a bunch of tools we can use to modifiy assets on import
{
// This hooks in to texture Preprocessing. that means it happens *before* the texture is processed by unity so we can fiddle with its import settings!
// This method will be run on ever asset as it is imported. For example, whenever a new png file is added to the project or if you right click -> reimport on a texture asset.
void OnPreprocessTexture()
{
//if (assetPath.Contains("UI") || assetPath.Contains("Sprite")) // Optional filtering. We can set it to only effect files with certain keywords in the path. Uncomment if useful
{
In PlayerScript:
// Sprite direction
if (xInput > 0) {
transform.rotation = Quaternion.Euler (0, 0, 0);
} else if (xInput < 0) {
transform.rotation = Quaternion.Euler (0, 180, 0);
}
// Aiming
using UnityEngine;
using UnityEditor;
using System.Collections;
public class MakeMMOButton : MonoBehaviour {
[MenuItem("Edit/Make MMO", false, 5)]
static void OnMakeMMOButton()
{
throw new System.NotImplementedException();
public Random random; <- put this in one of your class files (Usually Xna has a Game class? probably that one
random = new Random(); <-- In the same file put this somewhere. Xna games tend to have a LoadContent or a Initialize method? that would be a good place. This creates and instance of the Random class. Random is an object that has lots of methods for giving you differnt types of random numbers, like the name suggests! :)
var item = myList[random.Next(myList.count)]; <- WHen you want to get the random item. the Next method on Random makes it generate the next random number. Passing in the length/count of the items in your list makes it generate a number thats between 0 and that number.
@cubed2d
cubed2d / Degub.cs
Created October 16, 2014 13:07
fix for error CS0103: aka Degub
public static class Degub
{
public static void Log(object message)
{
Debug.Log(message);
}
public static void Log(object message, Object context)
{
Debug.Log(message, context);
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityColor32 = UnityEngine.Color32;
[Serializable]
[StructLayout(LayoutKind.Explicit)] // Tell .net that we know exactly where we want these values
public struct Color32
{
[FieldOffset(0)]
using UnityEngine;
using System.Collections;
public class APIExamples : MonoBehaviour
{
public int randomColorCount = 5;
public Color[] RandomBrightColors;
public Color[] RandomPastelColors;
public Color[] RandomDarkColors;