Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
ai.fluctio.fluctio-sim / Utils / General / GenericVector3.cs
Size: Mime:
using System;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Fluctio.FluctioSim.Utils.General
{
	[Serializable]
	public record Vector3<T>(T X, T Y, T Z)
	{
		[field: SerializeField] public T X { get; set; } = X;
		[field: SerializeField] public T Y { get; set; } = Y;
		[field: SerializeField] public T Z { get; set; } = Z;
		public Vector3(T value) : this(value, value, value) {}
	}

	[Serializable]
	public record Vector3Bool(bool X, bool Y, bool Z) : Vector3<bool>(X, Y, Z)
	{
		public Vector3Bool(bool b) : this(b, b, b) {}
		
		public static Vector3 operator*(Vector3Bool b, Vector3 f)
		{
			return new Vector3(
				b.X ? f.x : 0,
				b.Y ? f.y : 0,
				b.Z ? f.z : 0
			);
		}

		public int Count => (X ? 1 : 0) + (Y ? 1 : 0) + (Z ? 1 : 0);
	}
	
	#if UNITY_EDITOR
	[CustomPropertyDrawer(typeof(Vector3<>), true)]
	public class Vector3Drawer : PropertyDrawer
	{
		private static readonly string[] PropertyPartNames = {"X", "Y", "Z"};

		public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
		{
			using var propertyScope = new EditorGUI.PropertyScope(position, label, property);
			using var indentScope = new EditorGUI.IndentLevelScope(0);
			
			position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

			var width = position.width / PropertyPartNames.Length;
			var padding = position.width / 15;
			const int labelWidth = 15;
			for (var i = 0; i < PropertyPartNames.Length; i++)
			{
				var labelRect = new Rect(position.x + i*width, position.y, labelWidth, position.height);
				var propertyPartName = PropertyPartNames[i];
				EditorGUI.LabelField(labelRect, propertyPartName);

				var boolRect = new Rect(position.x + i*width + labelWidth, position.y, width-padding-labelWidth, position.height);
				var propertyPart = property.FindPropertyRelative(Util.GetBackingFieldName(propertyPartName));
				EditorGUI.PropertyField(boolRect, propertyPart, GUIContent.none);
			}
		}
	}
	#endif
}