Skip to content

Commit

Permalink
add ArrayPlus
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonic853 committed Jan 12, 2024
1 parent 009fcc3 commit db14cf1
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Scripts/ArrayPlus.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
%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: c333ccfdd0cbdbc4ca30cef2dd6e6b9b, type: 3}
m_Name: ArrayPlus
m_EditorClassIdentifier:
serializedUdonProgramAsset: {fileID: 11400000, guid: a75b9eeddf6032e4685b370711a04f9f, type: 2}
udonAssembly:
assemblyError:
sourceCsScript: {fileID: 11500000, guid: aacc8464b2231b94d9c536c46f11daf1, type: 3}
scriptVersion: 2
compiledVersion: 2
behaviourSyncMode: 0
hasInteractEvent: 0
scriptID: 6835145117252330042
serializationData:
SerializedFormat: 2
SerializedBytes:
ReferencedUnityObjects: []
SerializedBytesString:
Prefab: {fileID: 0}
PrefabModificationsReferencedUnityObjects: []
PrefabModifications: []
SerializationNodes:
- Name: fieldDefinitions
Entry: 7
Data: 0|System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[UdonSharp.Compiler.FieldDefinition,
UdonSharp.Editor]], mscorlib
- Name: comparer
Entry: 7
Data: 1|System.Collections.Generic.GenericEqualityComparer`1[[System.String,
mscorlib]], mscorlib
- Name:
Entry: 8
Data:
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
8 changes: 8 additions & 0 deletions Scripts/ArrayPlus.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions Scripts/ArrayPlus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@

using System;
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;

namespace UdonLab
{
public class ArrayPlus : UdonSharpBehaviour
{
public static T[] Add<T>(ref T[] _array, T _value, bool duplicates = true)
{
if (!duplicates)
{
int _index = Index(_array, _value);
if (_index != -1)
return _array;
}
T[] _newArray = new T[_array.Length + 1];
Array.Copy(_array, _newArray, _array.Length);
_newArray[_array.Length] = _value;
_array = _newArray;
return _array;
}
public static T[] AddIndex<T>(ref T[] _array, T _value, int index, bool duplicates = true)
{
if (!duplicates)
{
int _index = Index(_array, _value);
if (_index != -1)
return _array;
}
if (index < 0 || index > _array.Length)
{
return _array;
}
else if (index == _array.Length)
{
return Add(ref _array, _value);
}
else
{
T[] _newArray = new T[_array.Length + 1];
for (int i = 0; i < index; i++)
{
_newArray[i] = _array[i];
}
_newArray[index] = _value;
for (int i = index + 1; i < _array.Length + 1; i++)
{
_newArray[i] = _array[i - 1];
}
return _newArray;
}
}
public static int Find<T>(T[] _array, T _value)
{
return Index(_array, _value);
}
public static int[] FindAll<T>(T[] _array, T _value)
{
int[] _index = new int[0];
for (int i = 0; i < _array.Length; i++)
{
if (_array[i].Equals(_value))
{
Add(ref _index, i);
}
}
return _index;
}
public static T[] FindAllValue<T>(T[] _array, T _value)
{
T[] _newArray = new T[0];
for (int i = 0; i < _array.Length; i++)
{
if (_array[i].Equals(_value))
{
Add(ref _newArray, _array[i]);
}
}
return _newArray;
}
public static int Index<T>(T[] _array, T _value)
{
for (int i = 0; i < _array.Length; i++)
{
if (_array[i].Equals(_value))
return i;
}
return -1;
}
public static T[] RemoveIndex<T>(ref T[] _array, int index)
{
if (index < 0 || index >= _array.Length)
{
return _array;
}
else
{
T[] _newArray = new T[_array.Length - 1];
for (int i = 0; i < index; i++)
{
_newArray[i] = _array[i];
}
for (int i = index; i < _array.Length - 1; i++)
{
_newArray[i] = _array[i + 1];
}
_array = _newArray;
return _array;
}
}
public static T[] Remove<T>(ref T[] _array, T _value)
{
int index = Index(_array, _value);
if (index == -1)
return _array;
return RemoveIndex(ref _array, index);
}
}
}
11 changes: 11 additions & 0 deletions Scripts/ArrayPlus.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit db14cf1

Please sign in to comment.