diff --git a/.gitignore b/.gitignore index 66b2761..e53f6ff 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ !/Packages/com.varneon.v-udon.udonity !/Packages/com.varneon.v-udon.vehicles !/Packages/com.varneon.v-udon.vehicles-lite -!/Packages/com.varneon.v-udon.tween \ No newline at end of file +!/Packages/com.varneon.v-udon.tween +!/Packages/com.varneon.v-udon.array-extensions \ No newline at end of file diff --git a/Packages/com.varneon.v-udon.array-extensions/Runtime.meta b/Packages/com.varneon.v-udon.array-extensions/Runtime.meta new file mode 100644 index 0000000..d41496d --- /dev/null +++ b/Packages/com.varneon.v-udon.array-extensions/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2cdf649c8ab407c4ea0b7a51b3844b87 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts.meta b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts.meta new file mode 100644 index 0000000..bad48ee --- /dev/null +++ b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: fce1a502c61eb604e83ec9c772aca614 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/UdonArrayExtensions.cs b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/UdonArrayExtensions.cs new file mode 100644 index 0000000..9325d3b --- /dev/null +++ b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/UdonArrayExtensions.cs @@ -0,0 +1,267 @@ +using System; +using UnityEngine; + +namespace Varneon.VUdon.UdonArrayExtensions +{ + /// + /// Array extension methods for adding partial feature set from List to Udon + /// + public static class UdonArrayExtensions + { + /// + /// Adds an object to the end of the array + /// + /// + /// + /// + public static T[] Add(this T[] array, T item) + { + int length = array.Length; + + T[] newArray = new T[length + 1]; + + array.CopyTo(newArray, 0); + + newArray.SetValue(item, length); + + return newArray; + } + + /// + /// Adds an object to the end of the array while ensuring that duplicates are not added + /// + /// + /// + /// + public static T[] AddUnique(this T[] array, T item) + { + if(Array.IndexOf(array, item) >= 0) { return array; } + + return array.Add(item); + } + + /// + /// Adds the elements of the specified collection to the end of the array + /// + /// + /// + /// + /// + public static T[] AddRange(this T[] array, T[] collection) + { + int length = array.Length; + + int collectionLength = collection.Length; + + T[] newArray = new T[length + collectionLength]; + + array.CopyTo(newArray, 0); + + collection.CopyTo(newArray, length); + + return newArray; + } + + /// + /// Determines whether an element is in the array + /// + /// + /// + /// + /// + public static bool Contains(this T[] array, T item) + { + return Array.IndexOf(array, item) >= 0; + } + + /// + /// Gets the element type of the array type + /// + /// + /// Type.GetElementType() is not exposed in Udon + /// + /// + /// + public static Type GetElementTypeUdon(this Type type) + { + return Type.GetType(type.FullName.TrimEnd(']', '[')); + } + + /// + /// Creates a shallow copy of a range of elements in the source array + /// + /// + /// + /// + /// + /// + public static T[] GetRange(this T[] array, int index, int count) + { + int length = array.Length; + + index = Mathf.Clamp(index, 0, length); + + count = Mathf.Clamp(count, 0, length - index); + + T[] newArray = new T[count]; + + Array.Copy(array, index, newArray, 0, count); + + return newArray; + } + + /// + /// Inserts an element into the array at the specified index + /// + /// + /// + /// + /// + /// + public static T[] Insert(this T[] array, int index, T item) + { + int length = array.Length; + + index = Mathf.Clamp(index, 0, length); + + T[] newArray = new T[length + 1]; + + newArray.SetValue(item, index); + + if (index == 0) + { + Array.Copy(array, 0, newArray, 1, length); + } + else if (index == length) + { + Array.Copy(array, 0, newArray, 0, length); + } + else + { + Array.Copy(array, 0, newArray, 0, index); + Array.Copy(array, index, newArray, index + 1, length - index); + } + + return newArray; + } + + /// + /// Inserts the elements of a collection into the array at the specified index + /// + /// + /// + /// + /// + /// + public static T[] InsertRange(this T[] array, int index, T[] collection) + { + int length = array.Length; + + int collectionLength = collection.Length; + + int newLength = length + collectionLength; + + index = Mathf.Clamp(index, 0, length); + + T[] newArray = new T[newLength]; + + if (index == 0) + { + Array.Copy(array, 0, newArray, collectionLength, length); + Array.Copy(collection, 0, newArray, 0, collectionLength); + } + else if (index == length) + { + Array.Copy(array, 0, newArray, 0, length); + Array.Copy(collection, 0, newArray, index, collectionLength); + } + else + { + Array.Copy(array, 0, newArray, 0, index); + Array.Copy(collection, 0, newArray, index, collectionLength); + Array.Copy(array, index, newArray, index + collectionLength, newLength - index - collectionLength); + } + + return newArray; + } + + /// + /// Removes the first occurrence of a specific object from the array + /// + /// + /// + /// + public static T[] Remove(this T[] array, T item) + { + int index = Array.IndexOf(array, item); + + if (index == -1) { return array; } + + return array.RemoveAt(index); + } + + /// + /// Removes the element at the specified index of the array + /// + /// + /// + /// + public static T[] RemoveAt(this T[] array, int index) + { + int length = array.Length; + + if(index >= length || index < 0) { return array; } + + int maxIndex = length - 1; + + T[] newArray = new T[maxIndex]; + + if (index == 0) + { + Array.Copy(array, 1, newArray, 0, maxIndex); + } + else if(index == maxIndex) + { + Array.Copy(array, 0, newArray, 0, maxIndex); + } + else + { + Array.Copy(array, 0, newArray, 0, index); + Array.Copy(array, index + 1, newArray, index, maxIndex - index); + } + + return newArray; + } + + /// + /// Resizes the array + /// + /// + /// + /// + /// + public static T[] Resize(this T[] array, int newSize) + { + if(newSize < 0) { newSize = 0; } + + T[] newArray = new T[newSize]; + + Array.Copy(array, 0, newArray, 0, Mathf.Min(newSize, array.Length)); + + return newArray; + } + + /// + /// Reverses the order of the elements in the entire array + /// + /// + /// + /// + public static T[] Reverse(this T[] array) + { + Array.Reverse(array); + + return array; + } + } +} diff --git a/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/UdonArrayExtensions.cs.meta b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/UdonArrayExtensions.cs.meta new file mode 100644 index 0000000..6ce118b --- /dev/null +++ b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/UdonArrayExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c49b5b77c93fb0b41ba630d2851ccf0a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/Varneon.V-Udon.ArrayExtensions.asmdef b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/Varneon.V-Udon.ArrayExtensions.asmdef new file mode 100644 index 0000000..cf149b9 --- /dev/null +++ b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/Varneon.V-Udon.ArrayExtensions.asmdef @@ -0,0 +1,13 @@ +{ + "name": "Varneon.V-Udon.ArrayExtensions", + "references": [], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/Varneon.V-Udon.ArrayExtensions.asmdef.meta b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/Varneon.V-Udon.ArrayExtensions.asmdef.meta new file mode 100644 index 0000000..1ec6395 --- /dev/null +++ b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/Varneon.V-Udon.ArrayExtensions.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 25ec89f76b6a31942b681d9fca4db200 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/Varneon.V-Udon.ArrayExtensions.asset b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/Varneon.V-Udon.ArrayExtensions.asset new file mode 100644 index 0000000..7e3c4b8 --- /dev/null +++ b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/Varneon.V-Udon.ArrayExtensions.asset @@ -0,0 +1,16 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 5136146375e9a0a498a72a0091b40cc1, type: 3} + m_Name: Varneon.V-Udon.ArrayExtensions + m_EditorClassIdentifier: + sourceAssembly: {fileID: 5897886265953266890, guid: 25ec89f76b6a31942b681d9fca4db200, + type: 3} diff --git a/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/Varneon.V-Udon.ArrayExtensions.asset.meta b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/Varneon.V-Udon.ArrayExtensions.asset.meta new file mode 100644 index 0000000..8dadd12 --- /dev/null +++ b/Packages/com.varneon.v-udon.array-extensions/Runtime/Scripts/Varneon.V-Udon.ArrayExtensions.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b8800209388ad7f45b60d158d731b624 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.varneon.v-udon.array-extensions/package.json b/Packages/com.varneon.v-udon.array-extensions/package.json new file mode 100644 index 0000000..de7cbeb --- /dev/null +++ b/Packages/com.varneon.v-udon.array-extensions/package.json @@ -0,0 +1,15 @@ +{ + "name": "com.varneon.v-udon.array-extensions", + "version": "0.1.0", + "displayName": "VUdon - Array Extensions", + "description": "Collection of array extension methods compatible with UdonSharp 1.x which adds partial feature set from List", + "unity": "2019.4", + "unityRelease": "31f1", + "author": { + "name": "Varneon", + "url": "https://github.com/Varneon" + }, + "vpmDependencies": { + "com.vrchat.udonsharp": "1.x.x" + } +} \ No newline at end of file diff --git a/Packages/com.varneon.v-udon.array-extensions/package.json.meta b/Packages/com.varneon.v-udon.array-extensions/package.json.meta new file mode 100644 index 0000000..3751425 --- /dev/null +++ b/Packages/com.varneon.v-udon.array-extensions/package.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 3395de041ddf9904386eb117dfe9da3e +PackageManifestImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.varneon.v-udon.array-extensions/version.txt b/Packages/com.varneon.v-udon.array-extensions/version.txt new file mode 100644 index 0000000..6c6aa7c --- /dev/null +++ b/Packages/com.varneon.v-udon.array-extensions/version.txt @@ -0,0 +1 @@ +0.1.0 \ No newline at end of file diff --git a/Packages/com.varneon.v-udon.array-extensions/version.txt.meta b/Packages/com.varneon.v-udon.array-extensions/version.txt.meta new file mode 100644 index 0000000..7c5e77a --- /dev/null +++ b/Packages/com.varneon.v-udon.array-extensions/version.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 86917d9ec4fc85f48a0618d70e6c1c34 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: