Last active
August 16, 2024 12:49
-
-
Save noisecrime/43f69cc87e8a98d65db35a92746f2c86 to your computer and use it in GitHub Desktop.
On-Demand access, loading & caching of Unity BuiltInResource Primitive Type Meshes.
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; | |
namespace NoiseCrimeStudios.Core.UnitySupport | |
{ | |
/// <summary> | |
/// On-Demand access, loading & caching of Unity BuiltInResource Primitive Type Meshes. | |
/// </summary> | |
public static class UnityPrimitives | |
{ | |
private static Mesh unityCapsuleMesh = null; | |
private static Mesh unityCubeMesh = null; | |
private static Mesh unityCylinderMesh = null; | |
private static Mesh unityPlaneMesh = null; | |
private static Mesh unitySphereMesh = null; | |
private static Mesh unityQuadMesh = null; | |
/// <summary> | |
/// Gets the Mesh of 'Unity BuiltInResource Primitive' Type, via 'on demand' caching of Primitive type. | |
/// </summary> | |
public static Mesh TryGetMesh(PrimitiveType primitiveType) | |
{ | |
try | |
{ | |
switch ( primitiveType ) | |
{ | |
case PrimitiveType.Sphere: | |
return unitySphereMesh ?? ( unitySphereMesh = Resources.GetBuiltinResource<Mesh>(GetPrimitiveMeshPath(primitiveType)) ); | |
case PrimitiveType.Capsule: | |
return unityCapsuleMesh ?? ( unityCapsuleMesh = Resources.GetBuiltinResource<Mesh>(GetPrimitiveMeshPath(primitiveType)) ); | |
case PrimitiveType.Cylinder: | |
return unityCylinderMesh ?? ( unityCylinderMesh = Resources.GetBuiltinResource<Mesh>(GetPrimitiveMeshPath(primitiveType)) ); | |
case PrimitiveType.Cube: | |
return unityCubeMesh ?? ( unityCubeMesh = Resources.GetBuiltinResource<Mesh>(GetPrimitiveMeshPath(primitiveType)) ); | |
case PrimitiveType.Plane: | |
return unityPlaneMesh ?? ( unityPlaneMesh = Resources.GetBuiltinResource<Mesh>(GetPrimitiveMeshPath(primitiveType)) ); | |
case PrimitiveType.Quad: | |
return unityQuadMesh ?? ( unityQuadMesh = Resources.GetBuiltinResource<Mesh>(GetPrimitiveMeshPath(primitiveType)) ); | |
default: | |
throw new ArgumentOutOfRangeException(nameof(primitiveType), primitiveType, null); | |
} | |
} | |
catch ( Exception e ) | |
{ | |
Debug.LogError($"UnityGetPrimitiveMesh: Couldn't load Unity Primitive Mesh: {primitiveType}\n{e}"); | |
} | |
return null; | |
} | |
private static string GetPrimitiveMeshPath(PrimitiveType primitiveType) | |
{ | |
switch ( primitiveType ) | |
{ | |
case PrimitiveType.Sphere: | |
return "New-Sphere.fbx"; | |
case PrimitiveType.Capsule: | |
return "New-Capsule.fbx"; | |
case PrimitiveType.Cylinder: | |
return "New-Cylinder.fbx"; | |
case PrimitiveType.Cube: | |
return "Cube.fbx"; | |
case PrimitiveType.Plane: | |
return "New-Plane.fbx"; | |
case PrimitiveType.Quad: | |
return "Quad.fbx"; | |
default: | |
throw new ArgumentOutOfRangeException(nameof(primitiveType), primitiveType, null); | |
} | |
} | |
#if VALIDATE_METHODS | |
// Quick validation Methods to check resources. | |
public static void LogToConsole() | |
{ | |
Debug.Log($"Primitives: {unitySphereMesh} {unityCapsuleMesh} {unityCylinderMesh} {unityCubeMesh} {unityPlaneMesh} {unityQuadMesh}"); | |
} | |
public static void Validate() | |
{ | |
UnityPrimitives.TryGetMesh(PrimitiveType.Capsule); | |
UnityPrimitives.TryGetMesh(PrimitiveType.Cube); | |
UnityPrimitives.TryGetMesh(PrimitiveType.Cylinder); | |
UnityPrimitives.TryGetMesh(PrimitiveType.Plane); | |
UnityPrimitives.TryGetMesh(PrimitiveType.Quad); | |
UnityPrimitives.TryGetMesh(PrimitiveType.Sphere); | |
UnityPrimitives.LogToConsole(); | |
} | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment