Last active
August 30, 2016 21:24
-
-
Save Stektpotet/fb23bc79af909533b2f51a1d1a0a2f4a to your computer and use it in GitHub Desktop.
C# Scripts I've made to test implementation of a few topics in number theory
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; | |
using UnityEditor; | |
using System.Reflection; | |
using System; | |
namespace EditorAttributes | |
{ | |
[CustomPropertyDrawer(typeof(ButtonAttribute))] | |
public class ButtonDrawer : PropertyDrawer | |
{ | |
bool m_isPressed; | |
MethodInfo m_PressMethod = null, m_UnpressMethod = null; | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
ButtonAttribute buttonAttribute = attribute as ButtonAttribute; | |
if (property.propertyType == SerializedPropertyType.Boolean) | |
{ | |
if (GUI.Button(position, label)) | |
{ | |
OnPress(property, buttonAttribute); | |
} | |
} | |
} | |
private void OnPress(SerializedProperty property, ButtonAttribute button) | |
{ | |
Type eventOwnerType = property.serializedObject.targetObject.GetType(); | |
string eventName = button.PressMethodName; | |
if (m_PressMethod == null) | |
m_PressMethod = eventOwnerType.GetMethod(eventName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); | |
if (m_PressMethod != null) | |
m_PressMethod.Invoke(property.serializedObject.targetObject, null); | |
else | |
Debug.LogWarning(string.Format("Button: Unable to find method {0} in {1}", eventName, eventOwnerType)); | |
} | |
} | |
[CustomPropertyDrawer(typeof(ToggleButtonAttribute))] | |
public class ToggleButtonDrawer : PropertyDrawer | |
{ | |
bool m_isPressed; | |
MethodInfo m_PressMethod = null, m_UnpressMethod = null; | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
ToggleButtonAttribute buttonAttribute = attribute as ToggleButtonAttribute; | |
if (property.propertyType == SerializedPropertyType.Boolean) | |
{ | |
property.boolValue = GUI.Toggle(position, property.boolValue, label, EditorStyles.miniButton); | |
if (property.boolValue != m_isPressed) | |
{ | |
if (property.boolValue) | |
{ | |
OnPress(property, buttonAttribute); | |
} | |
else | |
{ | |
OnUnpress(property, buttonAttribute); | |
} | |
m_isPressed = property.boolValue; | |
} | |
} | |
} | |
private void OnPress(SerializedProperty property, ToggleButtonAttribute button) | |
{ | |
Type eventOwnerType = property.serializedObject.targetObject.GetType(); | |
string eventName = button.PressMethodName; | |
if (m_PressMethod == null) | |
m_PressMethod = eventOwnerType.GetMethod(eventName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); | |
if (m_PressMethod != null) | |
m_PressMethod.Invoke(property.serializedObject.targetObject, null); | |
else | |
Debug.LogWarning(string.Format("Button: Unable to find method {0} in {1}", eventName, eventOwnerType)); | |
} | |
private void OnUnpress(SerializedProperty property, ToggleButtonAttribute button) | |
{ | |
Type eventOwnerType = property.serializedObject.targetObject.GetType(); | |
string eventName = button.UnpressMethodName; | |
if (m_UnpressMethod == null) | |
m_UnpressMethod = eventOwnerType.GetMethod(eventName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); | |
if (m_UnpressMethod != null) | |
m_UnpressMethod.Invoke(property.serializedObject.targetObject, null); | |
else | |
Debug.LogWarning(string.Format("Button: Unable to find method {0} in {1}", eventName, eventOwnerType)); | |
} | |
} | |
//TODO | |
[CustomPropertyDrawer(typeof(ToolbarAttribute))] | |
public class ToolbarDrawer : PropertyDrawer | |
{ | |
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) | |
{ | |
return base.GetPropertyHeight(property, label)*2; | |
} | |
// Draw the property inside the given rect | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
// First get the attribute since it contains the range for the slider | |
ToolbarAttribute toolbar = attribute as ToolbarAttribute; | |
GUI.Label(position, toolbar.title); | |
if (property.propertyType == SerializedPropertyType.Enum) | |
{ | |
position.height *= 0.5f; | |
position.y += position.height; | |
if (toolbar.multiSelection) | |
{ | |
Rect r = position; | |
r.width /= property.enumDisplayNames.Length; | |
//GUI.BeginGroup(position); | |
//1,2,4,8,16,32,64 | |
property.enumValueIndex ^= Convert.ToInt32(GUI.Toggle(r, ((property.enumValueIndex & 1) == property.enumValueIndex), property.enumDisplayNames[0], EditorStyles.miniButtonLeft)); | |
for (int i = 1; i < property.enumDisplayNames.Length-1; i++) | |
{ | |
} | |
//GUI.EndGroup(); | |
} | |
else | |
{ | |
property.enumValueIndex = GUI.Toolbar(position, property.enumValueIndex, property.enumDisplayNames); | |
} | |
} | |
else | |
{ | |
EditorGUI.LabelField(position, label.text, "Use Toolbar with enum."); | |
} | |
} | |
} | |
} |
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; | |
using UnityEngine; | |
/// <summary> | |
/// These are attributes that makes the inspector-interface of unity | |
/// easier to work with as they each have their own propertydrawer | |
/// <see cref="AttributeDrawer.cs"/> | |
/// </summary> | |
namespace EditorAttributes | |
{ | |
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)] | |
public class ButtonAttribute : PropertyAttribute | |
{ | |
public readonly string PressMethodName; | |
public ButtonAttribute(string MethodName) | |
{ | |
PressMethodName = MethodName; | |
} | |
} | |
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)] | |
public class ToggleButtonAttribute : ButtonAttribute | |
{ | |
public readonly string UnpressMethodName; | |
public ToggleButtonAttribute(string PressMethodName, string UnpressMethodName) : base(PressMethodName) | |
{ | |
this.UnpressMethodName = UnpressMethodName; | |
} | |
} | |
} |
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; | |
using System.Collections.Generic; | |
using EditorAttributes; | |
public class Math : MonoBehaviour | |
{ | |
public int value = 144; | |
[Space] | |
[Button("GetGDC")] | |
public bool greatestCommonDivisor; | |
public void GetGDC() | |
{ | |
Debug.Log(GCD(value, lowerValue)); | |
} | |
public int lowerValue; | |
[Space] | |
[Button("GetRelativelyPrime")] | |
public bool relativelyPrime; | |
public void GetRelativelyPrime() | |
{ | |
RelativelyPrime(value); | |
} | |
[Space] | |
[Button("GetPrimes")] | |
public bool listPrimes; | |
public void GetPrimes() | |
{ | |
Primes(value); | |
} | |
[Space] | |
[Button("GetFactorizedValue")] | |
public bool factorize; | |
public void GetFactorizedValue() | |
{ | |
Factorize(value); | |
} | |
public void RelativelyPrime(int v) | |
{ | |
int count = 0; | |
for (int i = 1; i <= v; i++) | |
{ | |
if(GCD(value, i) <= 1) | |
{ | |
count++; | |
Debug.Log(i); | |
} | |
} | |
} | |
int GCD(int a, int b) | |
{ | |
int r = a % b; | |
if (r > 0) | |
{ | |
Debug.Log(b); | |
return GCD(b, r); | |
} | |
else | |
{ | |
return b; | |
} | |
} | |
public int[] Primes(int v) | |
{ | |
List<int> primes = new List<int>(); | |
for (int n = 2; n < value; n++) | |
{ | |
if(isPrime(n)) | |
{ | |
primes.Add(n); | |
//Debug.Log(n); | |
} | |
} | |
return primes.ToArray(); | |
} | |
public bool isPrime(int n) | |
{ | |
int startingValue = ((n % 10) % 2 == 0) ? n / 2 : n / 3; | |
for(int i = startingValue; i > 1; i--) | |
{ | |
if(n % i==0) | |
{ return false; } | |
} | |
return true; | |
} | |
void Factorize(int v) | |
{ | |
int n = v; | |
List<int> factors = new List<int>(); | |
while (n > 1) | |
{ | |
foreach (int i in Primes(n)) | |
{ | |
if(n % i == 0) | |
{ | |
factors.Add(i); | |
Debug.Log(i); | |
n = n / i; | |
break; //break out of foreach loop | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The AttributeDrawer.cs-file goes into Assets/.../Editor in your unity project