generated from kurone-kito/vpm-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added some utility classes as the extended methods
- Loading branch information
1 parent
bd1ac4b
commit 8c70ed8
Showing
14 changed files
with
369 additions
and
2 deletions.
There are no files selected for viewing
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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
Packages/black.kit.toybox/Runtime/Scripts/Utils/ArrayUtils.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace black.kit.toybox | ||
{ | ||
/// <summary>The utility class for arrays</summary> | ||
public static class ArrayUtils | ||
{ | ||
/// <summary> | ||
/// Determines whether the specified array contains the specified | ||
/// value. | ||
/// </summary> | ||
/// <typeparam name="T">Type of array elements</typeparam> | ||
/// <param name="array">Array</param> | ||
/// <param name="value">The value to contains</param> | ||
/// <returns> | ||
/// True if the specified array contains the specified value. | ||
/// Otherwise, false. | ||
/// </returns> | ||
public static bool Contains<T>( | ||
this T[] array, T value) where T : IEquatable<T> | ||
{ | ||
if (array == null) | ||
{ | ||
return false; | ||
} | ||
foreach (var obj in array) | ||
{ | ||
if (obj.Equals(value)) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Batch setting of game object activation status. | ||
/// </summary> | ||
/// <param name="array">Array of game objects</param> | ||
/// <param name="active">New activation status</param> | ||
public static void SetActive(this GameObject[] array, bool active) | ||
{ | ||
if (array == null) | ||
{ | ||
return; | ||
} | ||
foreach (var obj in array) | ||
{ | ||
if (obj) | ||
{ | ||
obj.SetActive(active); | ||
} | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/black.kit.toybox/Runtime/Scripts/Utils/ArrayUtils.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
Packages/black.kit.toybox/Runtime/Scripts/Utils/PlayerUtils.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using VRC.SDKBase; | ||
|
||
namespace black.kit.toybox | ||
{ | ||
/// <summary>The utility class forVRCPlayerApi.</summary> | ||
public static class PlayerUtils | ||
{ | ||
/// <summary>Get the player name.</summary> | ||
/// <param name="player">Player instance.</param> | ||
/// <param name="fallback">Fallback name.</param> | ||
/// <returns>player name</returns> | ||
public static string GetPlayerName( | ||
this VRCPlayerApi player, string fallback = null) | ||
{ | ||
var concretePlayer = player ?? Networking.LocalPlayer; | ||
return concretePlayer == null || !concretePlayer.IsValid() | ||
? fallback | ||
: concretePlayer.displayName; | ||
} | ||
|
||
/// <summary>Get the safety player name.</summary> | ||
/// <remarks> | ||
/// Note: The fallback string returns <strong>WITHOUT</strong> | ||
/// validation. | ||
/// </remarks> | ||
/// <param name="player">Player instance.</param> | ||
/// <param name="safeCharset"> | ||
/// String containing characters to be checked. | ||
/// </param> | ||
/// <param name="fallback">Fallback name.</param> | ||
/// <returns>player name</returns> | ||
public static string GetSafePlayerName( | ||
this VRCPlayerApi player, string safeCharset, string fallback = null) | ||
{ | ||
var displayName = player.GetPlayerName(fallback); | ||
return displayName.AreAllCharsContained(safeCharset) | ||
? displayName | ||
: fallback; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/black.kit.toybox/Runtime/Scripts/Utils/PlayerUtils.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
Packages/black.kit.toybox/Runtime/Scripts/Utils/StringUtils.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
namespace black.kit.toybox | ||
{ | ||
/// <summary>The utility class for strings</summary> | ||
public static class StringUtils | ||
{ | ||
/// <summary> | ||
/// Determine whether the string contains all the characters | ||
/// in the specified string. | ||
/// </summary> | ||
/// <param name="target">String to be checked.</param> | ||
/// <param name="chars"> | ||
/// String containing characters to be checked. | ||
/// </param> | ||
/// <returns> | ||
/// True if the string contains all the characters | ||
/// in the specified string, | ||
/// </returns> | ||
public static bool AreAllCharsContained( | ||
this string target, string chars) | ||
{ | ||
if (string.IsNullOrEmpty(target)) | ||
{ | ||
return true; | ||
} | ||
if (string.IsNullOrEmpty(chars)) | ||
{ | ||
return false; | ||
} | ||
foreach (char c in target) | ||
{ | ||
if (!chars.Contains(c.ToString())) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/black.kit.toybox/Runtime/Scripts/Utils/StringUtils.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Linq; | ||
using NUnit.Framework; | ||
using UnityEngine; | ||
|
||
namespace black.kit.toybox.Tests | ||
{ | ||
/// <summary>Test class for <see cref="ArrayUtils"/>.</summary> | ||
[TestFixture] | ||
public sealed class TestArrayUtils | ||
{ | ||
/// <summary> | ||
/// Test for the <see cref="ArrayUtils.Contains{T}(T[], T)"/> method. | ||
/// </summary> | ||
[TestCase(new[] { 1, 2, 3 }, 2, ExpectedResult = true)] | ||
[TestCase(new[] { 1, 2, 3 }, 4, ExpectedResult = false)] | ||
[TestCase(null, 2, ExpectedResult = false)] | ||
[TestCase(null, 4, ExpectedResult = false)] | ||
public bool Contains(int[] array, int value) => array.Contains(value); | ||
|
||
/// <summary> | ||
/// Test for the <see cref="ArrayUtils.SetActive(GameObject[], bool)"/> | ||
/// method. | ||
/// </summary> | ||
[Test] | ||
public void SetActive() | ||
{ | ||
GameObject[] array = { new(), null, new() }; | ||
var exists = array.Where(obj => obj); | ||
Assert.DoesNotThrow(() => array.SetActive(true)); | ||
Assert.IsTrue(exists.All(obj => obj.activeSelf)); | ||
Assert.DoesNotThrow(() => array.SetActive(false)); | ||
Assert.IsFalse(exists.Any(obj => obj.activeSelf)); | ||
} | ||
|
||
/// <summary> | ||
/// Test for the <see cref="ArrayUtils.SetActive(GameObject[], bool)"/> | ||
/// method. | ||
/// </summary> | ||
[Test] | ||
public void SetActive_Null() | ||
{ | ||
GameObject[] array = null; | ||
Assert.DoesNotThrow(() => array.SetActive(true)); | ||
Assert.DoesNotThrow(() => array.SetActive(false)); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/black.kit.toybox/Tests/Runtime/TestArrayUtils.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.