This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public partial class Flock : MonoBehaviour | |
{ | |
[SerializeField, Min(0)] float spawnRadius = 5f; | |
[SerializeField, Min(1)] int unitCount = 50; | |
[SerializeField] Transform unitPrefab; | |
[Header("Unit")] | |
[SerializeField, Range(0, 360)] float fieldOfView = 270f; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local eventManager <const> = {} | |
function addEventListener(eventName, listener) | |
local eventListeners = eventManager[eventName] | |
if not eventListeners then | |
eventListeners = {} | |
eventManager[eventName] = eventListeners | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
using Unity.Collections; | |
using UnityEditor.Media; | |
using UnityEditor.Recorder.Encoder; | |
class HVECEncoder : IEncoder | |
{ | |
Process process; | |
public void OpenStream(IEncoderSettings settings, RecordingContext ctx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
/// <summary> | |
/// Represents a color with hue, saturation, and lightness. | |
/// </summary> | |
readonly struct HSL | |
{ | |
public Color Color => HSV.FromHSL(this).Color; | |
public readonly float H; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from https://stackoverflow.com/a/299526 | |
static IEnumerable<MethodInfo> GetExtensionMethods(Assembly assembly, Type extendedType) | |
{ | |
return assembly | |
.GetTypes() | |
.Where(type => type.IsSealed && !type.IsGenericType && !type.IsNested) | |
.SelectMany(type => type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)) | |
.Where(method => method.IsDefined(typeof(ExtensionAttribute), false) && | |
method.GetParameters()[0].ParameterType == extendedType); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This contains no error checking, which you want for such fragile code. | |
// You also probably want to cache `mainWindow` if you call this often. | |
static Rect GetMainWindowPosition() | |
{ | |
var containerWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ContainerWindow"); | |
var showModeField = containerWindowType.GetField("m_ShowMode", BindingFlags.Instance | BindingFlags.NonPublic); | |
var mainWindow = Resources.FindObjectsOfTypeAll(containerWindowType).First(window => (int)showModeField.GetValue(window) == 4); | |
var positionProperty = mainWindow.GetType().GetProperty("position", BindingFlags.Instance | BindingFlags.Public); | |
return (Rect)positionProperty.GetValue(mainWindow, null); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static Object GetMainEditorWindow() | |
{ | |
var containerWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ContainerWindow"); | |
var showModeField = containerWindowType.GetField("m_ShowMode", BindingFlags.Instance | BindingFlags.NonPublic); | |
return Resources | |
.FindObjectsOfTypeAll(containerWindowType) | |
.First(window => (int)showModeField.GetValue(window) == 4); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
/// <summary> | |
/// Attribute to add to Vector3 and Vector3[] fields to make them draggable in the scene. | |
/// </summary> | |
public class Draggable : PropertyAttribute | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example usage inside Editor.OnSceneGUI for Vector3 fields: | |
// | |
// foreach (var property in GetPropertiesWithAttribute<MyCustomAttribute>(serializedObject)) | |
// { | |
// Handles.Label(property.vector3Value, property.name); | |
// } | |
static IEnumerable<SerializedProperty> GetPropertiesWithAttribute<TAttribute>(SerializedObject serializedObject) | |
{ | |
var targetObjectType = serializedObject.targetObject.GetType(); |
NewerOlder